ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | List of all members
Melody Class Reference

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...
 

Detailed Description

Plays a melody on a digital output pin using tone().

The following example plays a simple tone three times on digital pin 8:

#include <Melody.h>
int notes[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3,
NOTE_REST, NOTE_B3, NOTE_C4, NOTE_REST
};
byte lengths[] = {4, 8, 8, 4, 4, 4, 4, 4, 2};
Melody melody(8);
void setup() {
melody.setMelody(notes, lengths, sizeof(lengths));
melody.setLoopCount(3);
melody.play();
}
void loop() {
melody.run();
}

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:

void setup() {
melody.setMelody(notes, lengths, sizeof(lengths));
melody.setLoopDuration(60000UL);
melody.play();
}

Definition at line 122 of file Melody.h.

Member Function Documentation

int Melody::loopCount ( ) const
inline

Returns the number of times the melody should loop before stopping.

The default value is zero, indicating that the melody will loop indefinitely.

See Also
setLoopCount(), setLoopDuration(), play()

Definition at line 128 of file Melody.h.

void Melody::play ( )

Starts playing the melody, or restarts it if already playing.

See Also
playOnce(), setMelody(), stop(), loopCount()

Definition at line 146 of file Melody.cpp.

void Melody::playOnce ( )

Plays the melody once and then stops.

See Also
play(), stop()

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.

void Melody::setLoopCount ( int  count)
inline

Sets the number of times the melody should loop to count.

If count is zero, then the melody will loop indefinitely.

See Also
loopCount(), setLoopDuration()

Definition at line 129 of file Melody.h.

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().

See Also
loopCount(), setLoopCount()

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.

See Also
play()

Definition at line 199 of file Melody.cpp.

void Melody::stop ( )

Stops playing the melody.

See Also
play()

Definition at line 178 of file Melody.cpp.


The documentation for this class was generated from the following files: