ESP32(MicroPython) WS2812 RGB流水灯 新版

news2024/10/7 14:25:23

ESP32(MicroPython) RGB流水灯 新版

 本程序相比上一个程序,改用了24灯的环形WS2812模块,数据引脚改用13脚。增加了把相应颜色重复2次(即前半部分和后半部分的灯颜色排列相同)和4次的模式,模式增加到9种。

代码如下

#导入Pin模块
from machine import Pin
import time
from machine import PWM
from neopixel import NeoPixel
 
# 五向导航按键,COM引脚接3.3V
key1 = Pin(21, Pin.IN, Pin.PULL_DOWN)
key2 = Pin(19, Pin.IN, Pin.PULL_DOWN)
key3 = Pin(18, Pin.IN, Pin.PULL_DOWN)
key4 = Pin(5, Pin.IN, Pin.PULL_DOWN)
key5 = Pin(17, Pin.IN, Pin.PULL_DOWN)
key6 = Pin(16, Pin.IN, Pin.PULL_DOWN)
 
pin=13
rgb_num=24
rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)

key_en=1
#按键扫描函数
def key_scan():
    global key_en
    if key_en==1 and (key1.value()==1 or key2.value()==1 or key3.value()==1 or key4.value()==1 or
                      key5.value()==1 or key6.value()==1  ):
        time.sleep_ms(10)
        key_en=0
        if key1.value()==1:
            return 1
        elif key2.value()==1:
            return 2
        elif key3.value()==1:
            return 3
        elif key4.value()==1:
            return 4
        elif key5.value()==1:
            return 5
        elif key6.value()==1:
            return 6
    elif (key1.value()==0 and key2.value()==0 and key3.value()==0 and key4.value()==0 and
          key5.value()==0 and key6.value()==0  ) :
        key_en=1
    return 0

brightness=51
delay=40
mode=8
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness<255 :
        brightness+=17
    elif key==2 and brightness>17 :
        brightness-=17
    elif key==3 and delay<90 :
        delay+=10
    elif key==4 and delay>10 :
        delay-=10
    elif key==5 and mode<9 :
        mode+=1
    elif key==6 and mode>0 :
        mode-=1     

count=0
#程序入口
while True:
    key_get()
    if count==23 :
        count=0
    if mode==0 : #关灯
        for i in range(rgb_num):
            rgb_led[i]=(0, 0, 0)
            rgb_led.write()
    if mode==1 : #三原色
        temp=0
        i=count
        count+=1
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==2 : #三原色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==3 : #三原色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)        
    if mode==4 : #三间色
        temp=0
        i=count
        count+=1
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<8 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==5 : #三间色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<2 :
            temp=0
            repeat+=1
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp<4 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==6 : #三间色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)     
    if mode==7 : #三原色+三间色
        temp=0
        i=count
        count+=1
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==8 : #三原色+三间色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<2 :
            temp=0
            repeat+=1
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)           
    if mode==9 : #三原色+三间色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            repeat+=1
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay) 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/692200.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

关于element-ui form表单必填项已经选入值并回填了,但是还会报错必填提示

哈喽 大家好啊 今天用el-form表单的时候 发现明明已经选入值并回填了 发现还是会触发必填报错 如图所示&#xff1a; 因为我这里是点击后右边是一个select选项帮助 然后点击确认后回填 经过发现&#xff0c;是因为rule表单触发校验规则 receiverName: [{ required: true, t…

HiveSQL之datediff、date_add、date_sub详解及注意坑点

文章目录 datediff介绍&#xff1a;示例1&#xff1a;正常情况示例2&#xff1a;负值情况注意&#xff1a;使用场景示例总结 date_add介绍&#xff1a; date_sub介绍&#xff1a; 注意&#xff1a; datediff 介绍&#xff1a; datediff语法: datediff(string enddate,string …

2023上半年Java高频面试题库总结(600+java面试真题含答案解析)

不知什么时候起&#xff0c;互联网行业掀起一股寒冬之风&#xff0c;到处都给人一种岌岌可危的寒颤之感&#xff0c;总觉得是要见不到明日的太阳一般&#xff0c;细细想来&#xff0c;最近的行业内的各种状况确实让不少人有此担忧。 从我认识的好几个程序员口中了解到&#xff…

8.0、Java_IO流 - 如何利用缓冲区提高读写效率 ?

8.0、Java_IO流 - 如何利用缓冲区提高读写效率 &#xff1f; 简单介绍&#xff1a; FileInputStream 通过字节的方式读取文件&#xff0c;适合读取所有类型的文件&#xff08;图像、视频、文本文件等&#xff09;&#xff1b;Java 也提供了 FileReader 字符流 专门读取文本文件…

linux系统之lvm方式挂载磁盘

目录 一、简介二、创建LVM三、删除 一、简介 LVM&#xff1a;逻辑卷管理(Logical Volume Manager) 它是Linux环境下对磁盘分区进行管理的一种机制。LVM是建立在硬盘和分区之上的一个逻辑层&#xff0c;来提高磁盘分区管理的灵活性。它由ibm公司提出。目的&#xff1a;在原始设…

简单认识Nginx配置块location及rewrite

文章目录 一、location配置块1、分类2、location 常用的匹配规则3、location 优先级&#xff1a;4.location 匹配流程5、location实际使用规则1、直接匹配网站根目录首页2、处理静态文件请求3、通用规则 二、rewrite配置块1、简介2、rewrite跳转实现3、rewrite 执行顺序4.rewri…

