Two LED projects for light and sound

Lines 1-4 – Imports

The first four lines import the assorted libraries that I'll use to create the lightning effects. The RPi.GPIO library sets up access to the physical GPIO pins on the Raspberry Pi, and as GPIO lets me refer to it in the program as GPIO instead of having to type RPi.GPIO whenever I use it in the program. Using time gives access to the time.sleep function – a blocking delay function – and random has all sorts of functions for generating random numbers. Finally, pygame in this case only provides the library to play back audio.

Lines 6-15 – The Strike Function

The strike function creates the bright initial flash of the lightning strike. It has five arguments; lightning1 and lightning2 are the references to the PWM objects (lightning panel brightness). These are created in the setup section that follows. The brightness argument is how bright the lightning flash should be, and rumbleBrightness and rumbleLength are passed through to the rumble function to finish out the entire strike effect.

Line 7 sets the PWM duty cycle of the first panel to the requested brightness. Line 8 delays for 0.03 seconds, then line 9 sets the same brightness on the second panel. After another 0.03-second delay, the panels are turned back off (lines 11, 13) with a delay in between (line 12). This is fast enough that your eyes can't adapt, so it creates the blinding effect. Then, the rumble function is called (line 15) to simulate smaller strikes happening after the initial one.

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