Page 1 of 1
Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:04 pm
by mabrow8454
Hey there,
I've been working on a project where 3 different push-buttons should trigger 3 different motors, and it's very close to working.
Right now, when I push the first button, it says "Button 1 Pressed" and the motor fires. However, when I push the second and third buttons, I get nothing. I'm guessing I've made a mistake with my while loops. Can anyone give any suggestions? Code below.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
while True:
input_state_1 = GPIO.input(3)
if input_state_1 == False:
print('Button 1 Pressed')
GPIO.output(16, True)
time.sleep(2.6)
GPIO.output(16, False)
while True:
input_state_2 = GPIO.input(5)
if input_state_2 == False:
print('Button 2 Pressed')
GPIO.output(18, True)
time.sleep(2.6)
GPIO.output(18, False)
while True:
input_state_3 = GPIO.input(7)
if input_state_3 == False:
print('Button 3 Pressed')
GPIO.output(22, True)
time.sleep(2.6)
GPIO.output(22, False)
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:13 pm
by joan
You need to enclose your script within
quotes. Otherwise we can only guess at how it is meant to work.
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:15 pm
by joan
Mind you it does not seem to be too hard to find an error or two.
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:15 pm
by mabrow8454
Whoops, sorry! Thought I clicked the Code button but apparently not. See below:
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
while True:
input_state = GPIO.input(3)
if input_state == False:
print('Button #1 Pressed')
GPIO(16, True)
time.sleep(2.6)
GPIO(16, False)
while True:
input_state = GPIO.input(5)
if input_state == False:
print('Button #2 Pressed')
GPIO(18, True)
time.sleep(2.6)
GPIO(18, False)
while True:
input_state = GPIO.input(7)
if input_state == False:
print('Button #3 Pressed')
GPIO(22, True)
time.sleep(2.6)
GPIO(22, False)
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:22 pm
by B.Goode
So, having entered the first While loop to check on pin 3, how do you ever move on to do the other 2 checks?
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:23 pm
by RST8
It helps to use the
[/code] directives as we can't see the indentation of your code, but if I read it right then your first while loop will never exit
Code: Select all
while True:
#your code
# next section won't execute as the while loop will never exit
what you want to do I suspect is read the input, then have the while loop based on the input, rather than "True"
I'm assuming that the button press takes the input low rather than high, from your code
Code: Select all
input_state_1 = GPIO.input(3)
while input_state == True :
# sleep a bit
input_state_1 = GPIO.input(3)
# button was pressed, move to next loop
However, I suspect this might not be what you actually want to do as it will only allow you to press the motors in sequence, although perhaps that is what you want.
If you want to be able to press any of the three buttons to start its associated motor, then you need a single loop.
Joe
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:45 pm
by mabrow8454
Hey guys,
We only want one motor to fire at a time. So if you push Button #1 then Motor #1 should fire, and if you push Button #2 then Motor #2 should fire, et cetera.
I agree that my code isn't exiting the first While block. So do you think I could re-do everything inside the same While loop? Maybe like this?
Code: Select all
while True:
input_state_1 = GPIO.input(3)
input_state_2 = GPIO.input(5)
input_state_3 = GPIO.input(7)
if input_state_1 == False:
print('Button #1 Pressed')
GPIO(16, True)
time.sleep(2.6)
GPIO(16, False)
if input_state_2 == False:
print('Button #2 Pressed')
GPIO(18, True)
time.sleep(2.6)
GPIO(18, False)
if input_state_3 == False:
print('Button #3 Pressed')
GPIO(22, True)
time.sleep(2.6)
GPIO(22, False)
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 4:53 pm
by RST8
That's better, assuming that once a button is pressed then that motor will run for 2.6 s and no other motor can run during that time.
Joe
Re: Problem with Python While Loops
Posted: Thu Jul 09, 2015 5:06 pm
by mabrow8454
Had some random syntax errors in my code (should've been "GPIO.output" instead of just "GPIO", and it also didn't like the # character).
But putting everything inside the same While loop did the trick, it's working perfectly now. Thanks guys!