Sunday 16 December 2018

Arduino Xmas Lights with songs


Video: 



Parts: (all parts can be bought from Jaycar or ebay/amazon etc.)

3x  IRLZ34N Mosfet's
3x 10K Resistors
1x Arduino Uno/Nano etc
12v Power Supply
1x Roll RGB Led strip, 5M.
1x Piezo speaker


Wiring Diagram


Code:


//led defines and variables
#define RED_PIN 6
#define GREEN_PIN 9
#define BLUE_PIN 5
int RM = 1, GM = 1, BM = 1;
int R = 0, G = 0, B = 0;
int ColountCount = 0;


//music defines and veriables
#define SpeakerPin 12 //change to suit the speaker out wiring
int duration = 0;
char notes[] = "AA";
int beats[] = {1, 1};
int tempo = 165;
char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 958, 852, 758 };
void setup()
{
  pinMode(SpeakerPin, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
  Song4(); Song5();
  Song6(); Song7(); Song8(); Song9();
  Song10();
}

void Song4() {
  int duration = 69;
  char notes[] = "ddaagfedcdefga ddaagfedcdefga avgavCDagfdefgfgavaagfedfedgfgavCDagfed";
  int beats[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 1, 1, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}
void Song5() {
  int duration = 71;
  char notes[] = "cfffgagavCavafggfcfffgagavCavafggffaCDCvagfgavCcfagfccfffgagavCavafggf ";
  int beats[] = { 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song6() {
  int duration = 29;
  char notes[] = "faagfvvagaCCbCaDCvagfeagffef ";
  int beats[] = { 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song7() {
  int duration = 51;
  char notes[] = "aavCagfgagdgavCaggfgagff vavCDDaaCagfecavCagfgagff ";
  int beats[] = { 2, 3, 1, 2, 2, 4, 4, 3, 1, 2, 2, 8, 3, 1, 2, 2, 3, 1, 4, 2, 2, 3, 1, 6, 2, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 4, 4, 2, 2, 3, 1, 8, 8};
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song8() {
  int duration = 77;
  char notes[] = "cffefaagCCCvagacffefaagCffeedcCCCfvaagCCCfvaagDDDCvavgavCffgaDDDCvavgavCffgf ";
  int beats[] = {2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 3, 1, 2, 2, 4, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 3, 1, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 1, 1, 3, 1, 2, 2, 4, 3, 1, 2, 2, 2, 2, 4, 2, 1, 1, 3, 1, 2, 2, 4, 8};
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

void Song9() {
  int duration = 64;
  char notes[] = "ggdgadbabCbaggsesgabsedd DCbCbabgasedggsgagdbbabCbabCbagsgCbagg ";
  int beats[] = { 2, 4, 2, 2, 4, 4, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 3, 1, 6, 2, 4, 2, 2, 4, 4, 2, 2, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 3, 1, 6, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}
void Song10() {
  int duration = 63;
  char notes[] = "egbbbaCbagabgegasedeaaeesgsedgabbbaCbag DD bb baCdagabgegasede ";
  int beats[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 4, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 8 };
  for (int i = 0; i < duration; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);
    }
    else {
      playNote(notes[i], beats[i] * tempo);
    }
    PulseLed();
  }
}

int colourcount = 1;
void PulseLed() {
  colourcount++;
  if (colourcount > 3)colourcount = 1;

  if (colourcount == 1) {
    analogWrite(RED_PIN, 255);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 0);
  }
  if (colourcount == 2) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 255);
    analogWrite(BLUE_PIN, 0);
  }
  if (colourcount == 3) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 255);
  }


}

void playNote(char note, int duration) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(SpeakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(SpeakerPin, LOW);
    delayMicroseconds(tone);
  }
}

1 comment:

  1. Iron Man - Thor is Titanium a Metal / Stainless - TheTianium
    Iron Man - aftershokz titanium Thor is Titanium a metal apple watch stainless steel vs titanium / stainless-steel / stainless-steel / silver metal oxide. It is made of titanium oxide (molymium), silver oxide, zinc titanium granite countertops oxide, Material: Aluminum,Iron,‎Metal · titanium nose rings ‎Thinning iron titanium token Blade

    ReplyDelete