DIO
Timer
Interrupt
Introducing the concept of blocking in programming and its implications.
This code controls the state of two LEDs. The red LED toggles every 2 seconds, # and the blue LED's state is controlled by the button's state.
Β
ζ€η¨εΌη’Όζ§εΆε ©ε LED ηηζ γη΄ θ² LED ζ― 2 η§εζδΈζ¬‘οΌθθθ² LED ηηζ η±ζιηηζ ζ§εΆγ
time.sleep()
, Blue LED does not turn on immediately.from machine import Timer
timer0 = Timer(0)
timer0.init(period=2000, callback=lambda t:print(0))
Timer.init() method:
Used to configure the timer with specific parameters.
Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
from machine import Timer
timer0 = Timer(0)
timer0.init(mode=Timer.ONE_SHOT, period=2000, callback=lambda t:print(0))
timer1 = Timer(1)
timer1.init(mode=Timer.PERIODIC, period=1000, callback=lambda t:print(1))
Definition of Callbacks: Functions that are called when a timer expires.
from machine import Pin, Timer
timer = Timer(0)
led = Pin(21, Pin.OUT)
def toggle_led(timer):
led.value(not led.value())
timer.init( period=1000, mode=Timer.PERIODIC,
callback = toggle_led )
Stops the timer, and removes the configuration.
from machine import Timer
import time
# Initialize the counter and timer
counter = 0
timer = Timer(0)
def on_timer(timer):
global counter
print(f"Timer Triggered! {counter}")
counter += 1 # Increment the counter each time the timer is triggered
# Initialize the timer
timer.init(period=2000, mode=Timer.PERIODIC, callback=on_timer)
while True:
# Check the counter in the main loop
if counter == 5:
print(f"Deinitializing Timer... {counter}")
timer.deinit()
counter += 1
if counter > 5:
print(f"Main loop in while is ruuning... {counter}")
counter += 1
time.sleep(1) # Sleep for a while before checking the counter again
from machine import Timer
import time
timer = Timer(0)
period = 1000 # 1 second
last_time = time.ticks_ms()
def on_timer(timer):
global period, last_time
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time)
print("Timer Triggered! Time Difference: {} ms".format(time_diff))
period += 1000 # Increase period by 1 second
timer.init(period=period, mode=Timer.ONE_SHOT, callback=on_timer)
last_time = current_time # Update the last_time to the current_time
timer.init(period=period, mode=Timer.ONE_SHOT, callback=on_timer)
from machine import Timer
import time
# Initialize multiple timers
timer1 = Timer(0)
timer2 = Timer(1)
# Initialize last time for each timer
last_time1 = time.ticks_ms()
last_time2 = time.ticks_ms()
def on_timer1(timer):
global last_time1
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time1)
print("Timer 1 Triggered! Time Difference: {} ms".format(time_diff))
last_time1 = current_time # Update the last_time1 to the current_time
def on_timer2(timer):
global last_time2
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time2)
print("Timer 2 Triggered! Time Difference: {} ms".format(time_diff))
last_time2 = current_time # Update the last_time2 to the current_time
# Initialize timers with different periods and modes
timer1.init(period=1000, mode=Timer.PERIODIC, callback=on_timer1)
timer2.init(period=2000, mode=Timer.PERIODIC, callback=on_timer2)
from machine import Pin, Timer
# Define a list to hold the color and pin number tuples
color_and_pin_list = [('red', 23), ('yellow', 21), ('green', 4)]
# Use a dictionary comprehension to create the 'leds' dictionary
leds = {color: Pin(pin, Pin.OUT) for color, pin in color_and_pin_list}
# Initialize timer
timer = Timer(0)
def change_light(t):
if leds['red'].value(): # If red is ON
leds['red'].off()
leds['green'].on()
timer.init(period=700, mode=Timer.ONE_SHOT, callback=change_light)
elif leds['green'].value(): # If green is ON
leds['green'].off()
leds['yellow'].on()
timer.init(period=200, mode=Timer.ONE_SHOT, callback=change_light)
else: # If yellow is ON
leds['yellow'].off()
leds['red'].on()
timer.init(period=500, mode=Timer.ONE_SHOT, callback=change_light)
# Initialize the starting state and start the timer
leds['green'].on()
timer.init(period=700, mode=Timer.ONE_SHOT, callback=change_light)
In this example, we are using one timer to control all three lights, changing the callback period to simulate the different light durations.
from machine import Pin
import time
# Initialize LED and Button
led = Pin(21, Pin.OUT) # LED connected to GPIO12
button = Pin(32, Pin.IN, Pin.PULL_UP) # Button connected to GPIO13
led.on()
# Callback function to handle button press
def button_handler(pin):
led.value(not led.value()) # Toggle LED state
# Attach the callback function to button press interrupt
button.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
while True:
time.sleep(0.1) # Sleep to reduce CPU usage
from machine import Timer
tim = Timer(0)
def tick(timer):
print('Tick!', timer)
# Initialize timer interrupt
tim.init(mode=Timer.PERIODIC, period=1000, callback=tick)
# The function 'tick' will be called every 1000ms (1 second)