Add LED to timer example

master
Valentin Ochs 2019-11-25 18:25:01 +01:00
parent 2ec4a2a08a
commit 031759ff7c
1 changed files with 4 additions and 1 deletions

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
``` python
from machine import Timer
from machine import Timer, Pin
import utime
# Create a virtual timer with ID -1.
@ -160,10 +160,13 @@ timer = Timer(-1)
data = [0] * 32
index = 0
led = Pin(5, Pin.OUT)
def callback(timer):
global data, index
data[index] = utime.ticks_ms
index = (index + 1) % 32
led(!led())
# Run the callback once, after 100 ms
timer.init(mode = Timer.ONE_SHOT, period = 100, callback = callback)