#include <Adafruit_NeoPixel.h>
// NeoPixel configuration
#define NUM_LEDS 1
#define DATA_PIN 0 // Pin connected to the RGB LED data line
#define SPEAKER_PIN 2
Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
// Button pins
const int redButton = 6;
const int greenButton = 4;
const int blueButton = 5;
const int yellowButton = 7;
const int resetButton = 1;
bool locked = false;
void setup() {
strip.begin();
strip.show(); // Turn off all LEDs initially
pinMode(redButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(blueButton, INPUT_PULLUP);
pinMode(yellowButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
// Check Reset button first to unlock
if (digitalRead(resetButton) == LOW) { // LOW because of INPUT_PULLUP
strip.setPixelColor(0, strip.Color(0, 0, 0)); // Turn off the LED
strip.show();
locked = false; // Unlock the buttons
delay(300); // Debounce the reset button
return; // Exit to avoid processing other inputs in the same loop
}
// If buttons are not locked, check color buttons
if (!locked) {
if (digitalRead(redButton) == LOW) { // LOW because of INPUT_PULLUP
digitalWrite(SPEAKER_PIN, HIGH);
tone(SPEAKER_PIN, 294, 750);
strip.setPixelColor(0, strip.Color(255, 0, 0)); // Set LED to red
locked = true;
} else if (digitalRead(greenButton) == LOW) {
digitalWrite(SPEAKER_PIN, HIGH);
tone(SPEAKER_PIN, 262, 750);
strip.setPixelColor(0, strip.Color(0, 255, 0)); // Set LED to green
locked = true;
} else if (digitalRead(blueButton) == LOW) {
digitalWrite(SPEAKER_PIN, HIGH);
tone(SPEAKER_PIN, 262, 750);
strip.setPixelColor(0, strip.Color(0, 0, 255)); // Set LED to blue
locked = true;
} else if (digitalRead(yellowButton) == LOW) {
digitalWrite(SPEAKER_PIN, HIGH);
tone(SPEAKER_PIN, 262, 750);
strip.setPixelColor(0, strip.Color(255, 255, 0)); // Set LED to yellow
locked = true;
} else {
digitalWrite(SPEAKER_PIN, LOW);
noTone(SPEAKER_PIN);
}
// If any button is pressed, light up the LED and lock
if (locked) {
strip.show(); // Refresh the LED with the new color
delay(300); // Debounce the button press
}
}
}