Answers.Body: <p>You can use finite state machine. In this case you will have these 5 states:</p>
  
  <ol>
  <li>RUN</li>
  </ol>
  
  <p>Action: turn on the motors for moving straight</p>
  
  <p>Transition to state TURN in case of event: ultrasonic sensor detects obstacle closer than 4.5 inch).</p>
  
  <p>Transition to state WAIT in case of event: color sensor detects red color</p>
  
  <ol start="2">
  <li>WAIT</li>
  </ol>
  
  <p>Action: stop motors</p>
  
  <p>Transition to state CONTINUE in case of event: color sensor detects green</p>
  
  <ol start="3">
  <li>CONTINUE</li>
  </ol>
  
  <p>Action: move forward a bit while avoiding the flag</p>
  
  <p>Transition to state RUN immediately.</p>
  
  <ol start="4">
  <li>TURN</li>
  </ol>
  
  <p>Action: Turn the robot 90 degrees right, increment the rotations</p>
  
  <p>Transition to state STOP in case of event: rotations = 4</p>
  
  <p>Transition to state RUN in case of event: rotations &lt; 4</p>
  
  <ol start="5">
  <li>STOP</li>
  </ol>
  
  <p>Action: Interrupt the program.</p>
  
  <p>Here is the code I've just scratched without a testing, let me know if it works:
  <img src="https://i.sstatic.net/5TUfH.png" alt="enter image description here"></p>
  
  <p>You can enhance the state machine to check for the flag during rotations, too. :)</p>
  
  <p>You can learn this robotics topic from the great book by Daniele Benedetelli - LEGO MINDSTORMS EV3 Laboratory. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-13T21:34:18.743Z
Answers.LastActivityDate: 2014-11-13T21:44:42.330Z
Answers.LastEditDate: 2014-11-13T21:44:42.330Z
Answers.UserDisplayName: MirecXP
Body: <p>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.
  <img src="https://i.sstatic.net/0dbTj.jpg" alt="Here is a picture of the program that is my latest iteration."></p>
  
  <p>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.</p>
  
Tags: <ev3><programming>
Title: Trying to get a modified traffic signal challenge worked out
UserDisplayName: Jim LovellFord
ThreadText: # Trying to get a modified traffic signal challenge worked out
  
  (Asked by: username_0 on 2014-11-12)
  
  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.](https://i.sstatic.net/0dbTj.jpg)
  
  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.
  
  **Tags:** ev3, programming
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-13. Score: 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
  
  2. WAIT
  
  Action: stop motors
  
  Transition to state CONTINUE in case of event: color sensor detects green
  
  3. CONTINUE
  
  Action: move forward a bit while avoiding the flag
  
  Transition to state RUN immediately.
  
  4. 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
  
  5. 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](https://i.sstatic.net/5TUfH.png)
  
  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.
  
  
  ---
  
  