Example Sketch: StartResetIndicator
This example sketch makes a beep when it starts running, then it goes on to send Serial Monitor messages every half-second. These messages will continue indefinitely because they are in the loop function. If the power to the Arduino is interrupted, the sketch will start at the beginning again, and you will hear the beep.
- Reconnect power to your board.
- Enter, save, and upload StartResetIndicator to the Arduino.
- If you did not hear a tone, check your wiring and code for errors and try again.
- If you did hear an audible tone, open the Serial Monitor (this may cause a reset too). Then, push the reset button on the BOE Shield.
- Verify that, after each reset, the piezospeaker makes a clearly audible tone for one second, and then the “Waiting for reset…” messages resumes.
- Also try disconnecting and reconnecting your battery supply and programming cable, and then plugging them back in. This should also trigger the start-alert tone.
/* * Robotics with the BOE Shield - StartResetIndicator * Test the piezospeaker circuit. */ void setup() // Built in initialization block { Serial.begin(9600); Serial.println("Beep!"); tone(4, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone } void loop() // Main loop auto-repeats { Serial.println("Waiting for reset..."); delay(1000); }
How StartResetIndicator Works
StartResetIndicator begins by displaying the message “Beep!” in the Serial Monitor. Then, immediately after printing the message, the tone function plays a 3 kHz tone on the piezoelectric speaker for 1 second. Because the instructions are executed so rapidly by the Arduino, it should seem as though the message is displayed at the same instant the piezospeaker starts to play the tone.
When the tone is done, the sketch enters the loop function, which displays the same “Waiting for reset…” message over and over again. Each time the reset button on the BOE Shield is pressed or the power is disconnected and reconnected, the sketch starts over again with the “Beep!” message and the 3 kHz tone.
Sketch Update Notice!!!
The sketch StartResetIndicator was updated on 11/16/2012. On the Arduino Mega 2650, the upload hangs with the original code listing. The offending command was:
Serial.println(“Beep!!!”);
It turns out that having more than one exclamation point in a row in a serial string causes this problem in the Mega 2650. From this point forward, we will find another way to express our enthusiasm in serial strings. – Editor.
Your Turn – Adding StartResetIndicator to a Different Sketch
We’ll use tone at the beginning of every example sketch from here onward. So, it’s a good idea to get in the habit of putting tone and delay statements at the beginning of every sketch’s setup function.
- Open RightServoTest.
- Copy the tone and delay function calls from the StartResetIndicator sketch into the beginning of the RightServoTest sketch’s setup function.
- Run the modified sketch and verify that it responds with the “sketch starting” tone every time the Arduino is reset.