ESP32(掌控板) RGB灯控制

news2024/7/6 18:33:08

ESP32(掌控板) RGB灯控制

本程序实现了RGB灯亮度和颜色调节,通过触摸按键分别调节RGB值,通过机械按键选择要调节的灯或使用预设均通过调整变量的值加上判断实现。预设包含随机颜色和流水灯,各有高亮度和低亮度两种版本,另外可以把所有灯设为最高或最低亮度。为了加快按键扫描,流水灯采用每次循环执行一次、通过变量的值确定要进行的步骤的形式。

图形化程序如下

Arduino代码如下

/*!
 * MindPlus
 * mpython
 *
 */
#include <MPython.h>

// 动态变量
volatile float mind_n_i, mind_n_r, mind_n_g, mind_n_b, mind_n_j, mind_n_x, mind_n_k;
// 函数声明
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b);


// 主程序开始
void setup() {
	mPython.begin();
	dfrobotRandomSeed();
	mind_n_i = 1;
	mind_n_r = 0;
	mind_n_g = 0;
	mind_n_b = 0;
	mind_n_j = 2;
	mind_n_x = 1;
}
void loop() {
	if (((buttonA.isPressed()) && (mind_n_i>0))) {
		mind_n_i -= 1;
	}
	if (((buttonB.isPressed()) && (mind_n_i<7))) {
		mind_n_i += 1;
	}
	if ((mind_n_i>0)) {
		if ((mind_n_k==0)) {
			mind_n_k = 1;
			display.fillScreen(0);
			rgb.write(-1, rgbToColor(round(0), round(0), round(0)));
		}
		if (((touchPadP.isTouched()) && (mind_n_r>0))) {
			mind_n_r -= 10;
		}
		if (((touchPadY.isTouched()) && (mind_n_r<250))) {
			mind_n_r += 10;
		}
		if (((touchPadT.isTouched()) && (mind_n_g>0))) {
			mind_n_g -= 10;
		}
		if (((touchPadH.isTouched()) && (mind_n_g<250))) {
			mind_n_g += 10;
		}
		if (((touchPadO.isTouched()) && (mind_n_b>0))) {
			mind_n_b -= 10;
		}
		if (((touchPadN.isTouched()) && (mind_n_b<250))) {
			mind_n_b += 10;
		}
		display.setCursor(20, 16);
		display.print("红:");
		display.setCursor(50, 16);
		display.print("    ");
		display.setCursor(45, 16);
		display.print(mind_n_r);
		display.setCursor(45, 32);
		display.print("绿:");
		display.setCursor(75, 32);
		display.print("    ");
		display.setCursor(70, 32);
		display.print(mind_n_g);
		display.setCursor(70, 48);
		display.print("蓝:");
		display.setCursor(100, 48);
		display.print("    ");
		display.setCursor(95, 48);
		display.print(mind_n_b);
		if ((mind_n_i==1)) {
			display.fillInLine(1, 0);
			rgb.write(0, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.fillCircle(30, 8, 5);
			display.drawCircle(60, 8, 5);
			display.drawCircle(90, 8, 5);
		}
		if ((mind_n_i==2)) {
			display.fillInLine(1, 0);
			rgb.write(1, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.drawCircle(30, 8, 5);
			display.fillCircle(60, 8, 5);
			display.drawCircle(90, 8, 5);
		}
		if ((mind_n_i==3)) {
			display.fillInLine(1, 0);
			rgb.write(2, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.drawCircle(30, 8, 5);
			display.drawCircle(60, 8, 5);
			display.fillCircle(90, 8, 5);
		}
		if ((mind_n_i==4)) {
			display.fillInLine(1, 0);
			rgb.write(0, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			rgb.write(1, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.fillCircle(30, 8, 5);
			display.fillCircle(60, 8, 5);
			display.drawCircle(90, 8, 5);
		}
		if ((mind_n_i==5)) {
			display.fillInLine(1, 0);
			rgb.write(1, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			rgb.write(2, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.drawCircle(30, 8, 5);
			display.fillCircle(60, 8, 5);
			display.fillCircle(90, 8, 5);
		}
		if ((mind_n_i==6)) {
			display.fillInLine(1, 0);
			rgb.write(2, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			rgb.write(0, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.fillCircle(30, 8, 5);
			display.drawCircle(60, 8, 5);
			display.fillCircle(90, 8, 5);
		}
		if ((mind_n_i==7)) {
			display.fillInLine(1, 0);
			rgb.write(-1, rgbToColor(round(mind_n_r), round(mind_n_g), round(mind_n_b)));
			display.fillCircle(30, 8, 5);
			display.fillCircle(60, 8, 5);
			display.fillCircle(90, 8, 5);
		}
		delay(100);
	}
	if ((mind_n_i==0)) {
		if ((mind_n_k==1)) {
			display.fillScreen(0);
			mind_n_k = 0;
			display.setCursor(50, 5);
			display.print("预设");
			display.drawCircle(35, 13, 4);
			display.drawCircle(85, 13, 4);
			display.rect(20, 25, 88, 30, false);
		}
		if ((touchPadP.isTouched())) {
			mind_n_j = 1;
		}
		if ((touchPadY.isTouched())) {
			mind_n_j = 2;
		}
		if ((touchPadT.isTouched())) {
			mind_n_j = 3;
		}
		if ((touchPadH.isTouched())) {
			mind_n_j = 4;
		}
		if ((touchPadO.isTouched())) {
			mind_n_j = 5;
		}
		if ((touchPadN.isTouched())) {
			mind_n_j = 6;
		}
		if ((mind_n_j==1)) {
			display.fillInLine(3, 0);
			display.setCursor(42, 32);
			display.print("随机颜色");
			rgb.write(0, rgbToColor(round((random(0, 250+1))), round((random(0, 250+1))), round((random(0, 250+1)))));
			rgb.write(1, rgbToColor(round((random(0, 250+1))), round((random(0, 250+1))), round((random(0, 250+1)))));
			rgb.write(2, rgbToColor(round((random(0, 250+1))), round((random(0, 250+1))), round((random(0, 250+1)))));
		}
		if ((mind_n_j==2)) {
			display.fillInLine(3, 0);
			display.setCursor(35, 32);
			display.print("随机低亮度");
			rgb.write(0, rgbToColor(round((random(0, 100+1))), round((random(0, 100+1))), round((random(0, 100+1)))));
			rgb.write(1, rgbToColor(round((random(0, 100+1))), round((random(0, 100+1))), round((random(0, 100+1)))));
			rgb.write(2, rgbToColor(round((random(0, 100+1))), round((random(0, 100+1))), round((random(0, 100+1)))));
		}
		if ((mind_n_j==3)) {
			display.fillInLine(3, 0);
			display.setCursor(50, 32);
			display.print("流水灯");
			if ((mind_n_x==1)) {
				rgb.write(0, rgbToColor(round(200), round(0), round(0)));
				rgb.write(1, rgbToColor(round(100), round(100), round(0)));
				rgb.write(2, rgbToColor(round(0), round(200), round(0)));
			}
			if ((mind_n_x==2)) {
				rgb.write(0, rgbToColor(round(100), round(100), round(0)));
				rgb.write(1, rgbToColor(round(0), round(200), round(0)));
				rgb.write(2, rgbToColor(round(0), round(100), round(100)));
			}
			if ((mind_n_x==3)) {
				rgb.write(0, rgbToColor(round(0), round(200), round(0)));
				rgb.write(1, rgbToColor(round(0), round(100), round(100)));
				rgb.write(2, rgbToColor(round(0), round(0), round(200)));
			}
			if ((mind_n_x==4)) {
				rgb.write(0, rgbToColor(round(0), round(100), round(100)));
				rgb.write(1, rgbToColor(round(0), round(0), round(200)));
				rgb.write(2, rgbToColor(round(100), round(0), round(100)));
			}
			if ((mind_n_x==5)) {
				rgb.write(0, rgbToColor(round(0), round(0), round(200)));
				rgb.write(1, rgbToColor(round(100), round(0), round(100)));
				rgb.write(2, rgbToColor(round(200), round(0), round(0)));
			}
			if ((mind_n_x==6)) {
				rgb.write(0, rgbToColor(round(100), round(0), round(100)));
				rgb.write(1, rgbToColor(round(200), round(0), round(0)));
				rgb.write(2, rgbToColor(round(100), round(100), round(0)));
			}
			mind_n_x += 1;
			if ((mind_n_x==7)) {
				mind_n_x = 1;
			}
		}
		if ((mind_n_j==4)) {
			display.fillInLine(3, 0);
			display.setCursor(30, 32);
			display.print("流水灯低亮度");
			if ((mind_n_x==1)) {
				rgb.write(0, rgbToColor(round(100), round(0), round(0)));
				rgb.write(1, rgbToColor(round(50), round(50), round(0)));
				rgb.write(2, rgbToColor(round(0), round(100), round(0)));
			}
			if ((mind_n_x==2)) {
				rgb.write(0, rgbToColor(round(50), round(50), round(0)));
				rgb.write(1, rgbToColor(round(0), round(100), round(0)));
				rgb.write(2, rgbToColor(round(0), round(50), round(50)));
			}
			if ((mind_n_x==3)) {
				rgb.write(0, rgbToColor(round(0), round(100), round(0)));
				rgb.write(1, rgbToColor(round(0), round(50), round(50)));
				rgb.write(2, rgbToColor(round(0), round(0), round(100)));
			}
			if ((mind_n_x==4)) {
				rgb.write(0, rgbToColor(round(0), round(50), round(50)));
				rgb.write(1, rgbToColor(round(0), round(0), round(100)));
				rgb.write(2, rgbToColor(round(50), round(0), round(50)));
			}
			if ((mind_n_x==5)) {
				rgb.write(0, rgbToColor(round(0), round(0), round(100)));
				rgb.write(1, rgbToColor(round(50), round(0), round(50)));
				rgb.write(2, rgbToColor(round(100), round(0), round(0)));
			}
			if ((mind_n_x==6)) {
				rgb.write(0, rgbToColor(round(50), round(0), round(50)));
				rgb.write(1, rgbToColor(round(100), round(0), round(0)));
				rgb.write(2, rgbToColor(round(50), round(50), round(0)));
			}
			mind_n_x += 1;
			if ((mind_n_x==7)) {
				mind_n_x = 1;
			}
		}
		if ((mind_n_j==5)) {
			display.fillInLine(3, 0);
			display.setCursor(28, 32);
			display.print("红、绿、蓝=0");
			mind_n_r = 0;
			mind_n_g = 0;
			mind_n_b = 0;
			rgb.write(-1, rgbToColor(round(0), round(0), round(0)));
		}
		if ((mind_n_j==6)) {
			display.fillInLine(3, 0);
			display.setCursor(22, 32);
			display.print("红、绿、蓝=250");
			mind_n_r = 250;
			mind_n_g = 250;
			mind_n_b = 250;
			rgb.write(-1, rgbToColor(round(250), round(250), round(250)));
		}
		delay(200);
	}
}


// 静态函数
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b)
{
  return (uint32_t)((((uint32_t)r<<16) | ((uint32_t)g<<8)) | (uint32_t)b);
}

 

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

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

相关文章

《Kali渗透基础》07. 弱点扫描(一)

kali渗透 1&#xff1a;漏洞发现1.1&#xff1a;Exploit-DB1.2&#xff1a;searchsploit1.3&#xff1a;nmap 2&#xff1a;漏洞管理3&#xff1a;弱点扫描类型4&#xff1a;漏洞基本概念4.1&#xff1a;CVSS4.2&#xff1a;CVE4.3&#xff1a;OVAL4.4&#xff1a;CCE4.5&#…

TCP概念

文章目录 1.TCP1.1 为什么需要 TCP 协议&#xff1f;TCP 工作在哪一层&#xff1f;1.2 什么是 TCP &#xff1f;1.3 什么是 TCP 连接&#xff1f;1.4 如何唯一确定一个 TCP 连接&#xff1f;1.5 有一个 IP 的服务器监听了一个端口&#xff0c;它的 TCP 的最大连接数是多少&…

nginx的权限问题(13: Permission denied)解决办法

1、查看nginx启动用户和使用用户是否一致 ps aux | grep nginx 如图&#xff1a; 一个是www,一个是root用户 2、打开nginx配置文件 打开 nginx.conf 文件 查找nginx.conf的位置 ps -aux | grep nginx 3、把 nginx 改为 root 4、重启nginx服务 service nginx restart …

性能监控平台 | Prometheus+InfluxDB + Grafana!

在本文中&#xff0c;我将把几个常用的监控部分给梳理一下。前面我们提到过&#xff0c;在性能监控图谱中&#xff0c;有操作系统、应用服务器、中间件、队列、缓存、数据库、网络、前端、负载均衡、Web 服务器、存储、代码等很多需要监控的点。显然这些监控点不能在一个专栏中…

USG6000V 防火墙的策略应用

将G1/0/0划入TRUST区 firewall zone trustadd interface GigabitEthernet1/0/0 将G1/0/1划入UNTRUST区 firewall zone untrustadd interface GigabitEthernet1/0/1在防火墙上配置放行策略 security-policyrule name 1source-zone trustdestination-zone localdestination-z…

2023年船舶、海洋与海事工程国际会议(NAOME 2023) | Ei Scopus双检索

会议简介 Brief Introduction 2023年船舶、海洋与海事工程国际会议(NAOME 2023) 会议时间&#xff1a;2023年10月20日-22日 召开地点&#xff1a;中国镇江 大会官网&#xff1a;NAOME 2023-2023 International Conference on Naval Architecture and Ocean & Marine Engine…

Yolov8方法笔记

目录 1、安装yolov8的虚拟环境 &#xff08;1&#xff09;创建虚拟环境 &#xff08;2&#xff09;安装pytorch ​编辑 2、下载yolov8模型 3、解压缩&#xff0c;解压后的文件 4、将转换数据集&#xff08;以口罩检测为准&#xff09; 5、配置和执行 6、验证 7、测试 1、…

14-C++基本算法-深度优先搜索

&#x1f4da; 从递归阶乘到深度优先搜索 在学习深度优先搜索之前&#xff0c;我们先回顾一下递归阶乘的实现。递归阶乘是一种典型的递归算法&#xff0c;它通过将问题分解为更小的子问题来解决。 #include <iostream> using namespace std;int factorial(int n) {if (…

【PCIE】协议分析之-hot-reset热复位

被上游指定热复位整个通路 当高层&#xff08;higher Layer&#xff09;指示某些通道进行热复位&#xff08;Hot Reset&#xff09;时&#xff0c;以下操作将被执行&#xff1a; 所有在配置的链路中的通道都会发送带有热复位位&#xff08;Hot Reset bit&#xff09;和配置的…

IntelliJ IDEA运行bat脚本,自动taskkill端口进程

在idea运行完程序后&#xff0c;再次重新运行时如果之前的程序仍然占用着端口会导致报错&#xff1a;端口被占用。 因此每次重新运行都需要移除之前的端口所在的进程&#xff0c;这对于调试开发过程是非常频繁的操作。 需要一个快速的办法直接移除端口进程&#xff0c;如下&a…

Haskell 入门学习(一)之安装试用 Haskell

Haskell 入门学习&#xff08;一&#xff09;之安装试用 Haskell 文章目录 Haskell 入门学习&#xff08;一&#xff09;之安装试用 Haskell前言&#xff1a;安装Windows 安装Linux、MacOs 使用 VSCode 进行代码编写创建一个简单的项目使用 Cabal 管理项目项目大致结构运行项目…

Verilog学习笔记3:与非门

1位的与非门 代码&#xff1a; timescale 1ns/10ps module nand_gate( A, B, Y);input A; input B; output Y;assign Y~(A&B);endmodule//testbenchmodule nand_gate_tb; reg A; reg B; wire Y;nand_gate nand_gate(.A(A),.B(B),.Y(Y));initial begin A<0;B<0;#10 …

Ubuntu创建Git项目并push到远程Github

首先在本地创建git仓库 jasminelhl:~/prj$ mkdir Github-test jasminelhl:~/prj$ cd Github-test jasminelhl:~/prj/Github-test$ git init 提示&#xff1a;使用 master 作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中 提示&#xff1a;配置使用初始分支名&a…

MySQL之CONCAT、CONCAT_WS和GROUP_CONCAT函数用法

目录 一、准备数据 二、concat函数 1.语法格式 2.语法说明 3.场景使用 4.限制条件 三、concat_ws函数 1.语法格式 2.语法说明 3.场景使用 4.限制条件 四、group_conat函数 1.语法格式 2.语法说明 3.场景使用 4.限制条件 一、准备数据 #创建用户表 CREATE TABL…

[已解决]Springboot项目启动时端口被占用

目录 1.打开CMD窗口 2.找到端口号 3.找到对应程序 4.终止任务 5.再次查询端口&#xff0c;没有找到对应的端口号说明进行杀死成功 6. 重新启动项目&#xff0c;可以正常启动 Springboot端口号默认8080&#xff0c;启动时经常会遇到端口号被占用的情况&#xff0c;报错信息…

swift 打包xcframework报错“No ‘swiftinterface‘ files found within“

1、分别编译当前的framework&#xff0c;生成对应的真机framework与模拟器framework 生成对应framework.png 2、新建一个对应接收xcframework的文件夹&#xff0c;以XXX.xcframework格式命名 3、打开终端利用xcodebuild -create-xcframework命令进行生成xcframework。 具体如…

第一章 计算机网络概述【计算机网络】

第一章 计算机网络概述【计算机网络】 前言推荐第一章 计算机网络概述1.1 计算机网络在信息时代中的作用1.2互联网概述1.2.1 网络的网络1.2.2互联网基础结构发展的三个阶段1.2.3互联网的标准化工作 1.3互联网的组成1.3.1互联网的边缘部分1.3.2互联网的核心部分 1.4计算机网络在…

网络——网络排错

https://zhuanlan.zhihu.com/p/607083215 背景 出现网络故障时&#xff0c;我们需要做什么呢&#xff0c;做完网工&#xff08;不如dog&#xff09;&#xff0c;第一时间会被怀疑是网络问题&#xff0c;所以&#xff0c; 前端PC 前端PC能做的东西&#xff0c;不多&#xff0…

Go语言程序设计(十三)方法

Go语言虽然没有类(Class),但同样支持方法(Method),Go语言里的Method其实就是一个带接收者(Receiver)的函数。 一、Method的基本定义 Go语言中的Method类似于一个函数&#xff0c;只是函数名前多了一个绑定类型参数——receiver。 基本格式如下&#xff1a; func (recv recei…

启用 -parameters 编译选项简化 mybatis @Param 注解重复问题

在使用 mybatis 查询的时候, 只需要定义一个查询接口, mybatis 会为我们注入注解实现或是 xml 实现. 但当我们需要传递参数时, 通常需要 Param 来定义一个名称, 但经常的, 我们也不难发现, 这个名称与参数名称通常是一样的: User findUser(Param("username") String…