Musical Relay Switch

This code allows you to control an LED, play a melody, and activate a relay using a pushbutton. When you press the button, the LED turns on, the relay briefly activates, and the buzzer plays a melody. When you release the button, the LED turns off, and the relay deactivates. This project is a good introduction to combining different components in an Arduino project and reacting to user input with actions. It also introduces the concept of debouncing to avoid accidental button presses. You can visit and simulate the circuit HERE.

This Arduino code is a project for beginners that combines different components and actions, including controlling an LED, playing a melody on a buzzer, and activating a relay when a button is pressed. Let me break it down step by step in a beginner-friendly way:


1. Definitions of Musical Notes:

   - The code starts by defining musical notes and their corresponding frequencies. These definitions will be used later to create a melody.


2. Variable Declarations:

   - Next, the code declares some variables that will be used in the program. Here's what each variable is for:

     - `relayPin`: This is the pin number for controlling a relay. A relay is an electrical switch that can be turned on or off.

     - `buttonPin`: This is the pin number for a pushbutton, which is like a simple switch.

     - `ledPin`: This is the pin number for an LED, which is a small light.

     - `buzzer`: This is the pin number for a buzzer, which can produce sound.

     - Various variables like `buttonState`, `ledState`, `lastDebounceTime`, and `debounceDelay` are used for managing the button and LED.


3. Melody Array:

   - The code defines a melody as an array called `melody`. Each element in the array represents a musical note and its duration. The melody is what the buzzer will play.


4. Setup Function (`setup()`):

   - The setup function is executed only once when the Arduino is powered on.

   - In this function:

     - Serial communication is initialized for debugging (printing messages to the computer).

     - Pins are configured as inputs or outputs, such as the LED pin and button pin.

     - The relay pin is also configured as an output.


