#define PHOTO_PIN PA0
#define SPEAKER_PIN PB6
#define BUTTON_PIN PA1
bool soundEnabled = true;
bool lastButtonState = HIGH;
bool currentButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(PHOTO_PIN, INPUT_ANALOG);
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != currentButtonState) {
currentButtonState = reading;
if (currentButtonState == LOW) {
soundEnabled = !soundEnabled;
Serial.print("Sound ");
Serial.println(soundEnabled ? "ON" : "OFF");
if (!soundEnabled) {
noTone(SPEAKER_PIN);
}
}
}
}
lastButtonState = reading;
if (soundEnabled) {
int lightValue = analogRead(PHOTO_PIN);
int frequency = map(lightValue, 0, 4095, 100, 2000);
tone(SPEAKER_PIN, frequency);
Serial.print("Light: ");
Serial.print(lightValue);
Serial.print(" | Freq: ");
Serial.print(frequency);
Serial.println(" Hz");
}
delay(10);
}