I need help getting a variable to increase at the end of a void loop.

I'm trying to modify the blink program so then the time in between and duration of the blinks increases after each blink, can someone help me?

void setup()
{
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH);
delay(1000); //I want this to increase by 1 second each loop
digitalWrite(13, LOW);
delay(1000); //I want this to increase by 1 second each loop
}

micah_davis:
I'm trying to modify the blink program so then the time in between and duration of the blinks increases after each blink, can someone help me?

Not familiar with the blink program, but do you realize how many times per second your variable may be increasing?
Paul

I want it to the time in between blinks to increase by 1 second every loop, so if the blink is 1 second long, then when it loops, I want the blink to be 2 seconds long.

Post your best effort, in code tags please.

This is what I tried, but it didn't work

void setup()
{
pinMode(13, OUTPUT);
int x = 1000;
}

void loop()
{
digitalWrite(13, HIGH);
delay(x); //I want this to increase by 1 second each loop
digitalWrite(13, LOW);
delay(x); //I want this to increase by 1 second each loop
int x = x + 10000;
}

it gave me an error saying
In function 'void loop()':
16:9: error: 'x' was not declared in this scope
exit status 1

The variable needs to be global so the int x=1000 should be above the void setup(). I.e. not inside any function. And when you use it in loop() there should be no 'int'. That would create another different variable also called x.

Steve

It ran the program, but the time didn't change any.

Then you didn't take the int off the int x=x+1000.

Steve

ok it works now, thank you so much for the help.

The next thing is to learn about millis() - to eliminate the delay() calls.

With that, your program can do something useful while the blink is ‘waiting’...
e.g. read a pushbutton or generate a ‘tick’ sound.

Maybe even something substantial...
The actual blinking is the lowest importance event you will need to look after.
(In a true multi-tasking program, that wouldn’t even be part of the ‘program’. The blinking would be performed in the ‘background’ as a low priority requirement.)

After sorting millis() timing, you’ll be 80% toward understanding ‘non-blocking’ code methods.

The delay() argument is an unsigned long. Not an int.

Another way to do it, since 'x' doesn't need to exist outside loop() to be initialized.

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  static unsigned long x = 1000;  // 'static' means 'save the value between calls'
  digitalWrite(LED_BUILTIN, HIGH);
  delay(x); // I want this to increase by 1 second each loop
  digitalWrite(LED_BUILTIN, LOW);
  delay(x); // I want this to increase by 1 second each loop
  x += 1000;  // Increase by 1 second each loop
}

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