3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino Binary Alarm Clock

Step 12Alarm signal

It's quite easy to generate sound on the arduino by just using digitalWrite() and delay()  to toggle the state of the speaker pin in the right frequency. But for this project I need to constantly check the buttons, update the clock and update the display, even when the alarm signal is playing. Therefore the sound generation has to be non-blocking, which means you don't have to wait for the tones to end before you can do anything else.

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 StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
2
Followers
1
Author:njakol