0

I'm trying to write a robot drive python script. There are two variations of this code.

  1. To go when the button is pressed, and stop when the button is unpresed.
  2. 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.

closed as off-topic by joan, techraf, Milliways, Darth Vader Feb 15 '18 at 16:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – joan, techraf, Milliways, Darth Vader
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Nothing to do with the Pi. This is a Python question. Hint: what do you think try/except do? – joan Feb 14 '18 at 13:54
  • It uses the SBC Raspberry Pi Motor Shield. I thought that the except would interrupt the forward() function. – wolf_math Feb 14 '18 at 14:46
1

Try/Except is used to catch errors (exceptions)

Since I cannot see where you are calling go() from, I dont know if go() should be an infinite loop, but if it is

def go():
    while True:
        if not button_40=input(40):
            forward()
            continue  #back to start of while True loop
        stop()
  • Thanks for the try/except tip. I'm calling go from the shell (for now). I've tried your scripts and others like it, but they don't stop when I let up on the button. – wolf_math Feb 14 '18 at 20:05
  • I edited the answer, the variable button_40 was not being updated, it always was going to be its original state. When you say calling it from the shell, do you mean from a python shell or bash? – Chad G Feb 14 '18 at 21:27
  • the other thing to do is put print statements in your forward and stop functions so you will know if the code got there. And any other place you are not sure you are reaching. This will help you (or others debug where the code is going) feel free to update the code and include any output – Chad G Feb 14 '18 at 21:29
1

The except runs when the try fails in an error, In the case the 'try' runs, and doesn't fail during the while loop, thus not giving a chance for the 'try' to error and then run thru the 'except' code and send thru the stop() function.

Perhaps use 'if' statements to check conditions and save the 'try' 'except' for cases you need to catch an error.

  • That works only if I want the script to run forever. However it doesn't run stop() unless I type it into the shell. I want it to stop when the button is no longer pressed. – wolf_math Feb 14 '18 at 20:07
  • @mrSidX Apparently python does not have switch/case flow control syntax. – goldilocks Feb 15 '18 at 13:57
  • @goldilocks, you are correct and this method will not work in Python. – mrSidX Feb 15 '18 at 15:50
0
 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)
 button_40=GPIO.input(40)

 GPIO.setup(38,GPIO.IN, pull_up_down=GPIO.PUD_UP)
 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()
     print "going fwd:"

 def stop():
     m1.stop()
     m3.stop()
     af.off()
     print "stop!"

 def go():
     try:
         while (button_40 == False and button_38 = True):
              button_40=GPIO.input(40)    #Revive the pin state var each pass
              button_38=GPIO.input(38)
              if button_38 == False: 
                 print 'btn 38 = False!'
                 stop()
                 continue()
              else
                 forward()
     except Exception as inst:
          print type(inst)

loop go() if you exit the while loop maybe...

There may be some redundancy in this code, and I yet to test it, but I read similar thread to what you trying to do and the user had positive results putting the if's inside the while loop. Im wondering by assigning the button_40 and 38 pin variables in the beginning of your loop will assign a HIGH or LOW to a variable, and is not a reference to the GPIO pin itself, so when the Var gets a HIGH or LOW, it's going to stick, unless it gets reassigned a new (changing) value.

  • This looks similar to something I've tried. With this code I keep getting errors and I'm not sure why. local variable 'button_40' reference before assignment – wolf_math Feb 15 '18 at 20:02

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