#include // ===== 硬件配置 ===== #define NUM_RINGS 3 const uint8_t ringPins[NUM_RINGS] = {3, 5, 7}; // D1/D3/D5 const uint8_t buttonPins[NUM_RINGS] = {2, 4, 6}; // D0/D2/D4 Adafruit_NeoPixel rings[NUM_RINGS] = { Adafruit_NeoPixel(3, ringPins[0], NEO_GRB + NEO_KHZ800), Adafruit_NeoPixel(3, ringPins[1], NEO_GRB + NEO_KHZ800), Adafruit_NeoPixel(3, ringPins[2], NEO_GRB + NEO_KHZ800) }; // ===== 语音模块 ===== HardwareSerial VoiceSerial(0); // UART0 (GPIO20/21) void setup() { // 初始化硬件 for (int i = 0; i < NUM_RINGS; i++) { rings[i].begin(); rings[i].clear(); rings[i].show(); pinMode(buttonPins[i], INPUT); } VoiceSerial.begin(9600, SERIAL_8N1, 20, 21); setVolume(0x1E); randomSeed(analogRead(0)); } void loop() { // 随机选择灯环和颜色 uint8_t activeRing = random(NUM_RINGS); uint32_t randomColor = Adafruit_NeoPixel::Color(random(256), random(256), random(256)); // 点亮灯环 rings[activeRing].fill(randomColor); rings[activeRing].show(); // 0.9秒内检测按键 bool gameOver = false; unsigned long startTime = millis(); while (millis() - startTime < 900) { for (int i = 0; i < NUM_RINGS; i++) { if (digitalRead(buttonPins[i])) { if (i == activeRing) { // 正确按键 rings[activeRing].clear(); rings[activeRing].show(); playTrack(random(9) + 2); // 随机播放02-10 delay(300); return; // 直接进入下一轮 } else { // 错误按键 gameOver = true; break; } } } if (gameOver) break; delay(10); } // 游戏结束处理 playTrack(1); // 播放01 blinkAllRings(Adafruit_NeoPixel::Color(255, 0, 0)); // 红灯闪烁 // 等待三键重启 while (!(digitalRead(buttonPins[0]) && digitalRead(buttonPins[1]) && digitalRead(buttonPins[2]))) { delay(10); } blinkAllRings(Adafruit_NeoPixel::Color(255, 255, 0)); // 黄灯闪烁 } // ===== 辅助函数 ===== void blinkAllRings(uint32_t color) { for (int i = 0; i < 3; i++) { for (int j = 0; j < NUM_RINGS; j++) { rings[j].fill(color); rings[j].show(); } delay(200); for (int j = 0; j < NUM_RINGS; j++) { rings[j].clear(); rings[j].show(); } delay(200); } } void playTrack(uint8_t track) { uint8_t cmd[] = {0xAA, 0x07, 0x02, 0x00, track, (uint8_t)(0xAA + 0x07 + 0x02 + 0x00 + track)}; VoiceSerial.write(cmd, sizeof(cmd)); } void setVolume(uint8_t vol) { uint8_t cmd[] = {0xAA, 0x13, 0x01, vol, (uint8_t)(0xAA + 0x13 + 0x01 + vol)}; VoiceSerial.write(cmd, sizeof(cmd)); }