#define BUZZER 8
#define LED 9
#define POT A0
int melody[] = {
262, 294, 330, 349,
392, 440, 494, 523,
494, 440, 392, 349,
330, 294, 262
};
int duration[] = {
250, 250, 250, 250,
250, 250, 250, 400,
250, 250, 250, 250,
250, 250, 500
};
void setup() {
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int potValue = analogRead(POT);
int shift = map(potValue, 0, 1023, -100, 100);
for (int i = 0; i < 15; i++) {
int frequency = melody[i] + shift;
if (frequency < 50) {
frequency = 50;
}
tone(BUZZER, frequency);
digitalWrite(LED, HIGH);
delay(duration[i]);
noTone(BUZZER);
digitalWrite(LED, LOW);
delay(50);
}
delay(500);
}