Tip:
Highlight text to annotate it
X
Hi. Are you ready for Valentine's Day? In this video we are going to show you how, with
a microcontroller, and a little bit of engineering, you too can make this LED heart for your valentine.
Complete with an LCD screen for your personalized message of love and a randomized twinkling
effect, this special gift is sure to melt some hearts. Think about it. How many girls
out there can say that their Valentine's card runs at 14 megahertz?
This project borrows a lot of concepts from the LED array display we showed you how to
make in a previous video. If you haven't seen it, check out the DIY marquee video on the
tutorials page.
The physical construction of the heart is fairly straightforward. We cut out a cardboard
heart and spray painted it red, and drilled holes for the LEDs. We mounted the NerdKits
LCD in the middle of the heart and stuck the LEDs on. This setup uses the same trick as
a LED array project to double the number of LEDs, which allows us to control 20 LEDs with
11 pins. So far the LED heart has followed the same
setup as the LED array project, so unsurprisingly the code is going to borrow a lot from it
too. The only major difference between the two projects is that on the LED heart, we
need individual control of the brightness of each LED, not just on or off. To do this,
we represent each LED not by a 1 or a 0 for on or off, but by a number that's going to
represent the duty cycle of each LED. Having a 1 would mean that the LED is on for only
one out of 64 cyles making the light very dim. Having a 64 will mean the LED is on for
64 out of 64 cycles, making the light on at full brightness. The cycles are fast enough
so that if you don't see the LED blinking -- it's just dimmer if it's not on at full
duty. Let's take a look at the code. Most of the
lower level functions for turning the LEDs on and off are the same as the LED array project,
with a simple modification we described before. The main function first writes a message to
the LCD. Then it calls the twinkle function that gives the appearance of a randomized
twinkle in your heart. First, it sets the LEDs to a small starting
duty cycle. The function sits in a while loop that essentially keeps a state machine for
each LED in the heart. At any point in time, the LED can be off, increasing in brightness,
or decreasing in brightness, which are represented by a 0, 1 and 2 in the state array. At each
iteration of the while loop, the code takes the next LED and chooses a random number.
With a 1/10th probability, that LED will get switched to state 1. The code then goes through
and increases the brightness of all LEDs in state 1 by 1, and decreases the brightness
of all the LEDs in state 2 by 1. Whenever the LEDs reach the maximum brightness, they
get set to state 2, and when they reach the minimum brightness, they get set to state
0, until it's their turn to start another cycle.
The fact that we use a random number to choose which LEDs start a cycle creates a very nice
randomized effect, like the twinkling of stars in the night sky. For an added effect, the
code also randomizes the maximum brightness they reach every time, which makes some LEDs
light up quickly, and some get very bright before returning to their off state.
Hopefully, this video gives you an idea of how you can use concepts from previous projects
and turn them into a thoughtful gift that really comes from the heart. For more information
about our kits or more videos like this one, visit us at www.NerdKits.com.