In this tutorial I'll show you how to blink multiple LEDs at different rates on Arduino-like boards using Zerynth.
Blinking one LED is known as the “Hello World of embedded devices"... but with Zerynth we can make it funnier. We can blink many LEDs at different frequencies using many separated threads in just a few lines of code...
Yes, you've read it right: we can use threads on Arduino-like boards! No more creepy single loop logic code, typical of imperative programming. Zerynth allows pure thread driven implementation!
This can save your mind :)
Surfing the web you can see how to blink multiple LEDs using delay() in the loop() function. This logic it's OK if you want to blink them all at once or if you want to blink one after the other or to do other simple effects.
But what if you want to blink the LEDs at different rates? This question comes up practically every day on the Arduino forum and this is where the delay() function doesn't really help. In fact, in Arduino using delay() has a side effect - the Arduino does nothing for that while. To get two or more "actions" to run independent of each other, you cannot use delay().
To resolve this problem, there are many solutions, more or less difficult:
However, designers and non-programmers users are frustrated by the difficulties in implementing such functionalities in microcontrollers. They just want their prototype and product to work.
In order to solve these pains, Zerynth supports all the most used high-level features of Python like modules, classes, multithreading, callbacks, timers and exceptions, plus some custom hardware-related features like interrupts, PWM, digital I/O, etc.
Zerynth is an easy to use development suite for the high level design of interactive objects ready for the cloud and the IoT. Zerynth is composed of an cross-platform IDE, a mobile App and a Virtual Machine that runs on various platforms. With Zerynth creatives, designers and professionals can develop in Python for Arduino DUE, ST Nucleo and most of Particle products, and similar boards using paradigms and features typical of PC and mobile programming.
- 3 x 1 kΩ resistor
- 3 x low-power LEDs
- 1 x breadboard
- Some jumper wires
- Zerynth Studio. No matter which OS you use, Zerynth runs! Just download the package and install it.
- Arduino DUE or ST Nucleo F401RE or Particle (formerly Spark) Core. No matter which board you use, Zerynth is multi-board compatible!
To build the LEDs circuit, connect one end of the resistor to a digital pin. Connect the long leg of the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short leg of the LED (the negative leg, called the cathode) to the GND.
Using Zerynth is very easy! Here is a nice step-by-step tutorial to getting started.
Once installed Zerynth, you can open the example browser (step 1 in the image) from which Zerynth official examples can be "cloned". Select the example named "Multi Blink" (step 2) and clone it (step 3).
This example shows how to use Zerynth threads for driving three LEDs with asymmetric and different blinking rates. Its code is very simple and has a tons of comments. Just a couple of notes.
Once the script is run, three threads are created (plus the main thread, that is always available), each running a specific instance of the blink function, with different parameters.
Each thread in Zerynth is a sort of separated and parallel process that runs autonomously on your board. A thread requires a function to be executed as input for the definition. The same function can be instanced by various thread giving you the possibility to write very concise and readable code. With threads you can design your algorithm architecture assuming parallelism that is typical of high level. More info about Zerynth Virtual Machine here.
Moreover, thanks to Python argument passing, default values can be defined for function inputs. In this way you can launch threads without specifying all the inputs required by the function, default values will fill the holes. In this case all the parameters following 'blink' are passed to the functions as arguments.
Liked it? Let's go on to the next: clone the example "Sensor driven Multi-Blink"
This example shows how to drive various behaviours taking as input an analog signal acquired through ADC. In particular, the implemented scripts drives three LEDs at three different frequencies calculated on the basis of the acquired analog signal.
If you are still not satisfied, take a look at this project: you will make a simplified theremin-like instrument that changes the pitch played as you wave your hand over an Infrared Proximity Sensor. In addition you can easily vary the length of the "beat" and drive various blinking LEDs (seemingly) all at the same time... it's the magic of multi-threading!
Of course you can do it also with Arduino (C/C++)... but please, try doing it :)
Luigi F. Cerfeda (@L_F_Cerfeda) - Zerynth team