Ian Cumming
Published © LGPL

Musical Fairy Lights

It's almost the festive season and numbers of shops have started putting out their festive decorations, I thought that it’s about the right.

BeginnerFull instructions provided1.5 hours12,109

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
ULN2308
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Fritzing file

Fritzing File includes breadboard and schematics

Schematics

Breadboard View

Breadboard view of the Circuit

Schematics

Schematics of the Fairy Lights circuit

Code

Arduino Fairy Lights Code

Arduino
Upload it to your Arduino
/*
  Jingle Bell Lights
  Fairy Lights with Jingle Bells as a tune.
  By Ian Cumming https://www.hackster.io/ianabcumming/
  
  H/W: 4(or more) x LED's, 4(or more) x 220OHM resistors, ULN2803A, Piezo Speaker, Arduino UNO. 
*/

//GLOBAL VARS
 int barTime = 1200;                                     // 8/8 = 1000ms
byte nrLEDS = 4;                                         // 4 Base LEDs, You can add many in parallel
byte leds[] = { 3, 4, 5, 6};
byte speaker = 11;

byte patternLength = 64;                                 // pattern Length
byte songLength = 51;

const byte song[] = {                                    // Jingle Bells Data
  29, 2,29, 2,29, 4,29, 2,29, 2,29, 4,29, 2,32, 2,25, 3,27, 1,29, 8,30, 2,30, 2,
  30, 3,30, 1,30, 2,29, 2,29, 2,29, 1,29, 1,29, 2,27, 2,27, 2,29, 2,27, 4,32, 4,
  29, 2,29, 2,29, 4,29, 2,29, 2,29, 4,29, 2,32, 2,25, 3,27, 1,29, 8,30, 2,30, 2,
  30, 2,30, 2,30, 2,29, 2,29, 2,29, 1,29, 1,32, 2,32, 2,30, 2,27, 2,25, 8
}
;                                                        // The Structure is Note Number then 
                                                         // NoteLength in 8th's 
const byte pattern[] = {                                 //Pattern Data 
  0b0001,0b0010,0b0100,0b1000,                           //Bits corrispond to Leds in Array
  0b0001,0b0010,0b0100,0b1000,
  0b0001,0b0010,0b0100,0b1000,
  0b0001,0b0010,0b0100,0b1000,

  0b1000,0b1100,0b0100,0b0110,
  0b0010,0b0011,0b0001,0b1001,
  0b1000,0b1100,0b0100,0b0110,
  0b0010,0b0011,0b0001,0b1001,

  0b1010,0b0101,0b1010,0b0101,
  0b1010,0b0101,0b1010,0b0101,
  0b1010,0b0101,0b1010,0b0101,
  0b1010,0b0101,0b1010,0b0101,
  
  0b1100,0b0011,0b1100,0b0011,
  0b1100,0b0011,0b1100,0b0011,
  0b1100,0b0011,0b1100,0b0011,
  0b1100,0b0011,0b1100,0b0011,
  };

//FUNCTIONS 
 int noteToHz(int note) {                                 // Convert a Note Nr. to Frequency
  float freq = 440 * (pow(1.059463094359,note-21));       // -21 gives you note 1 at C3 (I Think)
  return int(freq);                                       // Results are accurate to 1hz
}

void lightLEDs(byte PORT_X) {                             // Control LED's State
  for (int q = 0;q<nrLEDS;q++) {
    digitalWrite(leds[q],bitRead(PORT_X,q));
  }
}


void setup() { //setup OUTPUT pins
  pinMode(speaker,OUTPUT);
  for(int t = 0; t < nrLEDS; t++){
    pinMode(leds[t],OUTPUT);
  }
}

void loop() {
  // Music Loop
  for (int t = 0; t < songLength; t++) {
    // Notes, Length and play the melody
    int note = noteToHz(song[t * 2]);
    int length = ((song[(t * 2)+1] * barTime) / 8);
    tone(speaker, note,  length - 50);
    
    // Flashing Lights!
    lightLEDs(pattern[t % patternLength]);
    delay(length);
  }
  
  // Silence Loop
  int randomSilence = random(1000,5000);
  for (int t = 0;t<randomSilence;t++){
    lightLEDs(pattern[t % patternLength]);  
    delay(barTime/4);
  }
}

Credits

Ian Cumming

Ian Cumming

6 projects • 71 followers
I like stuff, from building rockets to elctronics.

Comments