6

Originally, I had a simple program to move until the robot hit a wall, then it reversed and turned and moved again, repeated to infinity. It works by turning the motors on, waiting for input from the touch sensor, then reversing, turning, and continuing. Now I want to add a second wait on the touch sensor to trigger the reverse and turn sequence, but I can't add two different ports to wait from, or have two different triggers to start the reverse and turn. Is there any way to have a trigger from two different wait inputs?

2

You can do this with parallel programs and a logic variable. You will need 3 loops, one for each sensor and one for the motor. In the sensor loops, wait for the touch sensor to be pressed and then set a logic variable to true. In the motor loop, have a switch block that tests the logic variable. If it is true, reverse and turn around and then set the logic variable to false. If the logic variable is already false, just go forward.

Code sample

  • Thanks a lot. I considered using a variable to track it, but I wanted to know if there was a simpler, more intuitive way. Your script is probably better than what I could have come up with. – Saamoz Apr 8 '15 at 18:16
2

Here is another pattern that watches more than one sensor. Instead of parallel code blocks setting a variable value, this code combines sensor outputs to feed a logic block which feeds a switch. No variable is used.

Note that only the touched side of the switch statement is used. Not an important difference, I just find it easier to follow.

The other pattern might be described as event driven, which is used widely in programming.

Backup, turn and go forward when either touch sensor is touched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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