This Piper STEAM Project was contributed by Matthew Matz.
It’s the spookiest time of the year and there’s no better way to celebrate than by the eerie glow of a jack-o’-lantern! If the mess that comes with carving a real pumpkin isn’t your cup of tea, fret not, we have a crafty solution.
Our latest PiperCode project will show you how you can use your Piper Computer Kit to simulate the twinkling light from inside a jack-o’-lantern using the LEDs in your kit.
Get your parts ready - here’s what we’ll need:
- A pumpkin-shaped decoration
- 1 Red LED
- 1 Yellow LED
- 4 Jumper wires
- Small Breadboard
Next, set up your LEDs by connecting them like so:
To find the correct GPIO pins, you can use the electronics tab in PiperCode. On that diagram, ground pins are outlined in black and multipurpose pins in grey. Their pin numbers are also shown.
**Note: The Raspberry Pi may turn off if a short circuit occurs between its ground and high-voltage GPIO pin to avoid heat damage.
Now that your LEDs are wired up, time to start coding! Open PiperCode and set the Speed to “Run”, pictured below.
Our goal is to make the LEDs turn on and off randomly. This will make them appear to flicker like the light from a candle.
From the Loops menu on the left side, select the “repeat forever” block. Drag and drop it into the programming area. This will make sure that our program stays running:
Next, from the Logic menu, select the “if...do...else” block. Connect an “__ = __” block to the “if...do...else” block:
Place the “if...do...else” block inside of the “repeat forever” block:
To make the lights blink randomly, we’ll use the “random number from” block, and have to pick a random number from 0 to 1. Since there are only two choices, we can use one of the choices to turn the LED on and use the other to turn it off.
On one side, add the “random number from __ to __” block from the Logic menu, and on the other, add a “0” block from the Variable menu. Add a “0” block to both of the blanks in the “random number from” block, and change the second one to a “1”.
Finally, from the Chip menu, grab a “Turn Pin ___ ON” block. Place one in the “do” part of the “if...do...else” block, and place one inside of the “else” part. Set both “Turn Pin” blocks to “Pin 22”, and set the menu in the “Turn Pin 22” block in the “else” part to “OFF”:
Try running your program by clicking “Run” in the top-left corner of your screen. You should see the Red LED start to flicker. We’re almost there!
To make the Yellow LED flicker too, we’ll have to add a little more code. Since we want both the Red and Yellow LEDs to do the same thing, we can just copy the Red LED’s code and make a couple of changes.
Right-click the “if...do...else” block and choose “duplicate”. Place the new set of blocks right below the first “if...do...else” block inside of the “repeat forever” block. Change the “Turn Pin” blocks on the new blocks from “22” to “24”:
Click “Run” in the top-left corner to watch your LEDs start flickering.
To make it appear more like a candle, you can diffuse (a fancy way to say “spread out”) the light by placing a piece of paper, tissue, or thin plastic from a shopping bag over the LEDs.
You can place your breadboard in a candle holder or a carved out pumpkin, too!
If you need to make your wires longer, simply connect them end to end:
We hope you enjoyed this Piper STEAM project! Have a great Piper project idea? Let us know in the comments below.