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?
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.).