I want to make a program that does a few things in a random order: I have this so far:enter image description here

The problem here is... I have set the switch block with the random number block to 3 different programs to run. So of course they run randomly. But I want to make it like this: When program 1 on the switch block has finished the random number cannot be 1. 2 can run twice but not three times, the same for 3. 2 can never run after 3 or 3 after 2. So when 2 finished the program, all of the numbers can be the case. If 2 comes after two, the next step should be 1. Not 3 because I made number 3 the reversed of 2 which I don't want to make run after eachother. This is an example of how it should act in the loop that's going 6 times:enter image description here

2 Answers 2


This looks like a state machine.

Save the "state" of your program in a variable. The state will be which program to run, so 1, 2 or 3.

You will also need some more variables to track the previous state and how many times a program has run.

Code flow will be something like this:

next_state = 1
prev_state = 0
run_count = 0
begin loop
    switch next_state
        case 1: run program_1
        case 2: run program_2
        case 3: run program_3
    end switch
    if next_state == prev_state
        run_count = run_count + 1
       run_count = 0
    end if
    prev_state = next_state
end loop

I left out the implementation of compute_next_state since you have that spelled out quite well in your diagram.

  • I'm not sure how to use this code... I am not very skilled in coding and coding with nxt I have 0.0 experience...
    – Hendriks3D
    Feb 15, 2016 at 0:04

To make it easier, I added an extra program to run which is the same as 2X program 2 or 3. Because Program 2 and 3 are 90 degree turns. So now it should be clearer what the idea is: enter image description here

So the loop could start on program 1 and run 2, 3 or 4 after, but it could also start on 2, 3 or 4 on which the next step always program 1 is. It could stay running and keep taking infinite steps. So only after the first program is randomly chosen, the rest of the random chosen programs will be after program 1. The rest is in a logical order. After program 1 the system will randomly choose a new program for the next step that could be any of the steps exept program 1. So a long way of steps could look like this: Program 2 - Program 1 - Program 4 - Program 1 - Program 2 - Program 1 - Program 3 - Program 1 - Program 3 - Program 1 - Program 4 - Program 1 etc. etc. etc. The amount of steps that will be taken is the amount of times that the loop is set to. Right now I have made it in blocks. Very simple. Only the first two steps could be the same, but that's no problem to me. :) enter image description here Even though I found it out myself I thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.