insert servo code
3tigerz
Posts: 4
Hi, I am working with a Board of Education from Parallax. Can anyone help me with the correct insertion of my servo code? This is what I have, A piezo speaker, 5 LEDs and a servo that all come on together at a push of a button. My issue is with where to insert the servo subroutine code. I have it all working except the servo. Attached is the code I am working with. I have inserted the servo code at the end. Thank You ahead of time. FYI, I am a very newbie so please be gentle.
'
[ Title ]
' WishUAMerryWithLEDs.bs2
' Play a variety of Christmas songs with corresponding light displays using
' the 5-Position Switch, piezospeaker, and 5 red and green LEDs.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ EEPROM Data ]
' Store information needed to play "We Wish you a Merry Christmas"
Notes_MC DATA "e", "a", "a", "b", "a", "G", "F", "F",
"F", "b", "b", "C", "b", "a", "G", "e",
"e", "C", "C", "d", "C", "b", "a", "F",
"e", "e", "F", "b", "G", "a",
"e", "a", "a", "a", "G", "G", "a", "G", "F", "e",
"b", "C", "b", "a", "e", "e",
"e", "e", "F", "b", "G", "a", "Q"
Octaves_MC DATA 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 7, 6, 6, 6, 6,
6, 7, 7, 7, 7, 6, 6, 6,
6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 7, 6, 6, 7, 6,
6, 6, 6, 6, 6, 6
Durations_MC DATA 4, 4, 8, 8, 8, 8, 4, 4,
4, 4, 8, 8, 8, 8, 4, 4,
4, 4, 8, 8, 8, 8, 4, 4,
8, 8, 4, 4, 4, 2,
4, 4, 4, 4, 2, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4,
8, 8, 4, 4, 4, 2
'
[ I/O Definitions ]
speaker PIN 8 ' Piezospeaker connection
'
[ Variables ]
offset VAR Nib ' Variable space for LOOKUP table
index VAR Byte ' Index for pointing at data
noteDot VAR Bit ' Stores if a note is dotted or not
noteOctave VAR Nib ' Stores the octave of each note
noteLetter VAR Byte ' Stores the letter of each note
noteFreq VAR Word ' Stores the frequency of each note
noteDuration VAR Word ' Stores the duration of each note
'
[ Main Routine ]
DO
IF (IN2 = 0) THEN
GOSUB Merry_Christmas ' Play "We Wish You a Merry Christmas"
ENDIF
LOOP ' Loop forever
'
[ Subroutine - Merry_Christmas ]
' Plays the song "We Wish you a Merry Christmas"
Merry_Christmas: ' Subroutine label
index = 0 ' Set index back to 0 for new songs
' Loop until the end of the song, or the center button is pressed
DO UNTIL (noteLetter = "Q") OR (IN2 = 0)
READ Notes_MC + index, noteLetter: GOSUB Get_Frequency
READ Octaves_MC + index, noteOctave: GOSUB Get_Octave
READ Durations_MC + index, noteDuration: noteDuration = 1000 / noteDuration
GOSUB Display_Color ' Turn on LED display
FREQOUT speaker, noteDuration, noteFreq ' Play note
index = index + 1 ' Increase data pointer by 1
LOOP
noteLetter = "R" ' Set note letter to "R"
RETURN ' Return to Main Program
' Return to Main Program
'
[ Subroutine - Get_Frequency ]
' Returns the frequency for each corresponding note letter in the 8th octave
Get_Frequency: ' Subroutine label
LOOKDOWN noteLetter, [ "C", "d", "D", "e", "E",
"F", "g", "G", "a", "A",
"b", "B", "R", "Q" ], offset
LOOKUP offset, [ 4186, 4435, 4699, 4978, 5274,
5588, 5920, 6272, 6645, 7040,
7459, 7902, 0, 0 ], noteFreq
RETURN
'
[ Subroutine - Get_Octave ]
' Calculates what octave the note should be played in
Get_Octave: ' Subroutine label
noteOctave = 8 - noteOctave
noteFreq = noteFreq / (DCD noteOctave)
RETURN
'
[ Subroutine - Display_Color ]
' Turns on LEDs based on what notes are played
Display_Color: ' Subroutine label
LOW 13: LOW 12: LOW 11: LOW 10: LOW 09 ' Turn LEDs off
SELECT noteLetter
CASE "A", "a"
HIGH 13
CASE "B", "b"
HIGH 12
CASE "C", "c"
HIGH 11
CASE "D", "d"
HIGH 10
CASE "E", "e"
HIGH 09
CASE "F", "f"
HIGH 13
HIGH 11
HIGH 09
CASE "G", "g"
HIGH 12
HIGH 10
ENDSELECT
RETURN
'
[ Subroutine - counter_VAR ]
counter VAR Word
PAUSE 1000
DO
DEBUG "Pulse width increment by 8", CR
FOR counter = 500 TO 1000 STEP 10
PULSOUT 14, counter
PAUSE 7
DEBUG DEC5 counter, CR, CRSRUP
NEXT
DEBUG CR, "Pulse width decrement by 20", CR
FOR counter = 1000 TO 500 STEP 10
PULSOUT 14, counter
PAUSE 7
DEBUG DEC5 counter, CR, CRSRUP
NEXT
DEBUG CR, "Repeat", CR
LOOP
'
[ Title ]
' WishUAMerryWithLEDs.bs2
' Play a variety of Christmas songs with corresponding light displays using
' the 5-Position Switch, piezospeaker, and 5 red and green LEDs.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ EEPROM Data ]
' Store information needed to play "We Wish you a Merry Christmas"
Notes_MC DATA "e", "a", "a", "b", "a", "G", "F", "F",
"F", "b", "b", "C", "b", "a", "G", "e",
"e", "C", "C", "d", "C", "b", "a", "F",
"e", "e", "F", "b", "G", "a",
"e", "a", "a", "a", "G", "G", "a", "G", "F", "e",
"b", "C", "b", "a", "e", "e",
"e", "e", "F", "b", "G", "a", "Q"
Octaves_MC DATA 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 7, 6, 6, 6, 6,
6, 7, 7, 7, 7, 6, 6, 6,
6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 7, 6, 6, 7, 6,
6, 6, 6, 6, 6, 6
Durations_MC DATA 4, 4, 8, 8, 8, 8, 4, 4,
4, 4, 8, 8, 8, 8, 4, 4,
4, 4, 8, 8, 8, 8, 4, 4,
8, 8, 4, 4, 4, 2,
4, 4, 4, 4, 2, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4,
8, 8, 4, 4, 4, 2
'
[ I/O Definitions ]
speaker PIN 8 ' Piezospeaker connection
'
[ Variables ]
offset VAR Nib ' Variable space for LOOKUP table
index VAR Byte ' Index for pointing at data
noteDot VAR Bit ' Stores if a note is dotted or not
noteOctave VAR Nib ' Stores the octave of each note
noteLetter VAR Byte ' Stores the letter of each note
noteFreq VAR Word ' Stores the frequency of each note
noteDuration VAR Word ' Stores the duration of each note
'
[ Main Routine ]
DO
IF (IN2 = 0) THEN
GOSUB Merry_Christmas ' Play "We Wish You a Merry Christmas"
ENDIF
LOOP ' Loop forever
'
[ Subroutine - Merry_Christmas ]
' Plays the song "We Wish you a Merry Christmas"
Merry_Christmas: ' Subroutine label
index = 0 ' Set index back to 0 for new songs
' Loop until the end of the song, or the center button is pressed
DO UNTIL (noteLetter = "Q") OR (IN2 = 0)
READ Notes_MC + index, noteLetter: GOSUB Get_Frequency
READ Octaves_MC + index, noteOctave: GOSUB Get_Octave
READ Durations_MC + index, noteDuration: noteDuration = 1000 / noteDuration
GOSUB Display_Color ' Turn on LED display
FREQOUT speaker, noteDuration, noteFreq ' Play note
index = index + 1 ' Increase data pointer by 1
LOOP
noteLetter = "R" ' Set note letter to "R"
RETURN ' Return to Main Program
' Return to Main Program
'
[ Subroutine - Get_Frequency ]
' Returns the frequency for each corresponding note letter in the 8th octave
Get_Frequency: ' Subroutine label
LOOKDOWN noteLetter, [ "C", "d", "D", "e", "E",
"F", "g", "G", "a", "A",
"b", "B", "R", "Q" ], offset
LOOKUP offset, [ 4186, 4435, 4699, 4978, 5274,
5588, 5920, 6272, 6645, 7040,
7459, 7902, 0, 0 ], noteFreq
RETURN
'
[ Subroutine - Get_Octave ]
' Calculates what octave the note should be played in
Get_Octave: ' Subroutine label
noteOctave = 8 - noteOctave
noteFreq = noteFreq / (DCD noteOctave)
RETURN
'
[ Subroutine - Display_Color ]
' Turns on LEDs based on what notes are played
Display_Color: ' Subroutine label
LOW 13: LOW 12: LOW 11: LOW 10: LOW 09 ' Turn LEDs off
SELECT noteLetter
CASE "A", "a"
HIGH 13
CASE "B", "b"
HIGH 12
CASE "C", "c"
HIGH 11
CASE "D", "d"
HIGH 10
CASE "E", "e"
HIGH 09
CASE "F", "f"
HIGH 13
HIGH 11
HIGH 09
CASE "G", "g"
HIGH 12
HIGH 10
ENDSELECT
RETURN
'
[ Subroutine - counter_VAR ]
counter VAR Word
PAUSE 1000
DO
DEBUG "Pulse width increment by 8", CR
FOR counter = 500 TO 1000 STEP 10
PULSOUT 14, counter
PAUSE 7
DEBUG DEC5 counter, CR, CRSRUP
NEXT
DEBUG CR, "Pulse width decrement by 20", CR
FOR counter = 1000 TO 500 STEP 10
PULSOUT 14, counter
PAUSE 7
DEBUG DEC5 counter, CR, CRSRUP
NEXT
DEBUG CR, "Repeat", CR
LOOP
Comments
http://forums.parallax.com/showthread.php/144156-CONTEST-Hack-The-Halls
With all that tune playing, you may be hard pressed to generate proper servo pulses. Your project would be a greatly aided by a Servopal.
Looks like you want slow servo motion while your music is playing. Might be tough with a BS2, unless you can update servo positions after each note is played. Among other things in your code, you need to delete the painfully slow DEBUG statements in the middle of your servo PULSOUT routines.