B.2. command 160: play frequency, B.3. command 176: play melody – Pololu Orangutan X2 User Manual

Page 13

Advertising
background image

data byte 2 = most significant byte of duration word (minus the MSB, which is in the command byte)

data byte 3 = least significant byte of duration word (minus the MSB, which is in the command byte)

3.b.2. Command 160: Play Frequency

Effect: Plays the desired frequency immediately. Allowed frequencies are those greater than 40 Hz and less than
10 kHz. The frequency is a 15-bit value specified in Hz. Duration a 16-bit value specified in ms.

If the buzzer is currently playing when this command is received, this command will take precedence and
overwrite the current buzzer action. For example, if a melody was playing, the note specified by this command
will interrupt the melody and after the note is through the melody will not continue. As such, if you wish to stream
a sequence of notes to the mega168 using this command, you must time the issuance of the commands on the
mega644 so that each note doesn’t just overwrite the previous. This command will not queue up the notes if called
repeatedly.

It is important to note that there is a restriction on the duration of any notes whose frequencies are greater than
1 kHz. The product of the duration (in ms) and the frequency (in kHz) must be no greater than 0xFFFF (65,535).
If you attempt to play a frequency of 10 kHz, for example, the maximum duration you can specify is 6553 ms. If
you specify a longer duration there will be an integer overflow and the resulting duration of the note will not be
what you specified. If you wish to play a 10 kHz frequency for longer than 6.5 seconds, you can accomplish this
by stringing together multiple play-frequency commands.

Values sent: frequency (15 bits), duration (16 bits)

command byte = 160 | (MSB data byte 4 << 2) | (MSB data byte 3 << 1) | MSB data byte 2

data byte 1 = most significant byte of frequency word (MSB always 0)

data byte 2 = least significant byte of frequency word (missing MSB)

data byte 3 = most significant byte of duration word (missing MSB)

data byte 4 = least significant byte of duration word (missing MSB)

3.b.3. Command 176: Play Melody

Effect: Plays the desired melody stored in EEPROM. It is possible to store up to 8 melodies using the Store
Note (168—

Section 3.b.4

) and End Melody (224—

Section 3.b.5

) commands. If the melody doesn’t exist you

will either get no response or will possibly hear some garbage melody, depending on the state of the mega168’s
EEPROM. One way to get a garbage melody would be to store eight melodies (0 – 7), perform a hardware reset,
and then store two different 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 and have potentially overwritten a
portion of the old melodies 2 – 7). Attempting to play melody 2 could result in some unexpected sequence of
notes. One way to avoid this situation is to issue an “Erase Melodies” command (186—

Section 3.b.6

) before

storing a new set of melodies, however this is purely optional.

Values sent: melody (3 bits)

command byte = 176 | melody

Orangutan X2 Command Documentation v1.01

© 2001–2010 Pololu Corporation

3. Low-Level SPI Commands

Page 13 of 27

Advertising