BirthdayCard

Posted by yoon

yoon's picture
  • Components Used: LEDs, photocell, piezo speaker

 

  • Description:

Photocell detects the light density so that it tells that whether the card is open or not. When the card is open, it plays birthdaysong and blinks candles in a card. This birthdaycard can be for either 3-year-old kid or 30-year-old guy!

 

  • Arduino Code: I modified few things from piezo speaker example code. key b to b flat, and put LED blinking part into the playing song part so that they happen simultaneously, while it keeps checking whether the card is open or not. 

 

int color[] = { 9,10,11};  
int Photocell = 1;
int speakerOut = 7;               
byte names[] = {
  'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {
  1915, 1700, 1519, 1432, 1275, 1136, 1090, 956}; //changed b value to b flat
byte melody[] = "2c2c4d4c5f9e2c2c4d4c5g9f2c2c6C6a5f5e8d3b2b6a5f5g9f";
                          
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 30;
int statePin = LOW;
int i =0; //LED
 
uint8_t cardOpen = 0;
 
void setup() {
  for (int i = 0; i<=2; i++){
    pinMode(color[i], OUTPUT);  
  }
  pinMode(speakerOut, OUTPUT);
  pinMode(Photocell, INPUT);
  Serial.begin(115200);
}
 
void loop() {
 
  checkSensor();
  if (cardOpen) {
    song();
    checkSensor();
  }
}
 
void checkSensor (){
  if(analogRead(Photocell) > 300){
    cardOpen = 1;
   // Serial.println("Opened.");
  }
  else {
    cardOpen = 0;
    //Serial.println("Closed.");
  }
}
 
 
 
void song() { 
 
  digitalWrite(speakerOut, LOW);     
  for (count = 0; count < MAX_COUNT; count++) {
    checkSensor();
    if(cardOpen == 0){
      break;
    }
    statePin = !statePin;
    i = count%3;
    analogWrite(color[i], 255);
    delay(20);  
    checkSensor();
    if(cardOpen == 0){
      break;
    }
 
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
      }
      analogWrite(color[i], 0);
    }
  }
}
0
Your rating: None