Producing Alerts

These custom functions for producing alerts use the speaker and/or LED light:

  • singleBeep() — produces a typical beep

  • doubleBeep() — produces two quick beeps

  • longBeep() — produces a longer beep

  • playSong() — plays a song one note at a time in order

Be sure your app lists these global variables for the LED and speaker pin numbers before the setup() function:

int LED = 13;
int speaker = 9;

Be sure your app sets the pin modes for the LED and speaker within the setup() function:

    pinMode(LED, OUTPUT);
    pinMode(speaker, OUTPUT);

An alert can be used as feedback to a user when the robot's built-in D12 button is pressed. Alerts can also be useful as feedback to indicate when other events or conditions have occurred (such as: detecting an obstacle, reaching a destination, completing a task, etc.).

You can modify these custom functions (e.g., change frequency and duration of sound, etc.) and/or create your own alert functions for different situations (e.g., alarm sound, distress signal, obstacle alert, success signal, etc.).

singleBeep()

The singleBeep() custom function produces a typical beep sound:

void singleBeep() {
  digitalWrite(LED, HIGH);
  tone(speaker, 3000);
  delay(200);
  digitalWrite(LED, LOW);
  noTone(speaker);  
}

doubleBeep()

The doubleBeep() custom function produces two short, high-pitched beeps:

longBeep()

The longBeep() custom function produces a longer, low-pitched beep:

playSong()

You can also use the speaker to play simple music by playing a song one note at a time. Each note corresponds to a specific frequency played for a specific duration (such as: whole note, half note, quarter note, etc.).

Playing a song requires a file named notes.h that defines the specific frequencies for each note on a piano keyboard. It also defines durations (in milliseconds) for a whole note, half note, quarter note, etc. based on a defined beat length (in milliseconds). If necessary, you can modify the beat length to speed up or slow down the song.

To play a song using the speaker, your robot app will need to:

  1. Add notes.h as a separate tab (separate file) in your app

  2. Add an #include statement for notes.h in your app

  3. Add the playNote() custom function to play musical notes

  4. Create a custom function named playSong() to call the playNote() function for each note (or rest) in the song in sequence.

  5. Call the playSong() function to play the song

STEP 1. Add notes.h as Separate Tab in App

In your app, create a new blank tab named notes.h:

  • Arduino Create Web Editor: Click the tab with a drop-down icon, and select "New Tab." In the pop-up, enter notes.h as the name of the new tab, and click the OK button.

  • Arduino IDE Desktop Editor: Click the down arrow icon in the top-right corner of the code editor window, and then select "New Tab" in the drop-down list. A small dialog will appear at the bottom of the code editor window. Enter notes.h as the name of the new file, and click the OK button.

Copy the code below, and paste it into the blank tab named notes.h:

STEP 2. Include notes.h File in App

Next you need to include the notes.h file in your app code, similar to including a library.

In the code editor, click the tab that contains your main app. Add this code statement at the beginning of your app code:

STEP 3. Add Custom Function to Play Individual Note

A custom function named playNote() will be used to play one musical note at a time. When calling the function, you'll need to include parameters for the note and its duration.

Add the playNote() function after your loop() function:

The variable names for the notes and durations are defined in the notes.h file:

  • Each note on a piano keyboard is defined in notes.h with a variable name that represents a specific frequency. For example, noteC4 is the variable name for "Middle C", which is defined as having a frequency of 262 Hertz.

  • Each duration (whole note, half note, quarter note, etc.) is defined in notes.h with a variable name that represent a specific duration. For example, WN is the variable name for a whole note, which is defined as 4 beats (which will be 800 milliseconds if each beat length is defined as 200 milliseconds).

To play a note, your app code will need to call the playNote() function and list the defined variable names for the specific note and its duration within the parentheses.

For example, to play a "Middle C" whole note:

STEP 4. Create Custom Function to Play Song Note by Note

To play a song, your app code will need to play each note of the song in order by calling the playNote() function separately for each note (or rest) in the song.

You'll create a custom function named playSong() that will contain all the playNote() statements in sequence for your song.

Add this blank playSong() function after your loop() function:

To play a specific song, you'll need to know how it would be played on a piano one note at a time: i.e., what are the specific notes and their durations (whole note, half note, etc.)

Then you'll have to use the notes.h file to determine which variable names to list for the note (or rest) and its duration. Then you'll need to list a code statement for each note (and rest) to call the playNote() function.

For example, here's what the code inside the playSong() function would need to be in order to play the beginning of the song "Twinkle, Twinkle Little Star":

MULTIPLE SONGS: If your app needs to play more than one song, create a separate custom function to play the notes for each song. Give each custom function a unique name, such as playSong1(), playSong2(), etc.

STEP 5. Call Custom Function to Play Song

You can play your song by calling the playSong() function within another function (such as the setup(), loop(), or another custom function) depending on when the song should be played:

If you need the song to play faster or slower, you can change the value for the beatLength defined in the notes.h file. By default, beatLength has been set to 200 milliseconds.

  • If the song should be played faster, use a lower value for beatLength.

  • If the song should be played slower, use a higher value for beatLength.