I have a problem with this python script:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
#MOTOR 1
Motor1A = 22
Motor1B = 24
Motor1E = 26
#MOTOR 2
Motor2A = 19
Motor2B = 21
Motor2E = 23
#MOTOR 3
Motor3A = 11
Motor3B = 13
Motor3E = 15
#MOTOR4
Motor4A = 12
Motor4B = 16
Motor4E = 18
#VAR
true = 1
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)
GPIO.setup(Motor3A,GPIO.OUT)
GPIO.setup(Motor3B,GPIO.OUT)
GPIO.setup(Motor3E,GPIO.OUT)
GPIO.setup(Motor4A,GPIO.OUT)
GPIO.setup(Motor4B,GPIO.OUT)
GPIO.setup(Motor4E,GPIO.OUT)
GPIO.setup(7,GPIO.IN)
print "Turning circuit ON in ten seconds, check everything before starting!!!!!!! "
sleep(6)
print "starting in..."
sleep(1)
print "3"
sleep(1)
print "2"
sleep(1)
print "1"
sleep(1)
print "starting propeller"
while true == 1:
if (GPIO.input(7) == 0):
GPIO.output(Motor4A,GPIO.HIGH)
GPIO.output(Motor4B,GPIO.LOW)
GPIO.output(Motor4E,GPIO.HIGH)
else:
GPIO.output(Motor4E,GPIO.LOW)
sleep(5)
print "Lifting"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)
sleep(2)
GPIO.output(Motor1E,GPIO.LOW)
print "Moving"
GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.HIGH)
sleep(2)
GPIO.output(Motor2E,GPIO.LOW)
print "Dumping"
GPIO.output(Motor3A,GPIO.HIGH)
GPIO.output(Motor3B,GPIO.LOW)
GPIO.output(Motor3E,GPIO.HIGH)
sleep(5)
GPIO.output(Motor3E,GPIO.LOW)
sleep(3)
print "Moving back"
GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)
sleep(2)
GPIO.output(Motor2E,GPIO.LOW)
print "Dropping"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)
sleep(2)
GPIO.output(Motor1E,GPIO.LOW)
sleep(5)
GPIO.cleanup()
That pin input on gpio pin 7 is a button, and i want it to trigger a part of the code once is pressed, if is not, then run just another part of the code... the problem is that it ignores the button and it jumps to the "else" function... could someone help me please? and sorry for my english.
[update] the idea is to respond as quickly as possible as soon as the button is pressed, but i don't know where to start
the buton is wired with a pull down resistor (10kohmn) from 3.3 pin on raspberry
[update] It works! it was just a bad connection on the pull down resistor. I always forget to check connections. thanks guys!
-
How do you have the button wired? Please include pics of all the connections. What happens if you change if (GPIO.input(7) == 0): to if (GPIO.input(7) == 1):Steve Robillard– Steve Robillard2016年09月23日 04:44:55 +00:00Commented Sep 23, 2016 at 4:44
1 Answer 1
I have the following observations / queries:
- the whole process is single threaded with a round robin check to see if the button is pressed
- are you holding the button down when the initial countdown starts and the code enters the loop ?
- assuming no to the above, the first time the loop executes, the
if
condition fails as button is not depressed and the else part executes, the second time theif
will be checked is only after time> 26 seconds (sum of all sleep duration in theelse
part) - are you holding the input pin that long (t>26 seconds) such that the
if
condition will succeed the second time around? i'd recommend adding a print statement in theif
section as well to verify this section of code actually executes.
if your intention is to respond to the button press as quickly as it happens, the way i see it the solution requires a bit design something on the lines of
- having a task (priority) queue
- handling asynchronous (interrupt like) callbacks when the input pin status changes and pushing as 'handle input task' with highest priority into the queue
- timer based callbacks adding new 'change o/p pin' tasks (with lower priority than i/p) for changing the o/p pins instead of actual
sleep
calls - executing each task in the queue in the order it appears (which should be highest to lowest)
HTH