const int SPEAKER = 9;
const int BUTTON_PIN = 2;
int notes[] = {392, 392, 392, 311, 466, 392, 311, 466, 392, 587, 587, 587, 622, 466, 369, 311, 466, 392, 784, 392, 392, 784, 739, 698, 659, 622, 659, 415, 554, 523, 493, 466, 440, 466, 311, 369, 311, 466, 392};
// Ускоренные длительности (в 2 раза быстрее)
int times[] = {175, 175, 175, 125, 50, 175, 125, 50, 350, 175, 175, 175, 125, 50, 175, 125, 50, 350, 175, 125, 50, 175, 125, 50, 50, 50, 225, 75, 175, 125, 50, 50, 50, 225, 75, 175, 125, 50, 375};
bool playing = false;
int currentNote = 0;
unsigned long noteStartTime = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SPEAKER, OUTPUT);
}
void loop() {
static bool lastBtn = HIGH;
bool btn = digitalRead(BUTTON_PIN);
if (btn == LOW && lastBtn == HIGH) {
playing = !playing;
if (!playing) {
noTone(SPEAKER);
currentNote = 0;
} else {
currentNote = 0;
noteStartTime = millis();
tone(SPEAKER, notes[0]);
}
delay(50);
}
lastBtn = btn;
if (playing && currentNote < 39) {
if (millis() - noteStartTime >= (unsigned long)times[currentNote] * 2) {
noTone(SPEAKER);
currentNote++;
if (currentNote < 39) {
noteStartTime = millis();
tone(SPEAKER, notes[currentNote]);
} else {
playing = false;
}
}
}
}