const int potPin = A0;
const int piezoPin = 9;
const int ledPin = 6;
// Ноты мелодии
int melody[] = {
262, 294, 330, 392,
330, 294, 262, 0,
262, 330, 392, 523,
392, 330, 294, 0
};
// Длительность нот
int duration[] = {
300, 300, 300, 500,
300, 300, 500, 200,
300, 300, 300, 500,
300, 300, 500, 300
};
void setup() {
pinMode(piezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Считываем положение потенциометра
int potValue = analogRead(potPin);
// От -200 до +200 Гц
int shift = map(potValue, 0, 1023, -200, 200);
for (int i = 0; i < 16; i++) {
int note = melody[i] + shift;
// Пауза
if (melody[i] == 0) {
noTone(piezoPin);
digitalWrite(ledPin, LOW);
delay(duration[i]);
}
else {
// Защита от слишком низкой частоты
if (note < 100) {
note = 100;
}
tone(piezoPin, note);
// Предупреждающий LED
if (note > 500) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(duration[i]);
noTone(piezoPin);
delay(50);
}
}
}