Problems with 'while' and 'for' function

Hello, I’m programming blinkit two leds at 8 hz. I want get 10 cicles of the following scheme (30 s off, 30 s LED1 on, 30 s off, 30 sLED2 on) (I programmed with a pulse botton in pin 7). The problem is that it does not stop at 10 cycles. However if I give shorter intervals (for example: 3 s off, 3 s LED1 on, 3 s off, 3 s LED2 on) IT WORKS!!! (stop at 10 cycles), but I can´t get 10 cycles in my scheme (30 s LEDs on).

Help please! :slight_smile:


int val = 0;
int state = 0;
int old_val = 0;
int reps;
int var;

void setup()
{
pinMode(7,INPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

}

void loop()
{

val = digitalRead(7);
if ((val == HIGH) && (old_val == LOW)){
state=1-state;
}
old_val = val;
if (state==1){
{
var = 0;
while(var < 10) {
delay(30000);
for(reps = 0; reps < 240; reps ++)
{
blinkit12();
}

delay(30000);
for(reps = 0; reps < 240; reps ++)
{
blinkit13();
}
var++;
}

exit(0);
}

}
else{
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}

}

void blinkit12()
{
digitalWrite(12,HIGH);
delay(65);
digitalWrite(12,LOW);
delay(60);
}

void blinkit13()
{
digitalWrite(13,HIGH);
delay(65);
digitalWrite(13,LOW);
delay(60);
}


  1. Use code tags. Read the how-to-use-this-forum post and edit your original post.

  2. Use the auto-format function on your code in Arduino before posting it again. That will help you find many errors.

  3. exit(0); doesn’t belong in Arduino code.

Instead of “exit(0);”, use “state = 0;” to stop at the end of 20 minutes. The next button push should start the cycle again.

Note:

  var = 0;
  while(var < 10) {
// bunch of stuff
  var++;
  }

is harder to read but acts the same as:

  for (var = 0; var < 10; var++)
  {
    // bunch of stuff
  }

Thank you very much!