LilyPad Arduino: sensing (switches)
1. If you haven't yet, make a coaster for your LilyPad
See this tutorial for instructions. This will prevent the alligator clips that we'll be using from sliding around on the LilyPad. Trust me, it's worth it to do this!
2. Make a simple switch out of alligator clips
A switch is basically 2 pieces of conductive material that are sometimes pressed together and sometimes kept apart. The switch is CLOSED (pressed or triggered) when the conductors are pressed together and OPEN when the conductors are separated. We'll make a really simple switch using 2 alligator clips.
Attach a black alligator clip to the - tab on the LilyPad and an alligator clip of a different color (preferably not red) to tab 2 on the LilyPad. Now, when we touch the two alligator clips together we are closing or "pressing" the switch. Note that when we touch the clips together, the switchPin (flower petal 2) will be attached to ground or "-" via the alligator clips. We refer to ground or "-" in Arduino code as "LOW" and power or "+" or "+5V" as "HIGH". More about this in a second.
3. Attach the LilyPad to your computer and start the Arduino software
4. Copy this sample code into an Arduino window
Click here for switch sample code. Copy and paste this code into an empty Arduino window.
5. Format the code
Under the Tools menu, select Auto Format. After you do this, align all of your comments (the statements in grey-brown following "//" on each line) so that they are in readable columns on the right hand side of the screen. This will help you read through the code. Here's what my Arduino window looked like after I formatted everything:
Read through the code to get a sense of what it is doing. The comments at the end of each line should help you understand what's happening. Note that in the code we are listening for a LOW signal on the switchPin. We turn on the LED when the switchPin is attached to ground. As was mentioned earlier, when we put the two alligator clips together this is exactly what is happening: the switchPin is attached to ground via the clips. So, let's test it out in the real world...
6. Load the code onto the LilyPad
Compile the code and load it onto the LilyPad. Do this by hitting the upload button in the Arduino window (that's the right pointing arrow at the top of the Arduino window) and then quickly pressing the reset switch on the LilyPad. If you're new to this process, or if you have any problems uploading, see the previous LilyPad tutorial: 4. light (LEDs).
7. See what happens when you close the switch!
The LED should come on. If it doesn't, check to make sure your alligator clip connections are good. Here's what my switch triggered board looks like. Look closely to see the light ;-):
7. Play with modifying the code to get different behavior
8. Build your own switch
As you can see from the alligator clip example, it's easy to build a switch. Play with different materials to make your own switches. Some of the interesting conductive materials that you can use to make switches are conductive velcro, conductive fabric, conductive thread, aluminum foil, metal springs and metal beads. Use your imagination and whatever is lying around the house!