Button Selects Rtttl Tune - BS2
This is on a BS2 homework board. I modified the Rtttl program "MicroMusicWithRtttl.bs2" so that it would play 3 different tunes. The attached modified program is called ButtonSelectsRtttlTune.bs2.
·
There are 3 LED’s, a push button, and a speaker.
·
You can select which tune you want to play with the push button.
·
The 3 LED's...
·
The first LED will start blinking...
·
If you press the button while this light is blinking, the 1st tune will play (after the other LEDs finish their “user input” cycle).
·
Then the 2nd LED will start blinking. If you press the button while this light is blinking, the 2nd tune will play.
·
Then the 3rd LED will start blinking. If you press the button while this light is blinking, the 3rd tune will play.
·
(After you have made your selection(s), the tune(s) will begin to play.)
·
If you don't press any buttons, no tunes will play. If you press the button only when the 2nd LED is flashing, only the 2nd tune will play. Or only when first light flashing, only the first tune. Or press the button when the 1st and 2nd LEDs are flashing, then the 1st and 2nd tunes will play, one after the other is finished playing. Or press the button for all 3 lights, and all 3 tunes will play, one after the other.
·
There are many debug messages to also tell you what is going on, although this will work without being connected to a pc.
·
This program just about takes up all the memory. I had to move some things to subroutines so it would all fit. But there is a tiny bit of extra space now. If you comment out the debug messages, this would give you more room to add more tunes or longer tunes.
·
Unfortunately, moving things to subroutines, makes the code more difficult to understand. Sorry about that. I added a bunch of comments though.
·
I found that the [noparse][[/noparse]Done DATA ",q,"] line was not used (Done) in the original program. I added a comma q (,q) to the end of each Rtttl tune which implements this feature. (Breaks it out of the loop.) Then this makes it stop playing and is necessary for my modified program to then go on and play another tune.
·
So for example, here is one of the 3 tunes from the attached program. Notice the last entry·is a ,q added at the end...
·
'Song 1 For He's A Jolly Good Fellow
RTTTL_Pointer1 DATA "ForHe'sAJollyGoodFellow:d=4,o=7,b=320:c,2e,e,e,",
"d,e,2f.,2e,e,2d,d,d,c,d,2e.,2c,d,2e,e,e,d,e,2f,",
"g,2a,a,g,g,g,2f,d,2c,q"
·
You can replace just that part and switch the songs. Note that I renamed "RTTTL_File" to RTTTL_Pointer1, RTTTL_Pointer2, and RTTTL_Pointer3 in my program for the 3 different tunes.
·
Instructions for connecting the LED's, the pushbutton, and the speaker are all in the "What’s a Microcontroller?" book. Following is a link to that book online and the associated page numbers are listed.
·
"What’s a Microcontroller?" book...
http://www.parallax.com/dl/docs/books/edu/wamv2_2.pdf
·
LED page 48, except use 3 LED's to I/O 0, 1, and 2.
Pushbutton page 76, except connect to I/O 15.
Speaker page 220, except connect to I/O 14.
·
Have fun!
·
There are 3 LED’s, a push button, and a speaker.
·
You can select which tune you want to play with the push button.
·
The 3 LED's...
·
The first LED will start blinking...
·
If you press the button while this light is blinking, the 1st tune will play (after the other LEDs finish their “user input” cycle).
·
Then the 2nd LED will start blinking. If you press the button while this light is blinking, the 2nd tune will play.
·
Then the 3rd LED will start blinking. If you press the button while this light is blinking, the 3rd tune will play.
·
(After you have made your selection(s), the tune(s) will begin to play.)
·
If you don't press any buttons, no tunes will play. If you press the button only when the 2nd LED is flashing, only the 2nd tune will play. Or only when first light flashing, only the first tune. Or press the button when the 1st and 2nd LEDs are flashing, then the 1st and 2nd tunes will play, one after the other is finished playing. Or press the button for all 3 lights, and all 3 tunes will play, one after the other.
·
There are many debug messages to also tell you what is going on, although this will work without being connected to a pc.
·
This program just about takes up all the memory. I had to move some things to subroutines so it would all fit. But there is a tiny bit of extra space now. If you comment out the debug messages, this would give you more room to add more tunes or longer tunes.
·
Unfortunately, moving things to subroutines, makes the code more difficult to understand. Sorry about that. I added a bunch of comments though.
·
I found that the [noparse][[/noparse]Done DATA ",q,"] line was not used (Done) in the original program. I added a comma q (,q) to the end of each Rtttl tune which implements this feature. (Breaks it out of the loop.) Then this makes it stop playing and is necessary for my modified program to then go on and play another tune.
·
So for example, here is one of the 3 tunes from the attached program. Notice the last entry·is a ,q added at the end...
·
'Song 1 For He's A Jolly Good Fellow
RTTTL_Pointer1 DATA "ForHe'sAJollyGoodFellow:d=4,o=7,b=320:c,2e,e,e,",
"d,e,2f.,2e,e,2d,d,d,c,d,2e.,2c,d,2e,e,e,d,e,2f,",
"g,2a,a,g,g,g,2f,d,2c,q"
·
You can replace just that part and switch the songs. Note that I renamed "RTTTL_File" to RTTTL_Pointer1, RTTTL_Pointer2, and RTTTL_Pointer3 in my program for the 3 different tunes.
·
Instructions for connecting the LED's, the pushbutton, and the speaker are all in the "What’s a Microcontroller?" book. Following is a link to that book online and the associated page numbers are listed.
·
"What’s a Microcontroller?" book...
http://www.parallax.com/dl/docs/books/edu/wamv2_2.pdf
·
LED page 48, except use 3 LED's to I/O 0, 1, and 2.
Pushbutton page 76, except connect to I/O 15.
Speaker page 220, except connect to I/O 14.
·
Have fun!