The problem is that when you change the variable a interval uses the interval doesn't actually change, to change it you have to assign the interval to a variable and then use the function changeInterval. So to fix your code you could either use changeInterval or use could make the function call itself with timeouts instead of intervals (Timeouts call once after the time then get deleted).
With Timeouts:
var c = 1;
var time = 15;
function flash(){
on = !on;
digitalWrite(A0, on);
c++;
if(c >= 20){
digitalWrite(LED1, 1);
}
if(c >= 40){
digitalWrite(LED2, 1);
}
if(c >= 60){
digitalWrite(LED3, 1);
}
if(c >= 80){
c = 0;
digitalWrite([LED1, LED2, LED3],0);
time += 25;
}
setTimeout(flash,time);
}
flash();
Or with changeInterval:
var c = 1;
var time = 15;
function flash(){
on = !on;
digitalWrite(A0, on);
c++;
if(c >= 20){
digitalWrite(LED1, 1);
}
if(c >= 40){
digitalWrite(LED2, 1);
}
if(c >= 60){
digitalWrite(LED3, 1);
}
if(c >= 80){
c = 0;
digitalWrite([LED1, LED2, LED3],0);
time += 25;
changeInterval(loop, time);
}
}
var loop = setInterval(flash,time);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
The problem is that when you change the variable a interval uses the interval doesn't actually change, to change it you have to assign the interval to a variable and then use the function changeInterval. So to fix your code you could either use changeInterval or use could make the function call itself with timeouts instead of intervals (Timeouts call once after the time then get deleted).
With Timeouts:
Or with changeInterval: