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