#include <Adafruit_NeoPixel.h>
#define LED_PIN 11
#define BUZZER_PIN 5
#define BUTTON_PIN 3
#define LED_COUNT 16
// 初始化NeoPixel环
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// 音乐参数(《超级马里奥》主题曲片段)
int melody[] = { 660, 660, 660, 510, 660, 770, 380 };
int noteDurations[] = { 100, 100, 100, 100, 100, 100, 100 };
volatile bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
strip.begin();
strip.show(); // 初始化所有灯关闭
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISR, FALLING);
resetLights();
}
void loop() {
if (buttonPressed && (millis() - lastDebounceTime) > debounceDelay) {
buttonPressed = false;
startAnimation();
}
}
void buttonISR() {
if (!buttonPressed && (millis() - lastDebounceTime) > debounceDelay) {
buttonPressed = true;
lastDebounceTime = millis();
}
}
void resetLights() {
for (int i=0; i<LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(255, 255, 0)); // 黄色
}
strip.show();
}
void startAnimation() {
// 关闭所有灯光(逐个熄灭)
for (int i=0; i<LED_COUNT; i++) {
strip.setPixelColor(i, 0);
strip.show();
playMusicSegment(i);
delay(125); // 2秒完成16个灯(2000/16=125ms)
}
noTone(BUZZER_PIN);
delay(1000);
resetLights();
}
void playMusicSegment(int noteIndex) {
if (noteIndex < 7) { // 只播放前7个音符
int duration = 1000 / noteDurations[noteIndex];
tone(BUZZER_PIN, melody[noteIndex], duration);
delay(duration * 1.3);
noTone(BUZZER_PIN);
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
bz1:1
bz1:2
ring1:GND
ring1:VCC
ring1:DIN
ring1:DOUT
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r