-
• #2
Hi,
Rather than polling, this is where you want to use callback functions...
You can call a function that will handle your serial data when it is received, for example:
Serial1.on("data",function(data) { buffer += data; var i = buffer.indexOf("\r"); while (i>=0) { dataReceived(buffer.substr(0,i)); // <------- buffer = buffer.substr(i+1); i = buffer.indexOf("\r"); comState = 2; } }); function dataReceived(str) { var a = Number(str); if(a==12.3) {LED1.write(1);} else {LED1.write(0);} if(a==23.4) {LED2.write(1);} else {LED2.write(0);} if(a==34.5) {LED3.write(1);} else {LED3.write(0);} } function getTemp(arg){ Serial1.print("NTC,"+arg+"\r"); }; setInterval(getTemp, 1000); // ask for temperature every second
It'll be much more power efficient, since the board is only waking up when it has something to do. Generally you shouldn't ever really need a
loop
function in Espruino.Hope that helps! In the code above, it'll ask for temperature every 1 second regardless of whether there was a response - but often this is a good idea. If the temperature sensor doesn't respond (or doesn't get the data) for whatever reason, you don't want the Espruino board to stay waiting for it!
-
• #3
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 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: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'.
-
• #4
Thank you for responses
Hi,
Espruino is connected to second board over Serial1 and it sends some querys to get information from second board. After the query sent, javascript code must go idle state for receive response process. if the js code waits in a while loop for response after query, the Serial1.on("data.. event never processing. Is there any way to get response after query on single function ? I used a code similar to state machine for a solution.
Some object oriented languages uses like "doEvents", "processMessages" statements for execution event codes in busy state. Any suggestion for single function ?
Thank you
1 Attachment