G+_Kyle Boyington Posted October 29, 2015 Share Posted October 29, 2015 Python Programmers Question tl;dr: Make a program respond to a button press, even when it is sleeping (time.sleep(400)). Im building a little night light for my son. I am using a Raspberry Pi, and a led strip. I have come to the point where everything is working to get the lights working. The lights come on when you press the button, and cycle through the colors. Here is where Im stuck: I want the lights to be on for 20 minutes, then slowly fade out over a few minutes.But while the program is waiting for 20 minutes, it is unresponsive to the button presses. This makes sense as it is in a loop. How can I fix this I have been using the threaded callback from a tutorial I found, but doesn't seem to work when sleeping. Link to comment Share on other sites More sharing options...
G+_Eddie Foy Posted October 29, 2015 Share Posted October 29, 2015 Poll the button in the loop? Maybe sleep for a second 400 times in a/the loop? Does the RPi support IRQs on the GPIOs? That wold be another option, but stickier. If the sleep is just for a delay and not a power savings dealio, can you spin off another thread to deal with the button? Link to comment Share on other sites More sharing options...
G+_Ben Reese Posted October 30, 2015 Share Posted October 30, 2015 To fade you'll be using PWM. There are some pins with PWM built in, but I don't remember which. By sleep, you mean the Pi is becoming unresponsive? In your Python loop, have it store the fade_off time in a variable. When the button is pressed, that fade_off time gets reset to now + 20 min. If now is >= fade_off, start fading off; otherwise, full brightness. On my phone so I can't really type out the pseudo code very well, but hopefully this helps. Let me know if I'm misunderstanding the problem. Link to comment Share on other sites More sharing options...
G+_Scott Richards Posted October 31, 2015 Share Posted October 31, 2015 Hi Kyle, I see you've got the LED states and the fade under control. You've identified the problem with "sleep", not good for that use. It's my understanding that sleep states usually are for battery conservation, and minimal processes are used while waiting for the time to expire. Python class was 5 yrs ago, so can't help with finessing what you've already done. However, here's what my approach would be. (no proper syntax is used here!) ----- [TOP] The first section will be just a loop that looks for two things: 1) Is the button pressed? If not, then it falls to 2) IF button is pressed, THEN CALL subroutine ChangeState, which sets the "Start-time" =NOW() and sets "ON_timer" >0 2) Is the "ON_timer" > zero? -- which indicates that the light is "on", perhaps dimming. IF "ON_timer">0 THEN CALL subroutine Timer -- Exiting this loop to run other parts of the program & constantly returning here. RETURN -- End of loop -- Any time it's waiting it's constantly checking the button & timer. [Timer] IF ON_timer >Set-Time -- your 20 mins CALL Subroutine Dimmer ON_timer = NOW()-Start_Time RETURN TOP Link to comment Share on other sites More sharing options...
Recommended Posts