Tuesday, December 29, 2020

Christmas Decoration using Arduino Mega 2560

In this post I will show you how to make a Christmas decoration with blinking LEDs and a background music using Arduino Mega 2560.


You need to connect piezo buzzer to the 12th pin and LEDs from 22 to 52 pins as configured in the code.

LEDs should connect to Arduino pins based on the levels shown below. 

As shown in the video you can use similar STAR using plastic sheet to pin 21 LEDs for this decoration.

Note: In order to change the brightness of LEDs and sound of the buzzer, use appropriate resistors serial with load. (Eg : 220 ohm resister for each LED and 100 ohm resister for buzzer)

You can see which LED will fall into which level from the Arduino code.

Music was generated using Arduino tone function. After each note, one LED level will be switched ON, while others remain OFF, which will be continued throughout the entire song.

Music notes frequencies were generated by the formula mentioned in below reference.

https://pages.mtu.edu/~suits/NoteFreqCalcs.html

Here, in order to understand how each note plays, notes were written one by one, which makes the code bigger.

If you are sure about the musical notes, just place them in an array, along with weights of each note in a similar size another array, and play them using tone function inside a for loop.

Eg:  

// Musical Notes
#define G1 196
#define A1 220
#define B1 247
#define C 261
#define D 294
#define E 329
#define F 349
#define G 392
#define A 440
#define B 493
#define C2 523

int notes = {G, G, G, C, B, ....}

int weights = {0.5, 0.5, 1, 1, 2, ....}

 

Arduino Code

// Musical Notes
#define G1 196
#define A1 220
#define B1 247
#define C 261
#define D 294
#define E 329
#define F 349
#define G 392
#define A 440
#define B 493
#define C2 523

//#define blink_delay 500
#define tone_delay 250

#define SP 12 //Speaker

#define L1 22 // Mid LED

#define L2 24 // 1st Level LEDs
#define L3 26
#define L4 28
#define L5 30
#define L6 32

#define L7 34 // 2nd Level LEDs
#define L8 36
#define L9 38
#define L10 40
#define L11 42
#define L12 44
#define L13 46
#define L14 48
#define L15 50
#define L16 52

#define L17 31 // Outer LEDs
#define L18 33
#define L19 35
#define L20 37
#define L21 39

int leds[] = {L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, L20, L21};
int NUM_LEDS = 21;

void setup() {
  pinMode(SP, OUTPUT);
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(leds[i], OUTPUT);
  }
}

void lights_on() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(leds[i], HIGH);
  }
}

void lights_off() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(leds[i], LOW);
  }
}

void lights_blink_1(int blink_delay) {
  lights_off();
  delay(blink_delay / 4);
  lights_on();
  delay(blink_delay / 4);
  lights_off();
  delay(blink_delay / 4);
  digitalWrite(L1, HIGH);
  delay(blink_delay / 4);
}

void lights_blink_2(int blink_delay) {
  lights_off();
  delay(blink_delay / 4);
  lights_on();
  delay(blink_delay / 4);
  lights_off();
  delay(blink_delay / 4);
  for (int i = 1; i < 6; i++) {
    digitalWrite(leds[i], HIGH);
  }
  delay(blink_delay / 4);
}

void lights_blink_3(int blink_delay) {
  lights_off();
  delay(blink_delay / 4);
  lights_on();
  delay(blink_delay / 4);
  lights_off();
  delay(blink_delay / 4);
  for (int i = 6; i < 16; i++) {
    digitalWrite(leds[i], HIGH);
  }
  delay(blink_delay / 4);
}

void lights_blink_4(int blink_delay) {
  lights_off();
  delay(blink_delay / 4);
  lights_on();
  delay(blink_delay / 4);
  lights_off();
  delay(blink_delay / 4);
  for (int i = 16; i < 21; i++) {
    digitalWrite(leds[i], HIGH);
  }
  delay(blink_delay / 4);
  lights_on();
}


void loop() {
  // Jingle Bells
  lights_on();
  lights_blink_1(tone_delay * 4 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 2);

  // Jingle Bells
  lights_blink_4(tone_delay * 2 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 2);

  // Jingle all the
  lights_blink_3(tone_delay * 2 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 0.5);

  // way
  lights_blink_3(tone_delay * 0.5 * 1.3); tone(SP,E, tone_delay * 4);

  // Oh what fun it
  lights_blink_4(tone_delay * 4 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 0.5);

  // is to ride in a
  lights_blink_4(tone_delay * 0.5 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 0.5);
  lights_blink_4(tone_delay * 0.5 * 1.3); tone(SP,E, tone_delay * 0.5);

  // One horse o-pen
  lights_blink_1(tone_delay * 0.5 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);

  // Sleigh
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 2);
  lights_blink_2(tone_delay * 2 * 1.3); tone(SP,G, tone_delay * 2);

  // Jingle Bells
  lights_blink_3(tone_delay * 2 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 2);

  // Jingle Bells
  lights_blink_2(tone_delay * 2 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 2);

  // Jingle all the
  lights_blink_1(tone_delay * 2 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 0.5);

  // way
  lights_blink_1(tone_delay * 0.5 * 1.3); tone(SP,E, tone_delay * 4);

  // Oh what fun it
  lights_blink_2(tone_delay * 4 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 0.5);

  // is to ride in a
  lights_blink_2(tone_delay * 0.5 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 0.5);
  lights_blink_2(tone_delay * 0.5 * 1.3); tone(SP,E, tone_delay * 0.5);

  // One horse o-pen
  lights_blink_3(tone_delay * 0.5 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);

  // Sleigh
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 4);

  // Dashing through the
  lights_blink_4(tone_delay * 4 * 1.3); tone(SP,G1, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);

  // Snow in a
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,G1, tone_delay * 2);
  lights_blink_1(tone_delay * 2 * 1.3); tone(SP,G1, tone_delay * 0.5);
  lights_blink_2(tone_delay * 0.5 * 1.3); tone(SP,G1, tone_delay * 0.5);

  // One horse o-pen
  lights_blink_3(tone_delay * 0.5 * 1.3); tone(SP,G1, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);

  // Sleigh
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,A1, tone_delay * 4);

  // Through the field we
  lights_blink_4(tone_delay * 4 * 1.3); tone(SP,A1, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);

  // Go
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,B1, tone_delay * 4);

  // Laughing all the
  lights_blink_1(tone_delay * 4 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);

  // way
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 4);

  // Bells on Bob tail
  lights_blink_2(tone_delay * 4 * 1.3); tone(SP,G1, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);

  // ring
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,A1, tone_delay * 4);

  // Making spirits
  lights_blink_3(tone_delay * 4 * 1.3); tone(SP,G1, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 1);

  // Bright What
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,A1, tone_delay * 2);
  lights_blink_4(tone_delay * 2 * 1.3); tone(SP,A1, tone_delay * 1);

  // Fun it is to
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,A1, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);

  // ride and sing a
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,G, tone_delay * 1);

  // Sleighing song to
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,A, tone_delay * 1);
  lights_blink_2(tone_delay * 1 * 1.3); tone(SP,F, tone_delay * 1);
  lights_blink_3(tone_delay * 1 * 1.3); tone(SP,E, tone_delay * 1);
  lights_blink_4(tone_delay * 1 * 1.3); tone(SP,D, tone_delay * 1);

  // Night
  lights_blink_1(tone_delay * 1 * 1.3); tone(SP,C, tone_delay * 4);
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.