Luigi Francesco Cerfeda
Published © GPL3+

Multiple Blinking LEDs at different rates (with threads!)

How to blink multiple LEDs at different rates on Arduino-like boards using threads in just a few lines of code... the magic of Zerynth!

BeginnerFull instructions provided1 hour12,700
Multiple Blinking LEDs at different rates (with threads!)

Things used in this project

Hardware components

Resistor 1k ohm
Resistor 1k ohm
×3
low-power LEDs
×3
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Photon
Particle Photon
×1

Software apps and online services

Zerynth Studio
Zerynth Studio

Story

Read more

Schematics

Multiple Blinking LEDS with Zerynth

Code

Multiple Blinking LEDs at different rates using Zerynth

Python
# Initialize the digital pins where the LEDs are connected as output
pinMode(D2,OUTPUT)
pinMode(D3,OUTPUT)
pinMode(D4,OUTPUT)

# Define the 'blink' function to be used by the threads
# timeON and timeOFF are optional parameters, used as default if not specified when you call the function
def blink(pin,timeON=100,timeOFF=100):                                          
    while True:
        digitalWrite(pin,HIGH)   # turn the LED ON by making the voltage HIGH
        sleep(timeON)            # wait for timeON 
        digitalWrite(pin,LOW)    # turn the LED OFF by making the voltage LOW
        sleep(timeOFF)           # wait for timeOFF 

# Create three threads that execute instances of the 'blink' function. 
thread(blink,D2)               # D2 is ON for 100 ms and OFF for 100 ms, the default values of timeON an timeOFF
thread(blink,D3,200)         # D3 is ON for 200 ms and OFF for 100 ms, the default value of timeOFF
thread(blink,D4,500,200)   # D4 is ON for 500 ms and OFF for 200 ms

Credits

Luigi Francesco Cerfeda

Luigi Francesco Cerfeda

5 projects • 76 followers
Yet another Tony Stark wannabe

Comments