Problem: LilyPad button function

Hi!

I am a student in Indiana University and our reserach group tries to use LilyPad to build up some prototype.

We successufly got connected and uploaded the example/digital/blink code. It worked perfectly fine with various delay. However, when we tried to test the example/digital/button sample, it did not work well.

The LED was connected to 13, and button was connected to 2. We upload the code successfully, but the LED didn't turn on or off with button. Worse, the LED Blink irregularly.

We added some extra code to read from serial monitor the button inpupin votage status. It was not steady, jumping crazily between low and high even with no button actually connected.

Anybody know how to deal with this situation? Is this problem related to electricity grounding? Or we got a defect LilyPad?

Well, if you had included your code perhaps we could have commented on it. ::slight_smile:

I used the code from example.

Here is the code:


/*

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}

void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}


Thank you!!

Is your button wired as:

pin2 - button - gnd

If so, change setup to:

void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
 digitalWrite(inputPin,HIGH);  //default pin state to HIGH
}

Might do the trick...