Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Clock plays Happy Birthday
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Clock plays Happy Birthday

by mike31416 on Thu Dec 03, 2009 6:27 pm

I made a change to my clock to play the happy birthday song on a given birthday. There is a list of name, day and month which is scanned on the hour between 8am and 8pm. If it finds a match, it displays the name and plays the birthday song.

Here is the data:
Code: Select all | TOGGLE FULL SIZE
// note values
#define A     14080
#define AS    14917
#define B     15804
#define C     16744
#define CS    17740
#define D     18795
#define DS    19912
#define E     21096
#define F     22351
#define FS    23680
#define G     25088
#define GS    26580
#define A2    28160
#define A2S   29835
#define B2    31609
#define C2    33488
#define C2S   35479
#define D2    37589
#define D2S   39824
#define E2    42192
#define F2    44701
#define F2S   47359
#define G2    50175
#define G2S   53159
#define A3    56320

//octaves
#define oct8 4
#define oct7 8
#define oct6 16
#define oct5 32
#define oct4 64
#define oct3 128
#define oct2 256
#define oct1 512
#define oct0 1024

const uint16_t birthdaySong[] PROGMEM =
{
G, 0, G, A2, 0, 0, G, 0, 0, C2, 0, 0, B2, 0, 0, 0, 0, 0, G, 0, G, A2, 0, 0,
G, 0, 0, D2, 0, 0, C2, 0, 0, 0, 0, 0, G, 0, G, G2, 0, 0, E2, 0, 0, C2,
0, 0, B2, 0, 0, A2, 0, 0, F2, 0, F2, E2, 0, 0, C2, 0, 0, D2, 0, 0, C2
};

PGM_P birthdaySong_p PROGMEM = (PGM_P)birthdaySong;

struct BIRTHDAY_LIST
{
   char* name;
   uint8_t month;
   uint8_t day;
};

struct BIRTHDAY_LIST birthdayList[] =
{
      { "bill"    , 3,  5  },
      { "michael" , 3,  29 },
      { "mom"     , 4,  6  }
};



Here is the code:
Code: Select all | TOGGLE FULL SIZE
// frequency is in Hz, delay in ms
void playNote(uint16_t freq, int delay)
{

   // set the PWM output to match the desired frequency
   ICR1 = (F_CPU / 8) / freq;
   // we want 50% duty cycle square wave
   OCR1A = OCR1B = ICR1 / 2;

   TCCR1B |= _BV(CS11); // turn it on!
   delayms(delay);
   TCCR1B &= ~_BV(CS11); // turn it off!

   // turn speaker off
   PORTB &= ~_BV(PORTB1) & ~_BV(PORTB2);
}

void playSong(void)
{
   int x;
   uint16_t noteval;

   for (x = 0; x < sizeof(birthdaySong)/sizeof(uint16_t); x++)
   {
      kickthedog();
      noteval = (pgm_read_word(birthdaySong_p + (x * sizeof(uint16_t))) / oct4);

      if (noteval > 0)
      {
         playNote(noteval, 200);
      }
      else
      {
         delayms(200);
      }
   }
}

void birthday(void)
{
   uint8_t i;

   if (time_s || time_m || (time_h < 8 || time_h > 20))
   {
      return;
   }

   for (i = 0; i < sizeof(birthdayList)/sizeof(struct BIRTHDAY_LIST); i++)
   {
      if (date_m == birthdayList[i].month && date_d == birthdayList[i].day)
      {
         displaymode = NONE;
         display_str(birthdayList[i].name);
         playSong();
         displaymode = SHOW_TIME;
         break;
      }
   }
}


I put the call to birthday() in main right before the buttons are checked for the menu:
Code: Select all | TOGGLE FULL SIZE
      if (ACSR & _BV(ACO))
      {
         // DEBUGP("SLEEPYTIME");
         gotosleep();
         continue;
      }
      //DEBUGP(".");

      //  Check if it is a birthday
      birthday();

      if (just_pressed & 0x1)
      {



I got a clock for my brother for Christmas and made this change with all of the family birthdays. I am not going to tell him that it has this feature. It will be fun to have him find it! Maybe now he will remember my birthday!!!

Some future changes could be:
- Add a menu item to set the hours when its enabled to play
- Turn it off for the remainder of the day
- Add other calendar events

Mike

mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Clock plays Happy Birthday

by adafruit on Thu Dec 03, 2009 6:35 pm

so cute. i hope you did testing :)

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Clock plays Happy Birthday

by mike31416 on Fri Dec 04, 2009 7:33 am

Yep, tried a few of the dates I have configured. But you never know... The only perfect software is no software.

mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Clock plays Happy Birthday

by guillermo3r on Sat May 22, 2010 3:21 am

Hi,
I would like to do this hack but I have a doubt. In what part of the iv.c firmware should I include the data and the code created by mike31416?
Thanks :D

guillermo3r
 
Posts: 17
Joined: Wed Sep 23, 2009 3:42 am

Re: Clock plays Happy Birthday

by mike31416 on Tue May 25, 2010 6:31 am

Hi guillermo3r,

I inserted the data after uint16_t snoozetimer = 0; The code for the functions playNote(), playSong() and birthday() can be be anywhere before the call to main() in iv.c. If you put the birthday() function after the body of main(), you will need to add a function declaration near the top of iv.c before you use it in main(). For example you could put this line: void birthday(void); after the struct BIRTHDAY_LIST birthdayList[] = {}; body.

I put the call to birthday() in main right before the buttons are checked for the menu as shown in my post.

Since I made this post I have made two other changes. I now call the birthday() function after the date is shown when you press the set button button (code is at the end of main). I also turn on the right most decimal point if the current day is a birthday. This way I will see that it is a birthday even if I am not near the clock when it plays the song on the hour (8am-8pm). If I see the decimal, I then press the set button and the birthday song will play and show the name of the person.

I do not have the code with me right now and will post the changes later tonight.

Hope this helps!
Mike

mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Clock plays Happy Birthday

by guillermo3r on Tue May 25, 2010 2:08 pm

I am looking forrward to see all the changes :D

guillermo3r
 
Posts: 17
Joined: Wed Sep 23, 2009 3:42 am

Re: Clock plays Happy Birthday

by mike31416 on Wed May 26, 2010 7:18 am

The attached file has a copy of iv.c with the birthday mod. You can find the changes by searching for #ifdef BIRTHDAY_FEATURE.

Have fun!
Mike
Attachments
iv_bday.zip
Look for BIRTHDAY_FEATURE to find needed code changes.
(11.29 KiB) Downloaded 150 times

mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Please be positive and constructive with your questions and comments.