Skip to content

实现流水灯控制(可加减速)

流水灯

通过 Wokwi 仿真 micropython on ESP32 开发板,实现流水灯控制,可通过按键来改变流水灯的行为。

python
"""
写一个流水灯,赤橙黄绿,线材对应相应的颜色,采取不同的颜色按钮对应不同的功能,
粉色为加速,蓝色为减速,白色为默认,并且把相应的ID改为对应的按键名。
整体流程就是:主程序是定时器流水灯的闪烁,中断服务是时间的控制,
运行顺序是从头到尾之后,通过定时器的调用来运行不同是函数。
"""
from machine import Pin, Timer

# 定义流水灯
SPEED = 1000  # 控制频率

"""
配置在引脚的触发源处于活动状态时调用的中断处理程序。
如果引脚模式是 Pin.IN 则触发源是引脚上的外部值。
如果引脚模式是 Pin.OUT 则触发源是引脚的输出缓冲器。
如果引脚模式是 Pin.OPEN_DRAIN 那么触发源是状态'0'的输出缓冲器和状态'1'的外部引脚值。
"""
leds = [Pin(i, Pin.OUT) for i in range(4, 8)]  # 创建流水灯列表
timer_s = Timer(0)  # 构建定时器,创建控制速度对象。
i = 0  # 控制灯的循环


def twinkle(t):  # 定义闪烁函数
    global i  # 设置全局变量i, 用于切换LED
    print("SPEED={}".format(SPEED))  # 打印LED闪的速度
    leds[i % 4].value(1 - leds[i % 4].value())  # 切换LED
    i = i + 1


def default(t):  # 定义一个默认速度的函数
    global SPEED  # 设置全局变量SPEED, 用于调节LED闪的速度
    SPEED = 500  # 定义默认速度
    timer_s.init(period=SPEED, mode=Timer.PERIODIC, callback=twinkle)  # 定时器初始化,接收指定的速度周期性的回调闪灯函数。
    # 定时器的调用,每个函数都要有才可以在运行函数时处理定时器


def up(t):  # 定义一个加速的函数
    global SPEED  # 设置全局变量SPEED, 用于调节LED闪的速度
    if SPEED >= 100:  # 设置加速后的速度
        SPEED = SPEED // 2  # 加速的速度
        timer_s.init(period=SPEED, mode=Timer.PERIODIC, callback=twinkle)  # 定时器初始化,接收指定的速度周期性的回调闪灯函数


def down(t):  # 定义一个减速的函数
    global SPEED  # 设置全局变量SPEED, 用于调节LED闪的速度
    if SPEED < 1000:  # 设置减速后的速度
        SPEED = SPEED * 2  # 减速的速度
        timer_s.init(period=SPEED, mode=Timer.PERIODIC, callback=twinkle)  # 定时器初始化,接收指定的速度周期性的回调闪灯函数


"""
# period:周期时间(单位为ms)
# mode:工作模式,有 Timer.ONE_SHOT(执行一次)和 Timer.PERIODIC(周期性执行)两种
# callback:定时器中断的回调函数
"""

btn_default = Pin(13, Pin.IN, Pin.PULL_UP)  # 定义默认按钮,pin 脚模式为输入,上拉
btn_Up = Pin(12, Pin.IN, Pin.PULL_UP)  # 定义加速按钮,pin 脚模式为输入,上拉
btn_down = Pin(14, Pin.IN, Pin.PULL_UP)  # 定义减速按钮,pin 脚模式为输入,上拉

btn_default.irq(trigger=Pin.IRQ_RISING, handler=default)  # 默认按钮的中断,触发模式为上升沿中断,在中断触发时调用默认速度函数
btn_Up.irq(trigger=Pin.IRQ_RISING, handler=up)  # 加速按钮的中断,触发模式为上升沿中断,在中断触发时调用加速函数
btn_down.irq(trigger=Pin.IRQ_RISING, handler=down)  # 减速按钮的中断,触发模式为上升沿中断,在中断触发时调用减速函数

