I moved your topic to an appropriate forum category.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
const int ledPin = 3;
enum {LED_ON, LED_OFF} state = LED_OFF;
int loopCounter = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.print("Loop #"); Serial.println(loopCounter+1);
switch (state) {
case LED_ON:
if (++loopCounter >= 5) {
Serial.println(F("Turning Led Off"));
digitalWrite(ledPin, LOW);
state = LED_OFF;
loopCounter = 0;
}
break;
case LED_OFF:
if (++loopCounter >= 5) {
Serial.println(F("Turning Led On"));
digitalWrite(ledPin, HIGH);
state = LED_ON;
loopCounter = 0;
}
break;
}
// so that we can see what's going on
delay(100);
}
loopCounter Doesn't it always turn 0 ?
By the output:
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
Turning Led On
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
Turning Led Off
Loop #1
Loop #2
Is it same as using delay(500) 5 times ?
And i still don't get the part of Switch -case ? Like how is it repeating every 5 times (ie. turn off and on state for 5 loops) because of these
...
if (++loopCounter >= 5)
...
loopCounter = 0;
...