【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目实验之五:多色彩动态循环同心园
实验开源代码
/*
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目实验之五:多色彩动态循环同心园
*/
// GC9A010 -------- ESP32
// RST ------------ NC(复位引脚,此处未连接)
// CS ------------- D4(片选引脚,连接到ESP32的D4引脚)
// DC ------------- D2(数据/命令选择引脚,连接到ESP32的D2引脚)
// SDA ------------ D23 (green)(主数据输出引脚,连接到ESP32的D23引脚,绿色线)
// SCL ------------ D18 (yellow)(时钟信号引脚,连接到ESP32的D18引脚,黄色线)
// GND ------------ GND(接地引脚,连接到ESP32的接地端)
// VCC -------------3V3(电源引脚,连接到ESP32的3.3V电源)
#include "SPI.h" // 引入SPI库
#include "Adafruit_GFX.h" // 引入Adafruit GFX库
#include "Adafruit_GC9A01A.h" // 引入GC9A01A显示屏驱动库
#define TFT_DC 2 // 定义TFT显示屏的DC引脚
#define TFT_CS 4 // 定义TFT显示屏的CS引脚
Adafruit_GC9A01A tft(TFT_CS, TFT_DC); // 创建GC9A01A对象
void setup() {
Serial.begin(9600); // 初始化串口通信
Serial.println("Multi-Circle Animation with Color Change!");
tft.begin(); // 初始化显示屏
tft.setRotation(3); // 设置显示屏方向
tft.fillScreen(GC9A01A_BLACK); // 清屏
}
void loop() {
int centerX = tft.width() / 2; // 屏幕中心X坐标
int centerY = tft.height() / 2; // 屏幕中心Y坐标
// 定义颜色数组用于动态颜色切换
uint16_t colors[] = {GC9A01A_RED, GC9A01A_GREEN, GC9A01A_BLUE, GC9A01A_YELLOW, GC9A01A_CYAN, GC9A01A_MAGENTA, GC9A01A_WHITE};
int colorCount = sizeof(colors) / sizeof(colors[0]);
static int colorIndex = 0; // 当前颜色索引
static int radius1 = 10; // 第一个圆的半径
static int radius2 = 20; // 第二个圆的半径
static int radius3 = 30; // 第三个圆的半径
int step = 5; // 半径的变化步长
static bool increasing1 = true; // 控制第一个圆半径增减
static bool increasing2 = true; // 控制第二个圆半径增减
static bool increasing3 = true; // 控制第三个圆半径增减
// 清屏,绘制新一帧动画
tft.fillScreen(GC9A01A_BLACK);
// 绘制三个圆
tft.drawCircle(centerX, centerY, radius1, colors[colorIndex]); // 圆1
tft.drawCircle(centerX, centerY, radius2, colors[(colorIndex + 1) % colorCount]); // 圆2
tft.drawCircle(centerX, centerY, radius3, colors[(colorIndex + 2) % colorCount]); // 圆3
// 更新第一个圆半径
if (increasing1) {
radius1 += step;
if (radius1 >= 115) { // 达到最大半径后反向
increasing1 = false;
}
} else {
radius1 -= step;
if (radius1 <= 10) { // 达到最小半径后反向
increasing1 = true;
}
}
// 更新第二个圆半径
if (increasing2) {
radius2 += step;
if (radius2 >= 115) {
increasing2 = false;
}
} else {
radius2 -= step;
if (radius2 <= 10) {
increasing2 = true;
}
}
// 更新第三个圆半径
if (increasing3) {
radius3 += step;
if (radius3 >= 115) {
increasing3 = false;
}
} else {
radius3 -= step;
if (radius3 <= 10) {
increasing3 = true;
}
}
// 更新颜色索引
colorIndex = (colorIndex + 1) % colorCount;
// 延时0.3秒,控制动画速度
delay(300);
}
代码说明
1、颜色变化:
定义一个颜色数组colors,包含多种颜色(红、绿、蓝等)。
每次循环通过增加颜色索引colorIndex,动态切换绘制的颜色。
2、多圆绘制:
绘制三个圆,分别使用不同的颜色索引,让每个圆的颜色不同。
每个圆的半径独立变化,通过不同的逻辑控制,形成丰富的视觉效果。
3、半径变化控制:
每个圆的半径在10到115像素之间变化,分别通过变量increasing1、increasing2和increasing3独立控制。
4、动态清屏:
每次循环用tft.fillScreen(GC9A01A_BLACK)清屏,保持画面整洁,仅显示当前的圆形动画。
5、可扩展性:
圆的数量和颜色可以轻松扩展,通过增加数组或变量即可实现更多效果。
效果描述
屏幕上会同时绘制三个动态圆,圆的半径在10到115像素之间往复变化。
每个圆的颜色随着时间循环变化,形成绚丽的动画效果。
三个圆独立控制半径变化,带来层次感和动态美感。
实验场景图 动态图




实验记录视频(45秒)
【【花雕学编程】Arduino动手做(249)---ESP32驱动1.28寸 TFT GC9A01圆屏之多色彩动态循环同心园】
评论