Help in Arduino code! [SOLVED]

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

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

why did you post in "Suggestions for the Arduino Project" ?

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.

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

yes !
Let me correct it

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

Also read about scope, your rew variable is local to the loop and not static.

This is also untested.

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

got it

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;

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".

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

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

1 Like

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

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

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

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction


++loopCounter is incrementing the loopCounter variable

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