Motor Direction via Touch Sensor

Content

 Change the motor direction via the Touch Sensor.  That is: every time you press the touch sensor, it changes the motor direction.

Code

 This (version 1) runs the motor until the Touch Sensor is pressed.  Then stops the motor.

Code

 This (version 2) runs the motor forward, waits for touch, then backward, waits for touch.  If you want to do this several times in a row, you could copy-and-paste this second of code over and over.  (But there is a better way, using loops; see version 3.)

Code

This (version 3) does the forward/wait/backward/wait sequence in a While Loop, and runs forever.

Code
This (version 4) accomplishes the same as version 3, but with only ONE motor command.  Each time the loop executes (each time the button is pressed), the loop counter is used to determine the direction. If it is even, the motor goes forward; if it is odd, the motor goes backward.

Leave a Reply

Your email address will not be published. Required fields are marked *