Загрузка данных


const int buttonPin = 12;
const int ledPin = 13;
const int buzzerPin = 8;

bool ledState = false;
bool lastButtonState = HIGH;
bool currentButtonState;

// Ноты
#define NOTE_G4  392
#define NOTE_AS4 466
#define NOTE_C5  523
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_F5  698
#define NOTE_G5  784
#define NOTE_AS5 932
#define REST     0

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  noTone(buzzerPin);
}

void playMorgenshtern() {
  // Рифф в стиле Cadillac (Моргенштерн & Элджей)
  int melody[] = {
    // Основной хук
    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4,
    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4,

    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4,
    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4,

    // Повтор с вариацией
    NOTE_G5, NOTE_G4, NOTE_F5,  NOTE_DS5, NOTE_D5, NOTE_AS4,
    NOTE_G5, NOTE_G4, NOTE_F5,  NOTE_DS5, NOTE_D5, NOTE_AS4,

    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4,
    NOTE_G5, NOTE_G4, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_AS4
  };

  int noteDurations[] = {
    8, 8, 8, 8, 8, 4,
    8, 8, 8, 8, 8, 4,

    8, 8, 8, 8, 8, 4,
    8, 8, 8, 8, 8, 4,

    8, 8, 8, 8, 8, 4,
    8, 8, 8, 8, 8, 4,

    8, 8, 8, 8, 8, 4,
    8, 8, 8, 8, 8, 4
  };

  int notesCount = sizeof(melody) / sizeof(melody[0]);
  int tempo = 160;
  int wholenote = (60000 * 4) / tempo;

  for (int i = 0; i < notesCount; i++) {
    int duration = wholenote / noteDurations[i];

    if (melody[i] == REST) {
      noTone(buzzerPin);
      delay(duration);
    } else {
      tone(buzzerPin, melody[i], duration * 0.85);
      delay(duration);
      noTone(buzzerPin);
    }
  }

  noTone(buzzerPin);
}

void loop() {
  currentButtonState = digitalRead(buttonPin);

  if (lastButtonState == HIGH && currentButtonState == LOW) {
    
    ledState = !ledState;
    digitalWrite(ledPin, ledState);

    if (ledState == true) {
      playMorgenshtern();
    } else {
      noTone(buzzerPin);
    }

    delay(80);
  }

  lastButtonState = currentButtonState;
}