Generally you shouldn't ever really need a loop function in Espruino.
I assume line 8 comState = 2; is of no use anymore (and buffer is initialized with var buffer = ""; at the beginning of the code).
Furthermore, I wonder about the == equals in lines 14 through 16: I assume it was intended to be >=, was it? ...to show with leds in which range the temperature is? With ==, at max only one LED would be on and only on the exact temperature...
Breaking out of or loose from the (Arduino) loop thinking, you can also easily increase the functionality of your device: Put the same event driven approach to work for a 'watch-dog' that monitors the process of getting the temperature within a defined time, for example, 10 seconds. If no reading comes in fro what so ever reason, an alarm is reaised. Lines 11 and following would then look like:
var alarm = null, lit = false, dog = null;
function warn() { // blink all LEDs once a second with 50% duty cycle
dog = null;
alarm = setInterval(function(){
lit = !lit;
digitalWrite(LED1,lit);
digitalWrite(LED2,lit);
digitalWrite(LED3,lit);
}, 500);
}
function watch() { // start and restart the watch dog timer on 10 secs
if (dog) { clearTimeout(dog); dog = null; }
if (alarm) { clearInteval(alarm); alarm = null; }
dog = setTimeout(warn, 10000);
}
function dataReceived(str) {
watch(); // restart the watch dog timer
var a = Number(str);
digitalWrite(LED1, a = >12.3);
digitalWrite(LED2, a = >23.4);
digitalWrite(LED3, a = >34.5);
}
function getTemp(arg){
Serial1.print("NTC,"+arg+"\r");
}
watch(); // (re)start watch dog timer
setInterval(getTemp, 1000); // ask for temperature every second
PS: ...actually, it is NOT THAT wrong to think in a loop... when - for the implementation - including the waiting for the interrupt as the 'closing' part of the loop, like the clasps of a 'code' bracelet.
Write the code as 'the nice part of the bracelet', and close it with the setup of an interrupt / watch as the 'clasps'.
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.
I assume line 8
comState = 2;
is of no use anymore (and buffer is initialized withvar buffer = "";
at the beginning of the code).Furthermore, I wonder about the
==
equals in lines14
through16
: I assume it was intended to be>=
, was it? ...to show with leds in which range the temperature is? With==
, at max only one LED would be on and only on the exact temperature...Breaking out of or loose from the (Arduino) loop thinking, you can also easily increase the functionality of your device: Put the same event driven approach to work for a 'watch-dog' that monitors the process of getting the temperature within a defined time, for example, 10 seconds. If no reading comes in fro what so ever reason, an alarm is reaised. Lines
11
and following would then look like:PS: ...actually, it is NOT THAT wrong to think in a loop... when - for the implementation - including the waiting for the interrupt as the 'closing' part of the loop, like the clasps of a 'code' bracelet.
Write the code as 'the nice part of the bracelet', and close it with the setup of an interrupt / watch as the 'clasps'.