Christmas Caroling Device with Light Display
Jessica Uelmen
Posts: 490
Christmas Caroling Device with Light Display
·
Bring the spirit of the holidays with you anywhere using this DIY caroling device and light display! This activity demonstrates how to program four different Christmas songs to play depending on the position selected on the 5-Position Switch.· While each song is playing, red and green LEDs will blink a light pattern, unique to each song.
·
Download Source Code
Getting Started
If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before beginning:
·
√······· Complete Chapters 1-3 in What’s a Microcontroller
√······· Review the Stamps in Class “Mini Project”: Playing Sheet Music with the Piezospeaker
√······· Review the 5-Position Switch Documentation
Parts Required
(1) HomeWork Board with BASIC Stamp 2
The BASIC Stamp 2 Board of Education is also suitable for this activity.
(5) 470 Ω Resistors
(3) Green LEDs
(2) Red LEDs
(1) Piezospeaker
(1) 5-Position Switch
Schematics and Building the Circuits
Figure 1 is the schematic and Figure 2 is the wiring diagram for this circuit. ·Build your circuit as shown below.
·
Figure 1 – Christmas Caroler Schematic
Figure 2 – Christmas Caroler Wiring Diagram
Testing the Circuit
Once everything is wired, it’s good to be sure that everything is wired correctly. That way, you will always know that your circuit is functional, and won’t waste time trying to correct perfectly good code when the problem really lies in your wiring.· Run TestCarolingDevice.bs2 and make sure that:
·
√······· All LEDs turn on
√······· The piezospeaker plays a tone
√······· All 5 positions (up, down, left, right, and pressed) display on the Debug Terminal
Choosing Your Tunes
Now that we know the circuit is working, it’s time to pick the songs that we want our Caroling Device to play.· In this project, we’re going to use four of the five positions to play four different songs, and use the fifth position to stop playing a song when user desires.· For this application, four copyright-friendly songs have been chosen: We Wish You a Merry Christmas, Jingle Bells, Deck the Halls, and O Christmas Tree.
·
Once the four songs have been chosen, they should be coded in four separate programs.· This will make sure that they sound right before porting them to the main program.· For the sake of space, the code for each individual song is not listed below, but they can be downloaded from the ChristmasCarols_SourceCode.zip file attached below.
·
Time to Spread Some Holiday Cheer!
At this point, we’ve got our songs fully coded, our circuit works, and we’re ready to put everything together.· By now you may have guessed that this program is going to be rather large, and is going to take up a lot of EEPROM space.· So it’s our job to make sure we make the most of what we have.
·
The first EEPROM-Eater is the space required to store the information needed to play all four songs. ·This requires four DATA directives for each song – one to store the notes, one to store the octaves, one to store the durations, and to store the dotted notes.· Is there anything we can do to eliminate some of this space?
·
Why yes, there is!· When looking at the sheet music for Jingle Bells and We Wish You a Merry Christmas, I noted that there were very few to no dotted notes.· So instead of taking up EEPROM space to save 1-2 dotted notes in a whole song, why not just increase the value in the Durations DATA directive?· It won’t be as accurate, but as long as it sounds OK, that’s what counts!
·
In order to further save memory, we’ll extensively use subroutines so we’re not repeating the same lines of code unnecessarily.· The main part of the program will branch off to four separate subroutines to play four separate songs, based on which position on the 5-Position Switch is selected:
·
DO
· IF (IN0 = 0) THEN
··· GOSUB Deck_The_Halls
· ELSEIF (IN1 = 0) THEN
··· GOSUB Jingle_Bells
· ELSEIF (IN2 = 0) THEN
··· GOSUB Merry_Christmas
· ELSEIF (IN4 = 0) THEN
··· GOSUB Christmas_Tree
· ELSE
··· PAUSE 20
· ENDIF
LOOP
·
Each song’s subroutine will then have the same basic structure: READ the note letter, octave, duration, and dot notation from its appropriate location in EEPROM and then calculate the values needed for the BASIC Stamp’s FREQOUT command.· Most of these calculations can be carried out through the use of more subroutines to keep on savin’ that memory.· For example, the LOOKUP and LOOKDOWN tables for calculating each note’s frequency will be the same for each song, and therefore can be placed in a subroutine named Get_Frequency:
·
Get_Frequency:
· LOOKDOWN noteLetter, [noparse][[/noparse]· "C",· "d",· "D",· "e",· "E",
························· "F",· "g",· "G",· "a",· "A",
························· "b",· "B",· "R",· "Q"···· ], offset
·
· LOOKUP offset,······· [noparse][[/noparse] 4186, 4435, 4699, 4978, 5274,
························· 5588, 5920, 6272, 6645, 7040,
························· 7459, 7902,··· 0,··· 0···· ], noteFreq
RETURN
·
Likewise, re-calculating a note’s frequency so that it is in the proper octave can also be placed in a subroutine named Get_Octave:
·
Get_Octave:
· noteOctave = 8 - noteOctave
· noteFreq = noteFreq / (DCD noteOctave)
RETURN
·
Next, we’ll have to determine what the light pattern will be displayed while each song is playing.· An easy way to do this is simply to assign certain LEDs to turn on when certain notes are being played.· Since the notes in each song differ from one to the other, we can ensure that the pattern of lights will be different for each song all while calling the same subroutine, Display_Color.· You may notice that the first part of this subroutine turns all of the LEDs off.· This ensures that any LED that was on for the last note will be off for current note, creating a “blinking” effect each time!
·
Display_Color:
· LOW 15: LOW 14: LOW 13: LOW 12: LOW 11
· SELECT noteLetter
··· CASE "A", "a"
····· HIGH 15
··· CASE "B", "b"
····· HIGH 14
··· CASE "C", "c"
····· HIGH 13
··· CASE "D", "d"
····· HIGH 12
··· CASE "E", "e"
····· HIGH 11
··· CASE "F", "f"
····· HIGH 15
····· HIGH 13
····· HIGH 11
··· CASE "G", "g"
····· HIGH 14
····· HIGH 12
·· ENDSELECT
RETURN
·
·
Now there are two last things to consider before we can run the whole program.· First, we need to reset the index variable to 0 before playing any song.· This variable acts as a pointer to specific address locations in EEPROM, and if it wasn’t reset before each song was played, some songs might start playing from the middle, or some not at all!
·
Lastly, at the end of playing each song, we need to reset the value of noteLetter.· This is because our songs will only play if noteLetter doesn’t equal “Q” or if the center position on the 5-Position Switch is not pressed.· BUT every string of notes saved to EEPROM ends with “Q”, and if this is not reset, then the songs won’t play a second time.· In the code below, noteLetter is simply set to “R” at the end of each subroutine to prevent this from happening.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax Inc.
Post Edited (Jessica Uelmen (Parallax)) : 8/25/2010 6:37:22 PM GMT
·
Bring the spirit of the holidays with you anywhere using this DIY caroling device and light display! This activity demonstrates how to program four different Christmas songs to play depending on the position selected on the 5-Position Switch.· While each song is playing, red and green LEDs will blink a light pattern, unique to each song.
·
Download Source Code
Getting Started
If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before beginning:
·
√······· Complete Chapters 1-3 in What’s a Microcontroller
√······· Review the Stamps in Class “Mini Project”: Playing Sheet Music with the Piezospeaker
√······· Review the 5-Position Switch Documentation
Parts Required
(1) HomeWork Board with BASIC Stamp 2
The BASIC Stamp 2 Board of Education is also suitable for this activity.
(5) 470 Ω Resistors
(3) Green LEDs
(2) Red LEDs
(1) Piezospeaker
(1) 5-Position Switch
Schematics and Building the Circuits
Figure 1 is the schematic and Figure 2 is the wiring diagram for this circuit. ·Build your circuit as shown below.
·
Figure 1 – Christmas Caroler Schematic
Figure 2 – Christmas Caroler Wiring Diagram
Testing the Circuit
Once everything is wired, it’s good to be sure that everything is wired correctly. That way, you will always know that your circuit is functional, and won’t waste time trying to correct perfectly good code when the problem really lies in your wiring.· Run TestCarolingDevice.bs2 and make sure that:
·
√······· All LEDs turn on
√······· The piezospeaker plays a tone
√······· All 5 positions (up, down, left, right, and pressed) display on the Debug Terminal
[color=#008000]' TestCarolingDevice.bs2[/color] [color=#008000]' Tests that the Caroling Device is wired correctly.[/color] [color=#008000]' {$STAMP BS2}[/color] [color=#008000]' {$PBASIC 2.5}[/color] [color=#020FC0]HIGH[/color][color=#000000] 15 [/color][color=#008000]' Turn on all LEDs[/color] [color=#020FC0]HIGH[/color][color=#000000] 14[/color] [color=#020FC0]HIGH[/color][color=#000000] 13[/color] [color=#020FC0]HIGH[/color][color=#000000] 12[/color] [color=#020FC0]HIGH[/color][color=#000000] 11[/color] [color=#020FC0]FREQOUT[/color][color=#000000] 8, 2000, 3000 [/color][color=#008000]' Play a tone[/color] [color=#020FC0]DEBUG[/color][color=#000000] CLS, [/color][color=#008000]' Display directional graph[/color] [color=#ff0000]" "[/color][color=#000000], [/color][color=#800080]CR[/color][color=#000000],[/color] [color=#ff0000]" | "[/color][color=#000000], [/color][color=#800080]CR[/color][color=#000000],[/color] [color=#ff0000]" - - "[/color][color=#000000], [/color][color=#800080]CR[/color][color=#000000],[/color] [color=#ff0000]" |[/color] [color=#ff0000]"[/color][color=#000000], [/color][color=#800080]CR[/color] [color=#008000]' Display which direction is pressed on the 5-Position Switch[/color] [color=#020FC0]DO[/color] [color=#020FC0]IF[/color][color=#000000] ([/color][color=#800080]IN0[/color][color=#000000] = 0) [/color][color=#020FC0]THEN[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 2, 2, [/color][color=#ff0000]"<"[/color] [color=#020FC0]ELSE[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 2, 2, [/color][color=#ff0000]" "[/color] [color=#020FC0]IF[/color][color=#000000] ([/color][color=#800080]IN1[/color][color=#000000] = 0) [/color][color=#020FC0]THEN[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 4, [/color][color=#ff0000]"v"[/color] [color=#020FC0]ELSE[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 4, [/color][color=#ff0000]" "[/color] [color=#020FC0]IF[/color][color=#000000] ([/color][color=#800080]IN2[/color][color=#000000] = 0) [/color][color=#020FC0]THEN[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 8, 2, [/color][color=#ff0000]">"[/color] [color=#020FC0]ELSE[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 8, 2, [/color][color=#ff0000]" "[/color] [color=#020FC0]IF[/color][color=#000000] ([/color][color=#800080]IN3[/color][color=#000000] = 0) [/color][color=#020FC0]THEN[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 2, [/color][color=#ff0000]"o"[/color] [color=#020FC0]ELSE[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 2, [/color][color=#ff0000]" "[/color] [color=#020FC0]IF[/color][color=#000000] ([/color][color=#800080]IN4[/color][color=#000000] = 0) [/color][color=#020FC0]THEN[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 0, [/color][color=#ff0000]"^"[/color] [color=#020FC0]ELSE[/color] [color=#020FC0]DEBUG[/color] [color=#800080]CRSRXY[/color][color=#000000], 5, 0, [/color][color=#ff0000]" "[/color] [color=#020FC0]PAUSE[/color][color=#000000] 20[/color] [color=#020FC0]LOOP[/color]
Choosing Your Tunes
Now that we know the circuit is working, it’s time to pick the songs that we want our Caroling Device to play.· In this project, we’re going to use four of the five positions to play four different songs, and use the fifth position to stop playing a song when user desires.· For this application, four copyright-friendly songs have been chosen: We Wish You a Merry Christmas, Jingle Bells, Deck the Halls, and O Christmas Tree.
·
Once the four songs have been chosen, they should be coded in four separate programs.· This will make sure that they sound right before porting them to the main program.· For the sake of space, the code for each individual song is not listed below, but they can be downloaded from the ChristmasCarols_SourceCode.zip file attached below.
·
Time to Spread Some Holiday Cheer!
At this point, we’ve got our songs fully coded, our circuit works, and we’re ready to put everything together.· By now you may have guessed that this program is going to be rather large, and is going to take up a lot of EEPROM space.· So it’s our job to make sure we make the most of what we have.
·
The first EEPROM-Eater is the space required to store the information needed to play all four songs. ·This requires four DATA directives for each song – one to store the notes, one to store the octaves, one to store the durations, and to store the dotted notes.· Is there anything we can do to eliminate some of this space?
·
Why yes, there is!· When looking at the sheet music for Jingle Bells and We Wish You a Merry Christmas, I noted that there were very few to no dotted notes.· So instead of taking up EEPROM space to save 1-2 dotted notes in a whole song, why not just increase the value in the Durations DATA directive?· It won’t be as accurate, but as long as it sounds OK, that’s what counts!
·
In order to further save memory, we’ll extensively use subroutines so we’re not repeating the same lines of code unnecessarily.· The main part of the program will branch off to four separate subroutines to play four separate songs, based on which position on the 5-Position Switch is selected:
·
DO
· IF (IN0 = 0) THEN
··· GOSUB Deck_The_Halls
· ELSEIF (IN1 = 0) THEN
··· GOSUB Jingle_Bells
· ELSEIF (IN2 = 0) THEN
··· GOSUB Merry_Christmas
· ELSEIF (IN4 = 0) THEN
··· GOSUB Christmas_Tree
· ELSE
··· PAUSE 20
· ENDIF
LOOP
·
Each song’s subroutine will then have the same basic structure: READ the note letter, octave, duration, and dot notation from its appropriate location in EEPROM and then calculate the values needed for the BASIC Stamp’s FREQOUT command.· Most of these calculations can be carried out through the use of more subroutines to keep on savin’ that memory.· For example, the LOOKUP and LOOKDOWN tables for calculating each note’s frequency will be the same for each song, and therefore can be placed in a subroutine named Get_Frequency:
·
Get_Frequency:
· LOOKDOWN noteLetter, [noparse][[/noparse]· "C",· "d",· "D",· "e",· "E",
························· "F",· "g",· "G",· "a",· "A",
························· "b",· "B",· "R",· "Q"···· ], offset
·
· LOOKUP offset,······· [noparse][[/noparse] 4186, 4435, 4699, 4978, 5274,
························· 5588, 5920, 6272, 6645, 7040,
························· 7459, 7902,··· 0,··· 0···· ], noteFreq
RETURN
·
Likewise, re-calculating a note’s frequency so that it is in the proper octave can also be placed in a subroutine named Get_Octave:
·
Get_Octave:
· noteOctave = 8 - noteOctave
· noteFreq = noteFreq / (DCD noteOctave)
RETURN
·
Next, we’ll have to determine what the light pattern will be displayed while each song is playing.· An easy way to do this is simply to assign certain LEDs to turn on when certain notes are being played.· Since the notes in each song differ from one to the other, we can ensure that the pattern of lights will be different for each song all while calling the same subroutine, Display_Color.· You may notice that the first part of this subroutine turns all of the LEDs off.· This ensures that any LED that was on for the last note will be off for current note, creating a “blinking” effect each time!
·
Display_Color:
· LOW 15: LOW 14: LOW 13: LOW 12: LOW 11
· SELECT noteLetter
··· CASE "A", "a"
····· HIGH 15
··· CASE "B", "b"
····· HIGH 14
··· CASE "C", "c"
····· HIGH 13
··· CASE "D", "d"
····· HIGH 12
··· CASE "E", "e"
····· HIGH 11
··· CASE "F", "f"
····· HIGH 15
····· HIGH 13
····· HIGH 11
··· CASE "G", "g"
····· HIGH 14
····· HIGH 12
·· ENDSELECT
RETURN
·
·
Now there are two last things to consider before we can run the whole program.· First, we need to reset the index variable to 0 before playing any song.· This variable acts as a pointer to specific address locations in EEPROM, and if it wasn’t reset before each song was played, some songs might start playing from the middle, or some not at all!
·
Lastly, at the end of playing each song, we need to reset the value of noteLetter.· This is because our songs will only play if noteLetter doesn’t equal “Q” or if the center position on the 5-Position Switch is not pressed.· BUT every string of notes saved to EEPROM ends with “Q”, and if this is not reset, then the songs won’t play a second time.· In the code below, noteLetter is simply set to “R” at the end of each subroutine to prevent this from happening.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax Inc.
Post Edited (Jessica Uelmen (Parallax)) : 8/25/2010 6:37:22 PM GMT
Comments
·
_____________________________________________________________________________
··
(c) 2009·by Parallax Inc - all rights reserved.··
Post Edited (Jessica Uelmen (Parallax)) : 12/4/2009 7:03:58 PM GMT
Super job.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Whit+
"We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney
I'll think I will do this for my 15" tall tree that sits at my desk. Ought to go good with all the miniature Looney-toon ornaments. I'll see about adding mosfets so I can drive a handful of LEDs on each output.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andrew Williams
WBA Consulting
WBA-TH1M Sensirion SHT11 Module
Special Olympics Polar Bear Plunge, Mar 20, 2010
Please try to keep these going and keep it in the BS2 family. I say that not only because that is the stamp that I use, but also for the reason that if someone has the ability, intelligence and experience in using your Prop or SX chips they are probably not in need of this type of instruction.
Thanks again,
Dominic
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Deus tantum me iudicabit
Thank you in advance.
Given:
DO
· IF (IN0 = 0) THEN
··· GOSUB Deck_The_Halls
· ELSEIF (IN1 = 0) THEN
··· GOSUB Jingle_Bells
· ELSEIF (IN2 = 0) THEN
··· GOSUB Merry_Christmas
· ELSEIF (IN4 = 0) THEN
··· GOSUB Christmas_Tree
· ELSE
··· PAUSE 20
· ENDIF
LOOP
a DIP switch assembly could be used, the 5-position switch allows only one contact at a time.
See 'Page 4 of 9' --
http://www.parallax.com/Portals/0/Downloads/docs/prod/schem/PDB-RevD-Schematic.pdf
Thank you again.
I'm so glad that everyone is enjoying this little project - it was a lot of fun to put together, and I'm glad it's doing its job and spreading some holiday cheer. This is my absolute most favorite time of year, and I'm happy to share it with you all.
@servello - As long as there are people who enjoy these Mini Projects, we will continue making them. Of course kind words from customers always help to keep them going - thanks! [noparse];)[/noparse]
Happy Chrismachanukwanzika everyone!
-- Jessica
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax Inc.
Alex.
Lipo 6 | Xenadrine
Post Edited (JohnVolz) : 7/28/2010 4:43:35 PM GMT
Sir i wants to read my text on Speaker man piezoelectric speaker kind of suks man and it only freqout music that what·i know till now can we throw text or voice kind of stuff on speaker that would be really cool i want Answer on Student Level. I am student· . Like Welcome to this and that.
Thanks for the Codes man for the Christmas. Man why u need to use that direction joystick. Or maybe ur breadboard is small lol thats why
When u can do that with 4 push buttons when button 1 = 1 then freqout this when button2= 1 then freqout this and when button3= 1 then freqout this and when button4= 1 then freqout this. Man your joystick is of no use but man it would be more use full for controlling robot Arm but not that one bigger and nice one man kids use to play games with that kind. And need to 2 joystick for that. Or lol advertisement for the new item.
·
Anyway, just a comment: I did have some trouble with this project. I spent the better part of an hour comparing the schematic with the wiring diagram, supra, trying to figure out how you connected VCC on the switch to +5 volts. So, I watched your youtube clip, hit pause at the right time, and whamoo, figured it out! The wire is "hidden" underneath the Switch.
So, perhaps I haven't convinced anyone that I'm a genuis, but just that I'm completely new to electronics. I thought, though, that I'd comment in case anyone was also "new" and couldn't figure it out either.
Awesome job!
And welcome to this "Wonderful World of Electronics", we're happy to have you here!
Cheers,
Jessica
@AttorneyJon ... if you have a Supra, you should join the automotive group, let me know if you are interested
Sorry no Supra here, just a gas guzzling jeep. However, I appreciate the invite.
Over the holidays I managed -- somehow -- to build a great amplifier for this project. I used two integrated circuits: The 272 and the 386. The 272 acts as a preamplifier and the 386 as a power amplifier. The neighbors were singing along to my (our) Christmas Caroling Device. If I can figure out a way to do it, I'll try and post the schematic or a picture of the amplifier.
Anyways, VAS-Y!
Thanks for sharing!
Thank you and Happy Holidays!
However, Andy Lindsay actually wrote a great tutorial on how to amplify a piezospeaker. You can find it here.
Happy holidays!