Manual

3.b.4. Command 168: Store Note
Effect: This command is used to store a sequence of notes in the mega168’s EEPROM that can then be played
as a melody. Each stored note consists of an 8-bit note-frequency enumeration byte and a 16-bit millisecond-
duration word. For details on note enumeration, see the Play Note command (152—Section 3.b.1). Specifically,
this command adds the three bytes of note data to the end of the melody currently under construction, storing it
in EEPROM. After a hardware reset, the melody currently under construction is set to melody 0. It is possible to
store up to 8 melodies and up to 159 total notes. The notes can be distributed among the eight melodies in any
way. The “current melody” counter is incremented by the End Melody command (224—Section 3.b.5).
Storing melodies can be rather slow given that each EEPROM write takes ~3.4 ms and storing a note requires
three EEPROM writes. Typically, the code to store melodies will be executed once at the beginning of a program
and then removed from that program. It is crucial that you give enough time between successive issuances of these
commands for EEPROM to be written. If you simply stream these commands to the mega168, notes will be lost.
A safe delay between issuances would be 15 ms. At a minimum the delay should be 10.2 ms. Alternatively, you
can ensure EEPROM is ready for the next store-note command by polling with the “Check If EEPROM Busy”
command (254—Section 3.d.04), which returns zero if it’s ok to read from/write to EEPROM.
Values sent: note (8 bits), duration (16 bits)
command byte = 168 | (MSB data byte 3 << 2) | (MSB data byte 2 << 1) | MSB data byte 1
data byte 1 = 7 lower bits of note byte
data byte 2 = most significant byte of duration word (missing MSB)
data byte 3 = least significant byte of duration word (missing MSB)
3.b.5. Command 224: End Melody
Effect: Terminates the melody currently under construction by advancing the next melody’s pointer to point to
the location in EEPROM that would be the first note of the next melody. The “current melody” counter is then
incremented so the next Store Note command (168—Section 3.b.4) will store the first note of the next melody. It
is possible to store up to 8 melodies. The End Melody command performs two EEPROM writes and hence it is
recommended you allow it at least 6.8 ms before transmitting a command that will cause the mega168 to perform
an EEPROM write.
Values sent: none
Command byte = 224
3.b.6. Command 186: Erase Melodies
Effect: Resets the melody pointers to a state that represents zero stored melodies. It is not necessary to call this
function before storing a new set of melodies, but it can prevent you from having unwanted “garbage” melodies
left in EEPROM. One way to get a garbage melody would be to store eight melodies (0 – 7), perform a hardware
reset, and then store two melodies (0 and 1). The pointers for melodies 2 through 7 were not updated by the most
recent store commands, however the notes in EEPROM have changed. Attempting to play melody 2 could result
in some unexpected sequence of notes. This command will cause the mega168 to perform nine EEPROM writes,
so it is recommended you give it at least 30.6 ms before issuing another command that will cause the mega168 to
perform an EEPROM write.
Values sent: none
Orangutan X2 Command Documentation v1.01 © 2001–2010 Pololu Corporation
3. Low-Level SPI Commands Page 14 of 27