The challenge is for a robot to go around the perimeter of a rectangular field and execute 90 degree turns when it gets within 4.5 inches of a wall. Simultaneously, if the bot encounters a red flag, it needs to stop until the flag color turns green. At that point, the arm holding the color sensor needs to dip to avoid the traffic signal, and once it is passed, continue on it's way. Here is a picture of the program that is my latest iteration.

I have tried running two threads, one where the bot encounters a flag (which I have constructed a switch for cases red and green), while the other is where it encounters a wall. I think these two threads need to be encased within an infinite loop, but I can't seem to make it so.

1 Answer 1


You can use finite state machine. In this case you will have these 5 states:

  1. RUN

Action: turn on the motors for moving straight

Transition to state TURN in case of event: ultrasonic sensor detects obstacle closer than 4.5 inch).

Transition to state WAIT in case of event: color sensor detects red color

  1. WAIT

Action: stop motors

Transition to state CONTINUE in case of event: color sensor detects green


Action: move forward a bit while avoiding the flag

Transition to state RUN immediately.

  1. TURN

Action: Turn the robot 90 degrees right, increment the rotations

Transition to state STOP in case of event: rotations = 4

Transition to state RUN in case of event: rotations < 4

  1. STOP

Action: Interrupt the program.

Here is the code I've just scratched without a testing, let me know if it works: enter image description here

You can enhance the state machine to check for the flag during rotations, too. :)

You can learn this robotics topic from the great book by Daniele Benedetelli - LEGO MINDSTORMS EV3 Laboratory.

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.