5. Main Loop Function (`loop()`):

   - The loop function runs continuously, over and over.

   - It does the following:

     - Reads the state of the pushbutton (whether it's pressed or not).

     - Checks for button press and filters out noise using a debounce mechanism to prevent false triggers.

     - If the button is pressed and the LED is off:

       - The LED is turned on.

       - A relay is activated, and it's kept on for a short duration.

       - A melody is played on the buzzer.

     - If the button is released and the LED is on:

       - The LED is turned off.

       - The relay is deactivated.

     - The melody is played using the `tone()` function, and there are delays between notes to control the rhythm of the melody.


6. Custom Function (`ActivateRelay()`):

   - This is a separate function you've defined in your code.

   - It turns on the relay for a specified duration and then turns it off.


In summary, this code allows you to control an LED, play a melody, and activate a relay using a pushbutton. When you press the button, the LED turns on, the relay briefly activates, and the buzzer plays a melody. When you release the button, the LED turns off, and the relay deactivates. This project is a good introduction to combining different components in an Arduino project and reacting to user input with actions. It also introduces the concept of debouncing to avoid accidental button presses.

#define NOTE_B0  31#define NOTE_C1  33#define NOTE_CS1 35#define NOTE_D1  37#define NOTE_DS1 39#define NOTE_E1  41#define NOTE_F1  44#define NOTE_FS1 46#define NOTE_G1  49#define NOTE_GS1 52#define NOTE_A1  55#define NOTE_AS1 58#define NOTE_B1  62#define NOTE_C2  65#define NOTE_CS2 69#define NOTE_D2  73#define NOTE_DS2 78#define NOTE_E2  82#define NOTE_F2  87#define NOTE_FS2 93#define NOTE_G2  98#define NOTE_GS2 104#define NOTE_A2  110#define NOTE_AS2 117#define NOTE_B2  123#define NOTE_C3  131#define NOTE_CS3 139#define NOTE_D3  147#define NOTE_DS3 156#define NOTE_E3  165#define NOTE_F3  175#define NOTE_FS3 185#define NOTE_G3  196#define NOTE_GS3 208#define NOTE_A3  220#define NOTE_AS3 233#define NOTE_B3  247#define NOTE_C4  262#define NOTE_CS4 277#define NOTE_D4  294#define NOTE_DS4 311#define NOTE_E4  330#define NOTE_F4  349#define NOTE_FS4 370#define NOTE_G4  392#define NOTE_GS4 415#define NOTE_A4  440#define NOTE_AS4 466#define NOTE_B4  494#define NOTE_C5  523#define NOTE_CS5 554#define NOTE_D5  587#define NOTE_DS5 622#define NOTE_E5  659#define NOTE_F5  698#define NOTE_FS5 740#define NOTE_G5  784#define NOTE_GS5 831#define NOTE_A5  880#define NOTE_AS5 932#define NOTE_B5  988#define NOTE_C6  1047#define NOTE_CS6 1109#define NOTE_D6  1175#define NOTE_DS6 1245#define NOTE_E6  1319#define NOTE_F6  1397#define NOTE_FS6 1480#define NOTE_G6  1568#define NOTE_GS6 1661#define NOTE_A6  1760#define NOTE_AS6 1865#define NOTE_B6  1976#define NOTE_C7  2093#define NOTE_CS7 2217#define NOTE_D7  2349#define NOTE_DS7 2489#define NOTE_E7  2637#define NOTE_F7  2794#define NOTE_FS7 2960#define NOTE_G7  3136#define NOTE_GS7 3322#define NOTE_A7  3520#define NOTE_AS7 3729#define NOTE_B7  3951#define NOTE_C8  4186#define NOTE_CS8 4435#define NOTE_D8  4699#define NOTE_DS8 4978#define REST      0
// Int for Relayint relayPin =3;// constants won't change. They're used here to set pin numbers:const int buttonPin = 7;     // the number of the pushbutton pinconst int ledPin =  12;      // the number of the LED pin// change this to whichever pin you want to use for speaker int buzzer = 2;// variables will change:int buttonState = 0;         // variable for reading the pushbutton status// change this to make the song slower or fasterint tempo = 225;
const int melody[] PROGMEM = {
  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //1  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8,  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8,  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //5  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8,  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8,  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //9  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8,  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8,  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //13  NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8,  NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8,  NOTE_FS3, -16, NOTE_D3, -16, NOTE_B2, -16, NOTE_A3, -16, NOTE_FS3, -16, NOTE_B2, -16, NOTE_D3, -16, NOTE_FS3, -16, NOTE_A3, -16, NOTE_FS3, -16, NOTE_D3, -16, NOTE_B2, -16,};// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)// there are two values per note (pitch and duration), so for each note there are four bytesint notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in msint wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
//********************* BEGIN VOID SETUP **************************************************void setup() {
  // initialize the LED pin as an output:  pinMode(ledPin, OUTPUT);  // initialize the pushbutton pin as an input:  pinMode(buttonPin, INPUT);
  //initialize Relay as OUTPUT  pinMode(relayPin, OUTPUT);
   // initialize the LED pin as an output:  pinMode(ledPin, OUTPUT);    // initialize the pushbutton pin as an input:  pinMode(buttonPin, INPUT); }  // END OF VOID SETUP()**********************************************************
//***************************** START VOID LOOP *****************************************************void loop() {    // read the state of the pushbutton value:  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:  if (buttonState == HIGH) {        //Call Function to activate Relay on seperate timer    ActivateRelay(500);        // turn LED on:    digitalWrite(ledPin, HIGH);        // iterate over the notes of the melody.  // Remember, the array is twice the number of notes (notes + durations)  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
    // calculates the duration of each note    divider = pgm_read_word_near(melody+thisNote + 1);    if (divider > 0) {      // regular note, just proceed      noteDuration = (wholenote) / divider;    } else if (divider < 0) {      // dotted notes are represented with negative durations!!      noteDuration = (wholenote) / abs(divider);      noteDuration *= 1.5; // increases the duration in half for dotted notes    }
    // we only play the note for 90% of the duration, leaving 10% as a pause    tone(buzzer, pgm_read_word_near(melody+thisNote), noteDuration * 0.9);
    // Wait for the specief duration before playing the next note.    delay(noteDuration);
    // stop the waveform generation before the next note.    noTone(buzzer);  }  }  if  (buttonState == LOW) {    // turn LED off:    digitalWrite(ledPin, LOW);    }   } // *****************************************closing bracket for void loop()
//************************* OUTSIDE of Loop**************************************void ActivateRelay(int delayTime){    // swith relay on:    digitalWrite(relayPin, HIGH);    delay(delayTime);    // swith relay off:    digitalWrite(relayPin, LOW);    delay(delayTime);}

"Musical Doorbell Relay: Combining Tech and Creativity"


Introduction:

In the world of DIY electronics and home automation, one of my recent projects was to bring a touch of music and technology to the classic doorbell. I've created a "Musical Doorbell Relay" that not only activates the standard doorbell upstairs but also plays the iconic Doom theme song in my lab/office when someone rings the doorbell. And it doesn't stop there! I've even 3D printed a custom doorbell design with my own push button to complete the setup.


Project Overview:

This project combines various elements, including a relay, an Arduino board, a buzzer, and a 3D-printed doorbell, to create a unique and entertaining doorbell system. Here's how it works:


1. The Musical Component:

   - I've incorporated a buzzer into the system to play the Doom theme song when the doorbell rings. This adds an element of surprise and fun to the whole experience.


2. Activating the Standard Doorbell:

   - The relay is used to trigger the standard doorbell upstairs, ensuring that the conventional functionality is retained.


3. Custom 3D-Printed Doorbell:

   - To make this project truly unique, I designed and 3D printed a new doorbell. It not only looks great but also houses the custom-made push button that integrates seamlessly into the circuit.


How It Works:

When someone rings the doorbell, the relay is activated, triggering the standard doorbell sound upstairs. Simultaneously, the buzzer in my lab/office plays the Doom theme song, adding an element of personalization to the experience.


Conclusion:

This project has been a delightful combination of creativity, technology, and a touch of nostalgia. The ability to customize my doorbell's sound and design has not only added a unique twist to my home but also showcased the endless possibilities of DIY electronics.

Stay tuned for more DIY projects that bring technology and creativity together in exciting ways! If you have any questions or want to learn more about how you can create your own Musical Doorbell Relay, feel free to reach out. Happy tinkering!

Donate

If you've enjoyed exploring my Arduino projects and want to see more amazing creations, your support can make a big difference! By contributing, you're helping me continue to innovate and bring even more exciting projects to life. Together, we can explore the endless possibilities of DIY electronics! Don't forget to like, subscribe, and follow for updates on the latest developments. Thank you for being a part of this journey! 

Click here to make a difference with your donation today!