Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My While True loop is getting stuck in python

Hello I have been working on an infinite While True loop for the main file in my python code. I am working on a Raspberry Pi and my goal is that whenever one of the GPIO Pins senses an input it will print out a string. However When I push a button it will keep printing it infinitely and the only way for it to stop is by hitting Ctrl-C. While it is printing the same string over and over no other button will change what happens. What am I doing wrong did I forget a step somewhere?

import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(6, GPIO.IN)

input_A = GPIO.input(26)
input_B = GPIO.input(19)
input_C = GPIO.input(13)
input_D = GPIO.input(6)

while True:
    if input_A == True:
            print('A was pushed')

    if input_B == True:
            print('B was pushed')

    if input_C == True:
            print('C was pushed')

    if input_D == True:
            print('D was pushed')

    sleep(1.5);

1 Answers

You need to keep updating your input_* variables inside your while loop

while True:
    input_A = GPIO.input(26)
    input_B = GPIO.input(19)
    input_C = GPIO.input(13)
    input_D = GPIO.input(6)

    if input_A == True:
            print('A was pushed')

    if input_B == True:
            print('B was pushed')

    if input_C == True:
            print('C was pushed')

    if input_D == True:
            print('D was pushed')

    sleep(1.5);
like image 77
Peter Gibson Avatar answered Sep 11 '26 04:09

Peter Gibson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!