Two LED projects for light and sound

Lines 49-63 – The Main Loop

This section monitors the sensor and runs the effect when it is tripped. On line 50, the program waits in an infinite loop as long as the sensor is returning 1 (HIGH, no person detected). Remember that on line 36, I set the pull-up resistor on the GPIO input line. Thus, as long as the sensor isn't reading anything, the line will be HIGH, even if it's not connected at all. Line 51 says "don't do anything." This repeats until the sensor beam is broken.

When the beam is broken, the rest of the loop runs. The program first waits an additional 5 seconds (line 53), which lets the people get past the sensor and right up on the scene before it lights up. It also does a remarkable job in camouflaging the sensor, because visitors have already passed it well before it goes off.

Line 54 plays the sound clip, then the rest of the timing makes the lighting align with the audio (lines 55-61). Note that I never call rumble, only strike. Because the end of the strike function calls rumble automatically, it's not necessary in the main loop.

Line 63 makes the Raspberry Pi wait for 15 seconds before triggering again. The bridge and thunderstorm on the model train layout are located at a dead end, so as visitors view the scene, turn around, and leave again, 15 seconds is just about right to keep the effect from being triggered twice.

The Author

Scott enjoys programming in Python on Raspberry Pis and playing with model trains. When he's not doing those two things, he can usually be found stargazing at dark-sky sites.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content