LilyPad Lesson 3: Sensing Switches [Obviously a novice]

This is my first Arduino. I am going through the lessons, and I am working on Lesson 3, the third problem under #7.

http://lilypadarduino.org/?page_id=216

The code below is supposed to turn an LED on or off each time the button is pressed. The code below will turn the LED on when the button is pressed. When the button is pressed again, it will leave the LED on. The third press will turn the LED off. The fourth press will turn it on. The fifth will leave the LED on, and the sixth will turn it off. 7 - on, 8-off, 9-on, 10-stays on, 11-stays on, 12-off, 13-off, 14-on, 15-on, 16-off...

My code is posted below. Can anyone explain the fundamental error I am making?

Thank you, Joe

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

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
  ledValue = digitalRead(ledPin);

  if (switchValue == LOW) {    // if the switch is pressed then,

    if (ledValue == LOW){          //if the LED is off
      digitalWrite(ledPin, HIGH);    // turn the LED on
    }
    else {                            //if the LED is on
      digitalWrite(ledPin, LOW);      //turn the LED off
    }


  }     

  
}

What part of your sketch makes

joehodler: When the button is pressed again, it will leave the LED on. The third press will turn the LED off. The fourth press will turn it on. The fifth will leave the LED on, and the sixth will turn it off. 7 - on, 8-off, 9-on, 10-stays on, 11-stays on, 12-off, 13-off, 14-on, 15-on, 16-off...

that happen ?

Thanks for your interest Pancake.

I did a little searching of my own, and it looks like the problem is "bouncing". I will need to create a debouncing function to delay the LED change each time the switch is pressed.

This youtube clip covers it, but is a little confusing. http://www.youtube.com/watch?v=_LCCGFSMOr4&list=ECA567CE235D39FA84

I also found this in the article in the reference section. http://www.arduino.cc/en/Tutorial/Switch

I think I understand the theory, but it will take me a minute to figure out how to plug it into my code.

Re-reading, the
“When the button is pressed again, it will leave the LED on. The third press will turn the LED off. The fourth press will turn it on. The fifth will leave the LED on, and the sixth will turn it off. 7 - on, 8-off, 9-on, 10-stays on, 11-stays on, 12-off, 13-off, 14-on, 15-on, 16-off…”
was not a “design objective”.

OK
debounce
note the delay that I placed
(everyone hates Delay).

  if (switchValue == LOW) { 	// if the switch is pressed then,
     delay(100);    // yes, delay, for now   <<<<<<<<<<<<<
    if (ledValue == LOW){          //if the LED is off
      digitalWrite(ledPin, HIGH); 	// turn the LED on
    }
    else
    {  
                          //if the LED is on
      digitalWrite(ledPin, LOW);      //turn the LED off
    }
  } 	 
}