Add LED to timer example

This commit is contained in:
Lynn Ochs 2019-11-25 18:25:01 +01:00
parent 2ec4a2a08a
commit 031759ff7c

View File

@ -150,7 +150,7 @@ MicroPython on the ESP32 has virtual timers and 4 hardware timers. You can use a
Timers execute a callback, which takes the timer as an argument Timers execute a callback, which takes the timer as an argument
``` python ``` python
from machine import Timer from machine import Timer, Pin
import utime import utime
# Create a virtual timer with ID -1. # Create a virtual timer with ID -1.
@ -160,10 +160,13 @@ timer = Timer(-1)
data = [0] * 32 data = [0] * 32
index = 0 index = 0
led = Pin(5, Pin.OUT)
def callback(timer): def callback(timer):
global data, index global data, index
data[index] = utime.ticks_ms data[index] = utime.ticks_ms
index = (index + 1) % 32 index = (index + 1) % 32
led(!led())
# Run the callback once, after 100 ms # Run the callback once, after 100 ms
timer.init(mode = Timer.ONE_SHOT, period = 100, callback = callback) timer.init(mode = Timer.ONE_SHOT, period = 100, callback = callback)