Step 12Alarm signal
Therefore I used the Tone library written by Brett Hagman to generate sound. It's a really nice library which makes it easy to play different tones, and best of all, it's non-blocking.
Because the the melody should be non-blocking I couldn't just use a for-loop to loop through the tones. Instead I used if-statements. everytime the melody function is called it checks if the last tone has stopped playing, if it has it will start the next one. The "melody" I've used is just C, D, E, F, G, A, B, C first played one after eachother, then every other, then every third, and so on.
_______________________________________________________________________
The play_melody() function:
// "INTERNAL" VARIABLES FOR PLAY_MELODY FUNCTION:
int melody[] = { NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5 };
int melody_length = sizeof(melody) / 2; // Melody length, for looping.
// sizeof() returns the size of the array in bytes, and because
// an int is 2 bytes, sizeof will return 2*(number of array elements)
int i = 0; //loop variable
boolean reset_loop = true;
int jump = 1; // how many notes to jump in the melody array
int position = 0; // position in melody array
void play_melody()
{
if(!(tone_maker.isPlaying())) // if the last tone has stopped
{
if(i {
//tone_maker.stop();
tone_maker.play(melody[position], 300);
// A pause between notes...
//delay(300); // replace with nonblocking.
if (DEBUG)
{ // If debugging, report loop, tone, beat, and duration
Serial.print(position);
Serial.print(":");
Serial.print(melody[position]);
Serial.print("\n");
}
position += jump;
//if position is bigger than the array, start from beginning of array:
position = position%melody_length;
i++;
}
else
{
jump++;
i = 0;
}
}
}
_______________________________________________________________________
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































