# 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:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![prachi\_er](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/prachi_er/32/1202512_2.png) [@prachi\_er](https://forum.arduino.cc/u/prachi_er)\
**Post date:** [March 8, 2025, 8:56am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/1 "2025-03-08T08:56:26Z")

</div>

I am trying to make a simple arduino circuit which blinks after every 5th loop .The problem is led never turns off.  
code:

```arduino
int led=7;
int i =1;

void setup()
{
    Serial.begin(9600); 
	pinMode(led, OUTPUT);
}

void loop(){
  bool rew=true;
  if (rew == true){
    digitalWrite(led,HIGH);
    rew=false;
  }
  Serial.print(i);
  Serial.println(" -->Loop no ");
  if(1% 5==0)rew=true;
  delay(5000);
  i++;
}

```

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/9/1/291549e0c13e85d0e25f007427a3987b3bfd0046.png)

---

<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:00am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/2 "2025-03-08T09:00:20Z")

</div>

why did you post in "[Suggestions for the Arduino Project](https://forum.arduino.cc/c/community/suggestions-for-the-arduino-project/37)" ?

I moved your topic to an appropriate forum category.  
In the future, please take some time to pick the [forum category](https://forum.arduino.cc/categories) 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](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). The guide contains a lot of other useful information. Please read it.  
Thanks in advance for your cooperation.

---

<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:02am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/3 "2025-03-08T09:02:22Z")

</div>

> [@prachi\_er](#):
>
> ```arduino
> if(1% 5==0)rew=true;
> 
> ```

what is the result of 1 % 5 ? did you mean to type `i` instead of 1 ?

---

<div class="post-metadata">

**Author:** ![prachi\_er](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/prachi_er/32/1202512_2.png) [@prachi\_er](https://forum.arduino.cc/u/prachi_er)\
**Post date:** [March 8, 2025, 9:03am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/4 "2025-03-08T09:03:00Z")

</div>

yes !  
Let me correct it

---

<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:03am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/5 "2025-03-08T09:03:16Z")

</div>

> [@prachi\_er](#):
>
> ```arduino
> bool rew=true;
> if (rew == true){
> digitalWrite(led,HIGH);
> rew=false;
> }
> 
> ```

so the LED goes on right away.... you just set `rew` to true and then check if it's true..

Also read about [scope](https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/), your rew variable is local to the loop and not static.

---

<div class="post-metadata">

**Author:** ![HazardsMind](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hazardsmind/32/215209_2.png) [@HazardsMind](https://forum.arduino.cc/u/HazardsMind)\
**Post date:** [March 8, 2025, 9:04am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/6 "2025-03-08T09:04:39Z")

</div>

> [@prachi\_er](#):
>
> ```arduino
> void loop(){
> // this will blink every 5th loop, but will be very quick too
> digitalWrite(led, ++i % 5);
> }
> 
> ```

This is also untested.

---

<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:06am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/8 "2025-03-08T09:06:19Z")

</div>

if would be better to add the ternary operator and use HIGH and LOW instead of

> [@prachi\_er](#):
>
> ```arduino
> digitalWrite(led, ++i % 5);
> 
> ```

---

<div class="post-metadata">

**Author:** ![prachi\_er](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/prachi_er/32/1202512_2.png) [@prachi\_er](https://forum.arduino.cc/u/prachi_er)\
**Post date:** [March 8, 2025, 9:06am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/9 "2025-03-08T09:06:53Z")

</div>

got it

---

<div class="post-metadata">

**Author:** ![prachi\_er](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/prachi_er/32/1202512_2.png) [@prachi\_er](https://forum.arduino.cc/u/prachi_er)\
**Post date:** [March 8, 2025, 9:10am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/10 "2025-03-08T09:10:01Z")

</div>

```arduino
int led=7;
int i =1;

void setup()
{
    Serial.begin(9600); 
	pinMode(led, OUTPUT);
}

void loop(){
  bool rew=false ;
  if(i% 5==0)rew=true;
  if (rew == true|| i==1){
    digitalWrite(led,HIGH);
    delay(500);
    digitalWrite(led,LOW);
    //rew=false;
  }
  Serial.print(i);
  Serial.println(" -->Loop no ");
  delay(1000);
  i++;
}

```

The code works after the aforementioned changes.  
Thankyou all;

---

<div class="post-metadata">

**Author:** ![HazardsMind](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hazardsmind/32/215209_2.png) [@HazardsMind](https://forum.arduino.cc/u/HazardsMind)\
**Post date:** [March 8, 2025, 9:13am UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/11 "2025-03-08T09:13:23Z")

</div>

maybe for readability but it should work just fine. No timing was actually specified other than every 5th loop.

Perhaps it would be better to mention the example sketch "Blink Without Delay".

---

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

```

---

<div class="post-metadata">

**Author:** ![prachi\_er](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/prachi_er/32/1202512_2.png) [@prachi\_er](https://forum.arduino.cc/u/prachi_er)\
**Post date:** [March 8, 2025, 1:44pm UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/13 "2025-03-08T13:44:09Z")

</div>

Sorry for giving out the wrong specifications.

`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

```arduino
...
if (++loopCounter >= 5) 
...
loopCounter = 0;
...

```

Also Thankyou @HazardsMind and @J-M-L for helping

---

<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, 6:26pm UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/14 "2025-03-08T18:26:34Z")

</div>

> [@prachi\_er](#):
>
> And i still don't get the part of Switch -case

you might benefit from studying state machines. 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)

* * *

`++loopCounter` is incrementing the `loopCounter` variable

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 4, 2025, 6:27pm UTC](https://forum.arduino.cc/t/help-in-arduino-code-solved/1361581/15 "2025-09-04T18:27:15Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
