delay() is a scourge upon Arduino, and a plague that we are largely free from over here in Espruino-land. delay() is horribly overused on Arduino, and it's only simpler for very basic cases - as soon as you start making things more complicated, you end up in a mess.
We have modern, sensible tools for non-blocking delays - setInterval(), setTimeout(), and setWatch() (for reacting to a pin change).
All of those tasks on Arduino would either block, or require a "blinkWithoutDelay" type approach
unsigned long lastBlink=0;
unsigned long turnOnLedAt=30000;
byte led1st=0;
byte led2st=0;
[#define](https://forum.espruino.com/search/?q=%23define) ledpin 13
[#define](https://forum.espruino.com/search/?q=%23define) ledpin2 14 //note that there's no led connected here on normal arduino board
void loop() {
if (millis()+500 > lastBlink) {
led1st=!led1st;
digitalWrite(ledpin,led1st);
}
if (millis>turnOnLedAt && led2st==0) {
led2st=1;
digitalWrite(ledpin2,led2st);
}
}
vs Espruino:
ledpin1 = LED1;
ledpin2 = LED2;
var ledst=0;
function onInit () {
setTimeout("digitalWrite(ledpin2)",30000);
setInterval("ledst=!ledst;digitalWrite(ledpin1,ledst);",500);
}
or
var ledst=0;
ledpin1 = LED1;
ledpin2 = LED2;
function onInit () {
setTimeout(function() {digitalWrite(ledpin2);},30000);
setInterval(function() {ledst=!ledst;digitalWrite(ledpin1,ledst);},500);
}
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.
delay() is a scourge upon Arduino, and a plague that we are largely free from over here in Espruino-land. delay() is horribly overused on Arduino, and it's only simpler for very basic cases - as soon as you start making things more complicated, you end up in a mess.
We have modern, sensible tools for non-blocking delays - setInterval(), setTimeout(), and setWatch() (for reacting to a pin change).
All of those tasks on Arduino would either block, or require a "blinkWithoutDelay" type approach
vs Espruino:
or