Using a switch

22 Nov

Continuing with the experiments on our Tron-like LED bracelet we added a switch to make it more comfortable to turn the Lilypad on and off without having to touch the board.

We used alligator clips for this experiment, but you can also use conductive thread.

Start by attaching the On pin of the switch to one of the digital pins of the board (we used the pin 5) and the Off pin to the – pin of the board. Then connect your Lilypad to the computer and add the switch in your code. We kept the LEDs blinking like in the previous example.

Source code:

int ledPin = 6; // LED is connected to digital pin 6
int switchPin = 5; // switch connected to digital pin 5
int switchValue; // a variable to keep track of when switch is pressed

void setup()
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(switchPin, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin, HIGH); // sets the default (unpressed) state of switchPin to HIGH

void loop() // run over and over again
switchValue = digitalRead(switchPin); // check to see if the switch is pressed
if (switchValue == LOW) { // if the switch is pressed then,
digitalWrite(ledPin, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the pin 6 LED off
else { // otherwise,
digitalWrite(ledPin, LOW); // turn the LED off


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: