There are some good examples on how to do this on the rapberry-gpio-python project on SourceForge. Look at the wiki pages on "Inputs" and "PWM". You can check the input state by polling or waiting for its status to change. Your current code looks like it's set up for polling, which is fine, but waiting for a status change is more efficient and consumes less resources. You can change the blinking frequency when you see the input status change by calling PWM.ChangeFrequency().
Kayla T.
asked 02/07/22Changing a LED's blinking rate
So initially, the LED starts out by blinking continuously on and off for 0.5s. Then you have to program it so that when the switch is pressed it changes the LED's blink rate from 0.5s to 0.1s. When the switch is released it goes back to its original blink rate of 0.5s.
Here is what I have so far:
import RPi.GPIO as GPIO
from time import sleep
led = 17
button = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
while True:
GPIO.ouput(led, GPIO.HIGH)
sleep(0.5)
GPIO.output(led, GPIO.LOW)
sleep(0.5)
1 Expert Answer
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.