ArduinoLibs
|
Plays a melody on a digital output pin using tone(). More...
#include <Melody.h>
Public Member Functions | |
Melody (uint8_t pin) | |
Constructs a new melody playing object for pin. | |
bool | isPlaying () const |
Returns true if the melody is currently playing; false if not. | |
int | loopCount () const |
Returns the number of times the melody should loop before stopping. More... | |
void | setLoopCount (int count) |
Sets the number of times the melody should loop to count. More... | |
void | setLoopDuration (unsigned long ms) |
Sets the maximum number of loops to last no longer than ms milliseconds. More... | |
void | play () |
Starts playing the melody, or restarts it if already playing. More... | |
void | playOnce () |
Plays the melody once and then stops. More... | |
void | stop () |
Stops playing the melody. More... | |
void | setMelody (const int *notes, const uint8_t *lengths, unsigned int size) |
Sets the melody to the size elements of notes and lengths. More... | |
void | run () |
Runs the melody control loop. More... | |
Plays a melody on a digital output pin using tone().
The following example plays a simple tone three times on digital pin 8:
The notes
array contains the frequency of the notes to be played, with the special value NOTE_REST
indicating a rest where no notes are playing. The lengths
array contains the lengths of each of the notes; a value of 4 indicates a quarter note, a value of 8 indicates an eighth note, etc.
The run() method must be called from the application's main loop()
method to ensure that the melody advances from one note to the next. It will not block the application while notes are playing.
The number of loops can also be specified with setLoopDuration() which sets a maximum amount of time that the melody will play before stopping. The following example plays the melody for no more than 60 seconds:
|
inline |
Returns the number of times the melody should loop before stopping.
The default value is zero, indicating that the melody will loop indefinitely.
void Melody::play | ( | ) |
Starts playing the melody, or restarts it if already playing.
Definition at line 146 of file Melody.cpp.
void Melody::playOnce | ( | ) |
Plays the melody once and then stops.
Definition at line 162 of file Melody.cpp.
void Melody::run | ( | ) |
Runs the melody control loop.
This function must be called by the application's main loop()
function to cause the melody to advance from note to note. It will not block the application while notes are playing.
Definition at line 214 of file Melody.cpp.
|
inline |
Sets the number of times the melody should loop to count.
If count is zero, then the melody will loop indefinitely.
void Melody::setLoopDuration | ( | unsigned long | ms | ) |
Sets the maximum number of loops to last no longer than ms milliseconds.
This function must be called after the melody is specified with setMelody() as it uses the length of the melody and ms to determine the loopCount().
Definition at line 131 of file Melody.cpp.
void Melody::setMelody | ( | const int * | notes, |
const uint8_t * | lengths, | ||
unsigned int | size | ||
) |
Sets the melody to the size elements of notes and lengths.
If a melody is currently playing, then this function will stop playback.
The notes array contains the frequency of the notes to be played, with the special value NOTE_REST
indicating a rest where no notes are playing. The lengths array contains the lengths of each of the notes; a value of 4 indicates a quarter note, a value of 8 indicates an eighth note, etc.
Definition at line 199 of file Melody.cpp.
void Melody::stop | ( | ) |