Hi, I have created a program to blink 3 LED's in sequence. Im just sure if the hardware button is faulty or not. Can anyone share their views on the code?
Code:
import RPi.GPIO as GPIOimport timeLED1 = 16LED2 = 20LED3 = 21BUTTON = 13GPIO.setmode(GPIO.BCM)GPIO.setup(LED1, GPIO.OUT)GPIO.setup(LED2, GPIO.OUT)GPIO.setup(LED3, GPIO.OUT)GPIO.setup(BUTTON, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)while True: for x in range(0,3): while GPIO.input(BUTTON) == GPIO.LOW: pass if GPIO.input(BUTTON) == GPIO.HIGH and x == 0: GPIO.output(LED1,GPIO.HIGH) GPIO.output(LED2,GPIO.LOW) GPIO.output(LED3,GPIO.LOW) while GPIO.input(BUTTON) == GPIO.HIGH: pass while GPIO.input(BUTTON) == GPIO.LOW: pass elif GPIO.input(BUTTON) == GPIO.HIGH and x == 1: GPIO.output(LED1,GPIO.LOW) GPIO.output(LED2,GPIO.HIGH) GPIO.output(LED3,GPIO.LOW) while GPIO.input(BUTTON) == GPIO.HIGH: pass while GPIO.input(BUTTON) == GPIO.LOW: pass elif GPIO.input(BUTTON) == GPIO.HIGH and x == 2: GPIO.output(LED1,GPIO.LOW) GPIO.output(LED2,GPIO.LOW) GPIO.output(LED3,GPIO.HIGH) while GPIO.input(BUTTON) == GPIO.HIGH: pass while GPIO.input(BUTTON) == GPIO.LOW: pass GPIO.output(LED3,GPIO.LOW)
" Im just sure if the hardware button is faulty or not. "
What is the reason for your uncertainty?
How is the button/switch cabled to your RPi board?
Perhaps add some Python print() statements to your script to track progress of execution for diagnostic/debugging purposes.
Maybe the script is working correctly but you never perceive an LED being turned on because it is turned off again very quickly?
You use this code fragment 3 times -
Code:
while GPIO.input(BUTTON) == GPIO.HIGH: pass while GPIO.input(BUTTON) == GPIO.LOW: pass
There seems to be some redundancy here -
Code:
while GPIO.input(BUTTON) == GPIO.LOW: pass if GPIO.input(BUTTON) == GPIO.HIGH and x == 0:
Statistics: Posted by B.Goode — Thu May 23, 2024 1:52 pm