Pushbutton as On/Off Not Working!

This is so simple, and it should work, but won't! I am so frustrated that I want to puke! I have no clue wheather it is hardware or software, but the hardware looks fine to me... Basically what this is supposed to do is when you press the button (momentarily), the led turns on. When you press the button again, the led turns off. Rinse and repeat. My led just stays on...

int ledPin = 13;
int buttonPin = 12;
int counter;
int buttonstate;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT);
 digitalWrite(buttonPin, HIGH);
}

void loop() {
 buttonstate = digitalRead(buttonPin);
 
 while(buttonstate == LOW){
  counter + 1;
 }
 if (counter == 2){
  counter = 0;
 }
 if (counter == 0){
  digitalWrite(ledPin, HIGH);
 }
 else if (counter == 1){
  digitalWrite(ledPin, LOW);
 }
}

I haven't looked at your code yet, but could you outline how the hardware is connected? Even though it may look right, looks can be deceiving (even more so after hours of looking at it).

Edit:

Looked at your code, and this part doesn't really do anything...

 while(buttonstate == LOW){
  counter + 1;
 }

You don't assign the result of the counter+1 expression to anything.

counter = counter + 1;

or more succinctly

counter += 1;

or even shorter

counter++;

Edit 2:

Running your code through my own internal CPU, I have a feeling it won't do what you are expecting even if you fix the problem I mentioned. When will counter ever be zero by the time it gets to the if counter == 0 section?

Yes check your wiring. However there is one part of your sketch that could get you in trouble if you have any kind of switch contact bounce:

if (counter == 2){
counter = 0;

Change this to:

if (counter >= 2){
counter = 0;

Lefty

Ok, It works, but I had to change the while to if. I am also having some sort of bouncing... If you press the button for too short or long, the light will stay on (Or off.).

int ledPin = 13;
int buttonPin = 12;
int counter;
int buttonstate;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT);
 digitalWrite(buttonPin, HIGH);
}

void loop() {
 buttonstate = digitalRead(buttonPin);
 
 if(buttonstate == LOW){
  counter ++;
 }  
 if(counter >= 2){
  counter = 0;
 }
 if(counter == 0){
  digitalWrite(ledPin, HIGH);
 }
 else if(counter == 1){
  digitalWrite(ledPin, LOW);
 }
}

Connecting switches discussed at.....

Switch bounce discussed (badly integrated!!!) various places. Perhaps start at....

Thanks, but that isn't on and off... I want:

Press
LED on
Press
LED off

@Richard: That's it... It wotks in theory, not in practice.

It works, but only sometimes. If you hold it for too short or too long, the LED will stay in it's state (On or off.).

int ledPin = 13;
int buttonPin = 12;
int counter;
int buttonstate;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT);
 digitalWrite(buttonPin, HIGH);
}

void loop() {
 buttonstate = digitalRead(buttonPin);

 if(buttonstate == LOW){
  counter ++;
 }
 if(counter >= 2){
  counter = 0;
 }
 if(counter == 0){
  digitalWrite(ledPin, HIGH);
 }
 else if(counter == 1){
  digitalWrite(ledPin, LOW);
 }
}