« jane's One-on-One: prototype I | Main | open source resources - 2009 »

I want to smash my Arduino - Bryce Davidson

So, I have been exploring how to use a button on the lilypad, and so far it has been quite difficult to figure out. I know the button works, I know the power source works, and I that I know what the code needs to be to get the kind of functioning I want with the button switch, but so far it's not happening. Here is the code I am working with. See any problems? I really want this to work. I know it's possible!!!!!

int ledPin = 10;
int switchPin = 5; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the last button state

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT); // Set the switch pin as input

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}


void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(80); // wait for a second

{
val = digitalRead(switchPin); // read input value and store it in val

if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
Serial.println("Button just pressed");
}
else { // the button is -not- pressed...
Serial.println("Button just released");
}
}

buttonState = val; // save the new state in our variable
}


val = digitalRead(switchPin); // read input value and store it in val

if (val != buttonState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
Serial.println("Button just pressed");
} else { // the button is -not- pressed...
Serial.println("Button just released");
}
}

buttonState = val; // save the new state in our variable
}