In Espruino there is no notion of a loop such as in Arduino and a-like. Espruino initializes and runs through your code you upload and then to sleep / idle. Take a look at the various code examples in the tutorials, and forums, reference items, etc. What you see is setups of events and related callbacks.
Espruino gets really active only when some hardware or timed software event occurs (except you code - by mistake - an infinite loop and prevents it from going sleep/idle).
For a scenario, assume Espruino should do something every time after your pressed a button (release a momentary on push button), let's say toggle another pin to which an LED is connected. Assume LED is connected that it on when the pin is high (on).
For the example we will use Esprino's conveniently on-board user BTN button, which is connected to pin B12 and Ground. (BTN - and also BTN1 - are just other 'names' / symbols / references of pin B12). For the LED we use one of the even so conveniently on-board red, green, and blue LEDs - LED1..LED3 - connected to pins A13..A15 and Ground. (Again, LED1..LED3 are just other references for pins A13..A15).
The code for that looks like this:
// toggle blue on-board LED LED3 / pin !5
var toggleLED = function() {
if (digitalRead(LED3) === 1) {
LED3.reset();
} else {
LED3.set();
}
};
// watch pin BTN / B12 pin and for every rising edge toggle the LED
setWatch(toggleLED, BTN,
{ repeat:true, edge:'rising', debounce:10 });
To make things a bit more interesting, we say that the LED should toggle anyway every 5 seconds. This would then be a software event (driven of course by a hardware timer event ). To achieve this we add the following snipped to our code:
// set an interval / timer to toggle the LED every 5 seconds
var iv = setInterval(toggleLED,3000);
Of course, you understand that the button release and interval events happen independent but the effect on the LED is the same.
Based on this example you can create a reaction time tester or meter:
Every time the LED comes on, the user has to press the button, and the code measures the time between the LED comes on and the user presses the button.
Further modifications can be made that a randomized time passes after the button has been released for the LED to go on again...
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.
In Espruino there is no notion of a loop such as in Arduino and a-like. Espruino initializes and runs through your code you upload and then to sleep / idle. Take a look at the various code examples in the tutorials, and forums, reference items, etc. What you see is setups of events and related callbacks.
Espruino gets really active only when some hardware or timed software event occurs (except you code - by mistake - an infinite loop and prevents it from going sleep/idle).
For a scenario, assume Espruino should do something every time after your pressed a button (release a momentary on push button), let's say toggle another pin to which an LED is connected. Assume LED is connected that it on when the pin is high (on).
For the example we will use Esprino's conveniently on-board user BTN button, which is connected to pin B12 and Ground. (BTN - and also BTN1 - are just other 'names' / symbols / references of pin B12). For the LED we use one of the even so conveniently on-board red, green, and blue LEDs - LED1..LED3 - connected to pins A13..A15 and Ground. (Again, LED1..LED3 are just other references for pins A13..A15).
The code for that looks like this:
To make things a bit more interesting, we say that the LED should toggle anyway every 5 seconds. This would then be a software event (driven of course by a hardware timer event ). To achieve this we add the following snipped to our code:
Of course, you understand that the button release and interval events happen independent but the effect on the LED is the same.
Based on this example you can create a reaction time tester or meter:
Every time the LED comes on, the user has to press the button, and the code measures the time between the LED comes on and the user presses the button.
Further modifications can be made that a randomized time passes after the button has been released for the LED to go on again...
For more uses of events and timers see Software Buttons - Many buttons from just one hardware button. The software button uses dynamically created single trigger rising and falling edge events and timeouts.
Any other device sets up events with callbacks - that a key beauty of Espruino and its secret of extreme power saving.