timer_s.init(period=SPEED, mode=Timer.PERIODIC, callback=twinkle)  # 定时器初始化,接收指定的速度周期性的回调闪灯函数
"""
irq 中断回调参数:
# handler 是一个可选的函数,在中断触发时调用
# trigger 配置可以触发中断的事件。可能的值是:
# Pin.IRQ_FALLING 下降沿中断
# Pin.IRQ_RISING 上升沿中断
# Pin.IRQ_RISING_FALLING 上升沿或下降沿中断
# Pin.IRQ_LOW_LEVEL 低电平中断
# Pin.IRQ_HIGH_LEVEL 高电平中断
"""
json
{
  "version": 1,
  "author": "zhengxinonly",
  "editor": "wokwi",
  "parts": [
    {
      "type": "board-esp32-s3-devkitc-1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": { "env": "micropython-20231227-v1.22.0", "flashSize": "8" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 0,
      "left": -220,
      "attrs": { "color": "white", "lightColor": "green", "label": "green LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 0,
      "left": -180,
      "attrs": { "color": "white", "lightColor": "yellow", "label": "yellow LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 0,
      "left": -140,
      "attrs": { "color": "white", "lightColor": "blue", "label": "blue LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": 0,
      "left": -100,
      "attrs": { "color": "white", "lightColor": "red", "label": "red LED" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 250,
      "left": -240,
      "attrs": { "color": "red", "label": "Up" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": 250,
      "left": -160,
      "attrs": { "color": "green", "label": "Default" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn3",
      "top": 250,
      "left": -80,
      "attrs": { "color": "grey", "label": "Down" }
    }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "led4:A", "esp:4", "red", [ "v0" ] ],
    [ "led3:A", "esp:5", "blue", [ "v0" ] ],
    [ "led2:A", "esp:6", "yellow", [ "v0" ] ],
    [ "led1:A", "esp:7", "green", [ "v0" ] ],
    [ "led1:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led2:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led3:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led4:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "btn1:2.r", "esp:GND.1", "black", [ "h8", "v0" ] ],
    [ "btn2:2.r", "esp:GND.1", "black", [ "h8", "v0" ] ],
    [ "btn3:2.r", "esp:GND.1", "black", [ "h8", "v0" ] ],
    [ "btn1:1.r", "esp:14", "red", [ "v0" ] ],
    [ "btn2:1.r", "esp:12", "white", [ "v0" ] ]
  ],
  "dependencies": {}
}

跑马灯

python
from machine import Pin # 导入 Pin (开发板中 Pin 代表 GPIO)
from time import sleep_ms # 导入时间

# 定义每个角,脚的状态为输出。
pins = [Pin(p, Pin.OUT) for p in [4,5,6,7]]

def setPins(values):  # 定义每个脚的值,也就是每个灯的值
    for i in range(4):
       pins[i].value(values[i])

def runLed(speed):  # 定义跑马灯函数
    while True:  # 写一个死循环
       setPins([1,0,0,0])  # 一次传给每个LED灯的值,值为1对应的灯亮
       sleep_ms(speed)  # 设置跑马灯的速度
       setPins([0,1,0,0])
       sleep_ms(speed)
       setPins([0,0,1,0])
       sleep_ms(speed)
       setPins([0,0,0,1])
       sleep_ms(speed)

runLed(200)  # 运行跑马灯函数
json
{
  "version": 1,
  "author": "zhengxinonly",
  "editor": "wokwi",
  "parts": [
    {
      "type": "board-esp32-s3-devkitc-1",
      "id": "esp",
      "top": -9.78,
      "left": -43.43,
      "attrs": { "env": "micropython-20231227-v1.22.0", "flashSize": "8" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 0,
      "left": -220,
      "attrs": { "color": "white", "lightColor": "green", "label": "green LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 0,
      "left": -180,
      "attrs": { "color": "white", "lightColor": "yellow", "label": "yellow LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 0,
      "left": -140,
      "attrs": { "color": "white", "lightColor": "blue", "label": "blue LED" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": 0,
      "left": -100,
      "attrs": { "color": "white", "lightColor": "red", "label": "red LED" }
    }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "esp:4", "r1:2", "green", [ "h0" ] ],
    [ "r1:1", "led1:A", "green", [ "v0" ] ],
    [ "led4:A", "esp:4", "red", [ "v0" ] ],
    [ "led3:A", "esp:5", "red", [ "v0" ] ],
    [ "led2:A", "esp:6", "red", [ "v0" ] ],
    [ "led1:A", "esp:7", "red", [ "v0" ] ],
    [ "led1:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led2:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led3:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "led4:C", "esp:GND.1", "black", [ "v0" ] ]
  ],
  "dependencies": {}
}