• 物联网开发笔记(16)- 使用Wokwi仿真micropython on ESP32开发板实现跑马灯(4个LED)


    今天我们再来分享一个流水灯,也叫跑马灯哈!

    一、目的

            使用Wokwi仿真micropython on ESP32开发板实现跑马灯(4个LED)

    二、环境

    仿真网站Wokwi,语言MicroPython,开发板ESP32

    三、代码解析

    main.py:

    1. from machine import Pin
    2. import machine
    3. import time
    4. # 定义每个角,脚的状态为输出。
    5. pins = [machine.Pin(p, machine.Pin.OUT) for p in [32,33,25,26]]
    6. def setPins(values): # 定义每个脚的值,也就是每个灯的值
    7. for i in range(4):
    8. pins[i].value(values[i])
    9. def runLed(speed): # 定义跑马灯函数
    10. while True: # 写一个死循环
    11. setPins([1,0,0,0]) # 一次传给每个LED灯的值,值为1对应的灯亮
    12. time.sleep_ms(speed) # 设置跑马灯的速度
    13. setPins([0,1,0,0])
    14. time.sleep_ms(speed)
    15. setPins([0,0,1,0])
    16. time.sleep_ms(speed)
    17. setPins([0,0,0,1])
    18. time.sleep_ms(speed)
    19. runLed(200) # 运行跑马灯函数

    diagram.json:

    1. {
    2. "version": 1,
    3. "author": "Anonymous maker",
    4. "editor": "wokwi",
    5. "parts": [
    6. {
    7. "type": "wokwi-esp32-devkit-v1",
    8. "id": "esp",
    9. "top": 0,
    10. "left": 0,
    11. "attrs": { "env": "micropython-20220618-v1.19.1" }
    12. },
    13. {
    14. "type": "wokwi-led",
    15. "id": "led1",
    16. "top": -65.43332926432292,
    17. "left": -185.83333333333334,
    18. "attrs": { "color": "red" }
    19. },
    20. {
    21. "type": "wokwi-led",
    22. "id": "led2",
    23. "top": -64.43,
    24. "left": -151.5,
    25. "attrs": { "color": "green" }
    26. },
    27. {
    28. "type": "wokwi-led",
    29. "id": "led3",
    30. "top": -65.44,
    31. "left": -117.84,
    32. "attrs": { "color": "blue" }
    33. },
    34. {
    35. "type": "wokwi-led",
    36. "id": "led4",
    37. "top": -63.76,
    38. "left": -80.17,
    39. "attrs": { "color": "yellow" }
    40. }
    41. ],
    42. "connections": [
    43. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    44. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    45. [ "led4:A", "esp:D32", "gold", [ "v0" ] ],
    46. [ "led3:A", "esp:D33", "blue", [ "v0" ] ],
    47. [ "led2:A", "esp:D25", "green", [ "v0" ] ],
    48. [ "led1:A", "esp:D26", "red", [ "v0" ] ],
    49. [ "led1:C", "esp:GND.2", "black", [ "v0" ] ],
    50. [ "led4:C", "esp:GND.2", "green", [ "v0" ] ],
    51. [ "led3:C", "esp:GND.2", "green", [ "v0" ] ],
    52. [ "led2:C", "esp:GND.2", "green", [ "v0" ] ]
    53. ]
    54. }

    实现效果:

    好了。大家也可以点击下方链接查看具体代码和实效:

    Wokwi Arduino and ESP32 Simulatoricon-default.png?t=M85Bhttps://wokwi.com/projects/343093459991659090

     如果你想要实现8个,12个LED的闪烁效果,只需把列表加长,即可实现,快动手试试吧!欢迎评论区留言,谢谢!

     

  • 相关阅读:
    Pytorch实战教程(二十七)-基于ResNet模型实现猫狗分类
    Freeswitch使用originate转dialplan
    车路协同云控平台建设实践
    Python 编程语言的介绍
    Java:阻塞队列BlockingQueue与应用场景
    美团二面:SpringBoot读取配置优先级顺序是什么?
    JVM运行时数据区域详解
    云vscode搭建--使用容器化部署
    25.中间件
    Python笔记 之 wmi模块
  • 原文地址:https://blog.csdn.net/zhusongziye/article/details/126913511