CSE: Setup Alert

Make the setup() function play a two-tone sequence, alerting the user that the sketch has finished uploading:

// setup_alert - play a two-tone alert in setup()

int oj_rc = 9;  // pin D9 drives the right channel

void setup() {
  pinMode(oj_rc, OUTPUT);


void loop() {
  // nothing to do here.

void setup_alert() {
  int dur_delay = 500;  // duration of delay (ms)
  int dur_tone  = 500;  // duration of tone  (ms)
  int pitch_1   = 500;  // pitch of 1st tone (Hz)
  int pitch_2   = 800;  // pitch of 2nd tone (Hz)

  tone(oj_rc, pitch_1, dur_tone);
  delay(dur_delay);     // let tone() complete

  tone(oj_rc, pitch_2, dur_tone);
  delay(dur_delay);     // pause before loop()



Playing two tones (of differing pitches) produces a distinctive alert. Pausing a bit afterwards separates this alert from any following audio output.

Putting the tone generation code in setup_alert() lets us disable the alert by commenting out a single line. It also keeps the setup() function smaller and simpler. However, I still call pinMode() from setup(), because other code is likely to need this.

The first call to delay() works around the fact that that the call to tone() does not block (i.e., it returns immediately). If it gets called again during dur_tone, it terminates the current tone and begins a new one. So, without this call, most of the first tone will never get played!

The Uno's Pin 9 drives the Out Jack's "ring" (right channel); Pin 10 drives the "tip" (left channel).

This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r3 - 05 Mar 2016, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email