// The button's and the buzzer's pins const int BUTTON_PIN = 2; const int BUZZER_PIN = 8; // The bell's notes (Hz): E, C, D, G const int MELODY[] = {659, 523, 587, 392}; // How long each note lasts (ms) const int DURATIONS[] = {400, 400, 400, 800}; const int NOTE_COUNT = 4; int rings = 0; void setup() { // Pull-up: a pressed button reads LOW pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(BUZZER_PIN, OUTPUT); Serial.begin(9600); Serial.println("Doorbell ready: press the button"); } // Plays the notes one after another void playMelody() { for (int i = 0; i < NOTE_COUNT; i++) { tone(BUZZER_PIN, MELODY[i], DURATIONS[i]); // Let it end, then a short pause delay(DURATIONS[i] + 50); } noTone(BUZZER_PIN); } void loop() { if (digitalRead(BUTTON_PIN) == LOW) { rings++; Serial.print("Ring: "); Serial.println(rings); playMelody(); // Wait until the button is let go while (digitalRead(BUTTON_PIN) == LOW) { delay(10); } // Let the contacts settle on release delay(50); } }