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)