数据结构——归并排序和计数排序的介绍

文章目录 归并排序归并排序的思想单趟排序的实现归并排序实现非递归版本的实现特性总结 计数排序计数排序的思想计数排序的实现特性总结 归并排序 归并排序&#xff08;MERGE-SORT&#xff09;是建立在归并操作上的一种有效的排序算法,该算法是采用分治&#xff08;Divide and…

【Java】Map和Set

目录 一、搜索树 1、概念 2、操作-查找 3、操作-插入 4、操作-删除&#xff08;难点&#xff09; 6、性能分析 二、搜索 1、概念及场景 2、模型 三、Map 的使用 1、关于Map的说明 2、关于Map.Entry的说明,> 3、Map 的常用方法说明 4、TreeMap的使用案例 四、…

Django之ORM

一、Django模型层之ORM介绍 使用Django框架开发web应用的过程中&#xff0c;不可避免地会涉及到数据的管理操作&#xff08;增、删、改、查&#xff09;&#xff0c;而一旦谈到数据的管理操作&#xff0c;就需要用到数据库管理软件&#xff0c;例如mysql、oracle、Microsoft S…

护航行业安全!安全狗入选2023年度中国数字安全能力图谱(行业版)

近日&#xff0c;数世咨询正式发布了《2023年度中国数字安全能力图谱&#xff08;行业版&#xff09;》。 作为国内云原生安全领导厂商&#xff0c;安全狗也入选多个细项。 厦门服云信息科技有限公司&#xff08;品牌名&#xff1a;安全狗&#xff09;成立于2013年&#xff0c;…

8大service mesh框架大比拼,Istio不是唯一的选择!

文章目录 一、IsitoConsulLinkerdKumaOpen Service MeshMesheryTraefik MeshService Mesh Interface&#xff08;SMI&#xff09; 公众号&#xff1a; MCNU云原生&#xff0c;文章首发地&#xff0c;欢迎微信搜索关注&#xff0c;更多干货&#xff0c;第一时间掌握&#xff01…

Linux bluez蓝牙开发的准备工作

最近为了搞这个蓝牙的事情&#xff0c;忙碌了好几天&#xff0c;我就是想结合 bluez 的代码随便玩一下蓝牙设备&#xff0c;而且能够参考源码写点测试程序来操作这个蓝牙设备。这里只是说明 Linux 下的准备工作而非嵌入式的arm。 1&#xff0c;系统支持 我用的是真机安装的 D…

路由与交换技术(H3C)①——计算机网络基础

系列文章目录 ①——计算机网络基础 路由与交换技术&#xff08;H3C&#xff09;①——计算机网络概述 系列文章目录一 计算机网络概述1.1 计算机网络1.2 计算机网络的基本功能1.2.1 资源共享1.2.2 分布式处理与负载均衡1.2.3 综合信息服务 二 计算机网络的演进2.1 主机互联时…

Unity Editor扩展 实现一个Excel读表窗口

设计 Unity Editor窗口类 public class ExcelEditorWindow : EditorWindow {[MenuItem( "Frameworks/读表配置界面", false, 10 )]private static void Open(){Rect wr new Rect( 0, 0, 500, 500 );ExcelEditorWindow window ( ExcelEditorWindow ) EditorWindow.…

津津乐道设计模式 - 组合模式详解(以餐厅菜单系统举例让你快速掌握)

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

基于Python所写的玛丽冒险设计

点击以下链接获取源码资源&#xff1a; https://download.csdn.net/download/qq_64505944/87953199 《玛丽冒险》程序使用说明 在PyCharm中运行《玛丽冒险》即可进入如图1所示的游戏主界面。 图1 游戏主界面 具体的操作步骤如下&#xff1a; &#xff08;1&#xff09;游戏…

数据库监控与调优【十四】—— COUNT语句优化

COUNT语句优化 有关COUNT的几个实验与结论 准备工作 create table user_test_count (id int primary key not null auto_increment,name varchar(45),age int,email varchar(60),birthday date ) engine innodb;insert into user_test_count (id, name, a…

算法设计与分析之回溯法

文章目录 1. 回溯法简介1.1 DFS的基本思想1.2 回溯法的基本思想1.3 回溯法和DFS的区别1.4 剪枝 2. 01背包问题&#xff1a;子集树2.1 问题介绍2.2 解决思路2.3 算法实现2.4 如何优化 3. 旅行商问题TSP&#xff1a;排序树3.1 问题介绍3.2 解决思路3.3 算法框架3.4 算法实现 4. 总…

项目一点点记录

kafka发布通知 kafka是消息队列&#xff0c;kafka采用发布订阅模式进行消息的生产与消费。在项目中&#xff0c;我们采用spring来整合kafka&#xff0c; 通过定义事件event来封装 点赞、关注、评论三类事件&#xff0c;event实体中有 事件主题topic&#xff0c;当前用户id&…

怎么给PDF添加图片水印?其实很简单,看这篇就会了!

许多人都意识到版权问题的重要性&#xff0c;尽管在日常生活中我们可能很少遇到&#xff0c;但在办公和学习中却经常涉及到此类问题。例如&#xff0c;我们辛辛苦苦制作的PDF文件&#xff0c;如何确保不被他人盗用呢?这就涉及到如何为PDF添加图片水印的问题&#xff0c;相当于…