Hi there - I'm new to Arduino, so I can use a little help with what might seem basic to most of you!
Here's the behavior I'm going for:
- when a switch is turned on, blink an LED 3 times then turn it off after that
- when a switch is turned off, turn and/or keep the LED off.
Here's the code I'm working with. The switch works and the LED blinks, but it blinks continuously and doesn't stop after 3 times. I think there's a problem with my counter. I could use some help as I have been trying everything for hours and my brain is about to explode.
Thank you!
int ledPin = 13; // LED is connected to digital pin 13
int switchPin = 5; // switch connected to digital pin 2
int switchValue; // a variable to keep track of when switch is pressed
int counter = 0; //my counter variable
//int counterValue; //a possible value variable for my counter, but I don't know if I need this, help!
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()
{
switchValue = digitalRead(switchPin); // check to see if the switch is pressed
if (switchValue == LOW) { // if the switch is pressed then,
digitalWrite(ledPin, HIGH); // turn the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off
delay(200); // wait for a second
counter++; //increase the counter value by 1
if (counter > 3) { //if the counter value is greater that 3 then
digitalWrite(ledPin, LOW); // turn the LED off and keep it off (why isn't this working?)
}
}
else { // otherwise,
digitalWrite(ledPin, LOW); // turn the LED off
counter = 0;
}
}