SparkFun Inventor's Kit for RedBot
This Tutorial is Retired!
This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.
View the updated tutorial: Experiment Guide for RedBot with Shadow Chassis
Experiment 4: Push to Start & Making Sounds
In our past experiments, our RedBot starts running right away after we upload code. Here, we will show an example of how to use the user push button to start our program -- and, to make things fun -- we're going to add some noises? After all, what's a robot without some beep-boop sounds!
Let’s load this experiment onto the RedBot. Go to File > Examples > RedBot_Experiments > Exp4_MakingSounds or copy and paste the example code below:
language:c
/***********************************************************************
* Exp4_1_MakingSounds -- RedBot Experiment 4.1
*
* Push the button (D12) to make some noise and start running!
*
* Hardware setup:
* Plug the included RedBot Buzzer board into the Servo header labeled 9.
*
* This sketch was written by SparkFun Electronics,with lots of help from
* the Arduino community. This code is completely free for any use.
*
* 23 Sept 2013 N. Seidle/M. Hord
* 29 Oct 2014 B. Huang
***********************************************************************/
#include <RedBot.h>
RedBotMotors motors;
// Create a couple of constants for our pins.
const int buzzerPin = 9;
const int buttonPin = 12;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP); // configures the button as an INPUT
// INPUT_PULLUP defaults it to HIGH.
pinMode(buzzerPin, OUTPUT); // configures the buzzerPin as an OUTPUT
}
void loop()
{
if ( digitalRead(buttonPin) == LOW ) // if the button is pushed (LOW)
{
tone(buzzerPin, 1000); // Play a 1kHz tone on the pin number held in
// the variable "buzzerPin".
delay(125); // Wait for 125ms.
noTone(buzzerPin); // Stop playing the tone.
tone(buzzerPin, 2000); // Play a 2kHz tone on the buzzer pin
motors.drive(255); // Start the motors. The whiskers will stop them.
delay(1000); // delay for 1000 ms (1 second)
noTone(buzzerPin); // Stop playing the tone.
motors.brake(); // brake() or stop the motors.
}
else // otherwise, do this.
{
}
}
What You Should See/Hear
Your Redbot should make a beep-beep sound and then start spinning the wheels when you press the D12 button next to the USB connector.
Code to Note
tone([pin], [freq])
plays a sound of the given frequency on the pin. Since the buzzerPin
is set to 9, we can use these commands to play different tones on the buzzer. Rather than just using a blinking LED as an indicator, we can program our RedBot to make sounds at different times to help us know what the robot is doing. This is a way to debug problems just by the sound the robot makes!
language:c
tone(buzzerPin, 1000); // plays a note of 1 kHz (1000 Hz).
noTone()
stops playing a tone on a specific pin.
language:c
noTone(buzzer); // stops playing the note.
Learn More: Sound
Sound is a longitudinal wave that is caused by vibrations (compressions and expansions) in the air. The Arduino causes sounds by creating a square-wave of a given frequency on the pin. The buzzer reacts to the square-wave by moving the air back and forth creating sound. Because a square-wave is not a pure tone, you will may also hear other harmonics of the base frequency.
A list of notes and their related frequencies can be found here. Can you compose a simple scale? (Hint: The C-Major scale is the easiest, it doesn't have any sharps or flats.) Human hearing is generally limited between 20 Hz and 20,000 Hz. Can you find where your hearing cuts out? Note: the piezo-buzzer is not capable of producing high fidelity sounds at the low end of the frequency spectrum.
Going Further
Is it hard to remember which frequency corresponds to which note? Take a look at Experiment 4.1 -- Go to File > Examples > Exp4_1_Music
This experiment will play, “It’s a Small World After All” when you press the D12 button. If you look at the code, we use a couple tricks to make things easier.
You will see an extra file in this example called notes.h
. This file is a "header" file that is often used to contain extra constants, variables, and sub-routines that are used in your code. notes.h
has a list of #define statements that replace noteC4
with 262 (much like a variable, but more efficient).
It also has the length of the notes defined in terms of milliseconds. These are denoted as: WN
- whole note, HN
- half note, QN
- quarter note, EN
- eighth note, SN
- sixteenth note.
The second trick is a custom function called playNote([note], [duration])
. This custom function plays the note using the tone()
command and adds a delay()
. This simplifies playing notes to just one line of code. For example, to play a middle C for a quarter note, we can type: playNote(noteC4, QN);
What song can you compose? Twinkle Twinkle? Amazing Grace? or When the Saints go Marching? Pick a piece to compose as your "theme" song for your RedBot!
Troubleshooting
My motors aren't turning!
- This code demonstrates only the tone() commands; there's no code to make the motors turn.
I don't hear anything?
- If you look at the code, the example program waits for a button press on D12 before doing anything. Press the button and listen.
- Make sure that the buzzer is plugged into the Servo header labeled #9.