You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • For a gradual introduction, you may go the following path:

    Setup to start out:

    • Connect to Espruino
    • Open settings (click ongear)
    • In Settings - General, CHECK Overwrite JavaScript with Graphical Editor
    • In Settings - Communication, UNCHECK Save on Send
    • Close settings
    • Switch to Graphical Editor

    Let's get started with graphical programming!

    A) Create program according to attachment 1: Simple Blink using Interval and Timeout

    Compose the program and upload it.

    The red LED (LED1) will turn on every 2 seconds, the first time 2 seconds after upload - this uses the interval construct. After 1 second of having it turned on the red LED (LED1) will turn off - this uses the timeout construct.

    Unfortunately, on power fail, or plugging Espruino out and in again, the blinking will be gone. Espruino has the ability to store the program and run independently of the development environment. As soon it is powered on, it will start the program.

    Let's make the changes in the code and the way the code is handled on Espruino on upload for this 'fix'.

    B) Enhance the program according to attachment 2: Simple Blink using Interval and Timeout in onInit

    Enhance the program by pulling in from Functions the block to do something (without returning something) and name it to to onInit. Then drag-drop the existing every 2 seconds block (with all its content) into the content space of the to onInit function block.

    Before you upload, go to Settings - Communication, and CHECK Save on Send. Now upload. You notice in the console the saving of 209 bytes and blinking begins.

    Now you can disconnect, unplug, and re-plug-in Espruino, and blinking begins right away. The function onInit is automatically looked for and invoked by Espruino on power up.

    Note: starting activities within onInit function is a so called best practice(s).

    Now we go a step further and want to achieve the blinking just with interval construct and the information if the LED was on or off.

    Let's make this change.

    C) Modify the program according attachment 3: Simple Blink using only Timeout and State in Blink separated from onInit

    Modify the program to separate the onInit from the blink and use the interval construct every 1 seconds and the state variable (in global space) that holds on to on and off state of the LED with the values true and false, respective. We can use state directly in the switching the LED pin to on and off matching true and false.

    After upload, we notice that the red LED comes on already after 1 second...

    It is also a very good practice to separate the onInit from the rest of the functionality / functions.

    Now we can show that using interval (and timeout) construct(s), Espruino is most of the time sleeping! ...waiting for the times to elapse to flip the LED, and waiting for other business...

    Therefore, lets add this feature. ...with the information provided in the next post.


    3 Attachments

    • A Simple Blink using Interval and Timeout.png
    • B Simple Blink using Interval and Timout in onInit.png
    • C Simple Blink using only Timeout and State in Blink separated from onInit.png
About

Avatar for allObjects @allObjects started