mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Problem with Python While Loops

Thu Jul 09, 2015 4:04 pm

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)

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:13 pm

You need to enclose your script within

Code: Select all

 
quotes. Otherwise we can only guess at how it is meant to work.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:15 pm

Mind you it does not seem to be too hard to find an error or two.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:15 pm

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)

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:22 pm

So, having entered the first While loop to check on pin 3, how do you ever move on to do the other 2 checks?

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:23 pm

It helps to use the

Code: Select all

[code]
[/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

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:45 pm

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)

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: Problem with Python While Loops

Thu Jul 09, 2015 4:53 pm

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

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: Problem with Python While Loops

Thu Jul 09, 2015 5:06 pm

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!

Return to “Troubleshooting”