Friday, December 11, 2015

Happy Hanukkah

Happy Hanukkah!!

I showed the Arduino to a friend of mine and she really liked it so we decided to build something for Hanukkah!
We built a playing Menorah, with led candles:



And here is the video (make sure you have your speakers on), it is playing a common Hanukkah song:


And here is the code:


#include <pitches.h>

int melody[] = {
  NOTE_A3, NOTE_FS3, NOTE_A3, 0, NOTE_A3, NOTE_FS3, NOTE_A3, 0, NOTE_FS3, NOTE_A3, NOTE_D4, NOTE_CS4, NOTE_B3, 0,
  NOTE_G3, NOTE_E3, NOTE_G3, 0, NOTE_G3, NOTE_E3, NOTE_G3, 0, NOTE_E3, NOTE_G3, NOTE_B3, NOTE_AS3, NOTE_A3, 0,
  NOTE_A3, NOTE_FS3, NOTE_A3, 0, NOTE_A3, NOTE_FS3, NOTE_A3, 0, NOTE_FS3, NOTE_A3, NOTE_D4, NOTE_CS4, NOTE_B3, 0,
  NOTE_CS4, NOTE_CS4, NOTE_CS4, 0, NOTE_CS4, NOTE_CS4, NOTE_CS4, 0, NOTE_CS4, NOTE_A3, NOTE_B3, NOTE_CS4, NOTE_D4, 0
};

int noteDurations[] = {
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3,
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3,
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3,
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3,
};

const int BLUE_PIN = 11;
const int GREEN_PIN = 12;
const int RED_PIN = 13;

const int d9 = 9;
const int d8 = 8;
const int d7 = 7;
const int d6 = 6;
const int d4 = 5;
const int d3 = 4;
const int d2 = 3;
const int d1 = 2;

int leds[] = {
  d1, d2, d3, d4, d6, d7, d8, d9, d1, d2, d3, d4, d6, d7,
  d8, d9, d1, d2, d3, d4, d6, d7, d8, d9, d1, d2, d3, d4,
  d6, d7, d8, d1, d2, d3, d4, d6, d7, d8, d9, d1, d2, d3,
  d4, d6, d7, d8, d9, d1, d2, d3, d4, d6, d7, d8, d9, d1,
  d2, d3, d4, d6, d7, d8
};

const int buzzer = 10;

bool played = false;

int DISPLAY_TIME = 10;  // In milliseconds

void setup()
{

  Serial.begin(9600);
 
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

  pinMode(d9, OUTPUT);
  pinMode(d8, OUTPUT);
  pinMode(d7, OUTPUT);
  pinMode(d6, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d1, OUTPUT);
 
  pinMode(buzzer, OUTPUT);
}

void loop()
{
  digitalWrite(d9, LOW);
  digitalWrite(d8, LOW);
  digitalWrite(d7, LOW);
  digitalWrite(d6, LOW);
  digitalWrite(d4, LOW);
  digitalWrite(d3, LOW);
  digitalWrite(d2, LOW);
  digitalWrite(d1, LOW);

//  digitalWrite(d9, HIGH);
//  digitalWrite(d8, HIGH);
//  digitalWrite(d7, HIGH);
//  digitalWrite(d6, HIGH);
//  digitalWrite(d4, HIGH);
//  digitalWrite(d3, HIGH);
//  digitalWrite(d2, HIGH);
//  digitalWrite(d1, HIGH);

 
  //mainColors();
 
  //showSpectrum();

  if(!played){
    playMusic();
  }
 
  lightCandles(6);

}

void lightCandles(int num){
  for(int i=1; i<=num; i++){
    digitalWrite(i+1, HIGH);
  }
  mainColors();
}

void onLeds(int leds[]){
  digitalWrite(leds[0], HIGH);
  digitalWrite(leds[1], HIGH);
}

void offLeds(int leds[]){
  digitalWrite(leds[0], LOW);
  digitalWrite(leds[1], LOW);
}

void playMusic(){
  for (int thisNote = 0; thisNote < 56; thisNote++) {
    int color1 = random(766);
    showRGB(color1);
   
    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzer, melody[thisNote], noteDuration);
   
    //Light the candles
    int m = (thisNote) % 4;
    int leds[2];
    switch (m)
    {
      case 0:
        leds[0] = 2;
        leds[1] = 9;
        break;
      case 1:
        leds[0] = 3;
        leds[1] = 8;
        break;
      case 2:
        leds[0] = 4;
        leds[1] = 7;
        break;
      case 3:
        leds[0] = 5;
        leds[1] = 6;
        break;
      default:
        // if nothing else matches, do the default
        // default is optional
      break;
    }
    //digitalWrite(leds[thisNote], HIGH);
    onLeds(leds);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(buzzer);
    //digitalWrite(leds[thisNote], LOW);
    offLeds(leds);
    turnOffRGB();
  }
  played = true;
}

void turnOffRGB(){
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
}

void mainColors()
{
  //Serial.write("mainColors");
 
  // Off (all LEDs off):

//  digitalWrite(RED_PIN, LOW);
//  digitalWrite(GREEN_PIN, LOW);
//  digitalWrite(BLUE_PIN, LOW);
 
 // delay(1000);
 
  // Red (turn just the red LED on):
 
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
 
  delay(1000);
 
  // Green (turn just the green LED on):
 
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
 
  delay(1000);
 
  // Blue (turn just the blue LED on):
 
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
 
  delay(1000);
 
  // Yellow (turn red and green on):
 
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
 
  delay(1000);
 
  // Cyan (turn green and blue on):
 
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
 
  delay(1000);
 
  // Purple (turn red and blue on):
 
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
 
  delay(1000);
 
  // White (turn all the LEDs on):
 
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
 
  delay(1000);
}

void showSpectrum()
{
  int x;  // define an integer variable called "x"
  for (x = 0; x < 768; x++)
  {
    showRGB(x);  // Call RGBspectrum() with our new x
    delay(DISPLAY_TIME);   // Delay for 10 ms (1/100th of a second)
  }
}


// showRGB()

// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.

// The "base" numbers are:
// 0   = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

void showRGB(int color)
{
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  if (color <= 255)          // zone 1
  {
    redIntensity = 255 - color;    // red goes from on to off
    greenIntensity = color;        // green goes from off to on
    blueIntensity = 0;             // blue is always off
  }
  else if (color <= 511)     // zone 2
  {
    redIntensity = 0;                     // red is always off
    greenIntensity = 255 - (color - 256); // green on to off
    blueIntensity = (color - 256);        // blue off to on
  }
  else // color >= 512       // zone 3
  {
    redIntensity = (color - 512);         // red off to on
    greenIntensity = 0;                   // green is always off
    blueIntensity = 255 - (color - 512);  // blue on to off
  }

  // Now that the brightness values have been set, command the LED
  // to those values
 
  analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
  analogWrite(GREEN_PIN, greenIntensity);
}


Have fun :)

No comments:

Post a Comment