I'm trying to write a robot drive python script. There are two variations of this code.
- To go when the button is pressed, and stop when the button is unpresed.
- To go when the button is pressed, and stop when secondary button is pressed.
I'm able to get the robot moving, but I'm unable to get it to stop. I can't seem to stop the forward function once it's started (unless I manually type it into Idle)
For this project I'm using the SBCompenents MotorShield. My current work is as follows.
import RPi.GPIO as GPIO import PiMotor from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(40,GPIO.IN, pull_up_down=GPIO.PUD_UP) #using GPIO 40 button_40=GPIO.input(40) GPIO.setup(38,GPIO.IN, pull_up_down=GPIO.PUD_UP) #using GPIO 38 button_38=GPIO.input(38) m1=PiMotor.Motor("MOTOR1",1) #motor1 m3=PiMotor.Motor("MOTOR3",1) #motor3 af=PiMotor.Arrow(3) #forward LED light def forward(): m1.forward(100) m3.forward(100) af.on() def stop(): m1.stop() m3.stop() af.off() def go(): try: while button_40 == False: forward() except: if button_38 == False: stop()
When I run the go() function, the bot will go forward, but it will not stop.