# Help in Arduino code! \[SOLVED\]

**URL:** <https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581>\
**Category:** Programming\
**Created:** [March 8, 2025, 8:56am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581 "2025-03-08T08:56:26Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 8, 2025, 9:45am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/12 "2025-03-08T09:45:44Z")

</div>

> [@HazardsMind](#):
>
> No timing was actually specified other than every 5th loop.

yes. The way I got the requirements was

- LED On for 5 loops cycles
- LED Off for 5 loops cycles

rinse & repeat.

➜ a state machine might be a nice readable way to implement this

Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

```arduino
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);
}

```

---

_[View the full topic](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581)._
