Glitch and the LED Sneakers – First Test

At this point I broke out the croc clips and the LEDs and started experimenting. Whenever you click ‘Upload’ in the Arduino IDE it compiles your current code, transfers it to your Lilypad, reboots the Lilypad and starts the code running – so the feedback loop for trying out ideas & seeing what works is very fast.

My first test hardware involved connecting the button and 4 of the LEDs to pads on the Lilypad. I started from the basic Blink program and added more LEDs and more delays to give some visual effects. I also added the ability to detect button presses in order to change the pattern.

I defined a couple of constants in my code:

int pin1 = 9;
int pin2 = 10;
int pin3 = 11;
int pin4 = 3;
int pinButton = 12;

then I needed to add code into setup() to tell the Lilypad how the hardware was configured:

pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pinButton, INPUT);
digitalWrite(pinButton, HIGH);

The digitalWrite() for a pin configured as an input might seem counter-intuitive but it’s an important step. It makes the Lilypad connect the input line to an internal pull-up resistor so that, when the line isn’t pulled down to 0v by the button being pressed, the line is at a solid 5v. Omit this line and you’ll get a lot of spurious signals from the button as the Lilypad tries to work out what the floating input line is doing.

Then in the main loop() code I can use

if ((digitalRead(pinButton) == LOW)) {
  // code goes in here for when the button's pressed
} else {
  // code goes in here for when the button's not pressed
}

to detect if the button is down and adjust the output accordingly. The first code block contains

  digitalWrite(pin1, HIGH);  // turn on the first LED
  digitalWrite(pin2, LOW);   
  digitalWrite(pin3, LOW);   
  digitalWrite(pin4, LOW);   
  delay(speed);              
  digitalWrite(pin1, LOW);   // turn on the second LED
  digitalWrite(pin2, HIGH);   
  digitalWrite(pin3, LOW);   
  digitalWrite(pin4, LOW);   
  delay(speed);              
  digitalWrite(pin1, LOW);   // turn on the third LED
  digitalWrite(pin2, LOW);   
  digitalWrite(pin3, HIGH);   
  digitalWrite(pin4, LOW);   
  delay(speed);              
  digitalWrite(pin1, LOW);   // turn on the fourth LED
  digitalWrite(pin2, LOW);   
  digitalWrite(pin3, LOW);   
  digitalWrite(pin4, HIGH);   
  delay(speed);              
  digitalWrite(pin1, LOW);   // and turn them all off
  digitalWrite(pin2, LOW);   
  digitalWrite(pin3, LOW);   
  digitalWrite(pin4, LOW);   
 
  delay(speed2);

and the second code block contains pretty much duplicated code but with the lit LED rotating in the opposite direction.

And yay – ugly but it worked!
See the full source code for this test here: http://topdownview.com/arduino/glitch/Glitch_first_test

Comments are closed to reduce the spam. If you'd like to add something, please use the contact form to let me know and I'll reopen comments for you. Thanks