Experiment Guide for RedBot with Shadow Chassis

Pages
Contributors: bri_huang, Shawn Hymel, SFUptownMaker
Favorited Favorite 11

Experiment 4: Push to Start & Making Sounds (SIK)

Read on if you have the SIK for RedBot or are using the RedBot Buzzer. If not, skip to the next section.

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 > SparkFun RedBot Library > Exp4_1_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 (and 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.

Push D12 on the RedBot Mainboard

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? Go to File > Examples > SparkFun RedBot Library > Exp4_2_Music or copy and paste the example code below:

language:c
/***********************************************************************
 * Exp04_2_Music -- RedBot Experiment 4.2 (Making Music)
 *
 * Rather than just making beeps and boops, what about playing an actual 
 * song? This example includes a "header" file called notes.h that has all
 * the notes on any standard piano #defined to make composing sounds easier.
 *
 * 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
 *
 * Music from: 
 * http://musicwithmstomomi.global2.vic.edu.au/2013/02/18/recorder-its-small-world-after-all/
 ***********************************************************************/

#include "notes.h"  // Individual "notes" have been #defined in the notes.h tab to make
                    // playing sounds easier. noteC4, for example, is defined as 262, the
                    // frequency for middle C. See the tab above? 

#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)
  {
    playTwinkleTwinkle();
  }
}

void playTwinkleTwinkle()
{
  playNote(noteC4, QN);
  playNote(noteC4, QN);

  playNote(noteG4, QN);    
  playNote(noteG4, QN);  

  playNote(noteA4, QN);    
  playNote(noteA4, QN);  

  playNote(noteG4, HN);

  playNote(noteF4, QN);    
  playNote(noteF4, QN);  

}

void playSmallWorld()
{ 
  // we use a custom function below called playNote([note],[duration]) 
  // to play a note and delay a certain # of milliseconds. 
  //
  // Both notes and durations are #defined in notes.h -- WN = whole note, 
  // HN = half note, QN = quarter note, EN = eighth note, SN = sixteenth note.
  //
  playNote(noteG5, HN+QN);
  playNote(noteG5, QN);
  playNote(noteB5, HN);
  playNote(noteG5, HN);
  playNote(noteA5, HN+QN);
  playNote(noteA5, QN);
  playNote(noteA5, HN+QN);
  playNote(Rest, QN);
  playNote(noteA5, HN+QN);
  playNote(noteA5, QN);
  playNote(noteC6, HN);
  playNote(noteA5, HN);
  playNote(noteB5, HN+QN);
  playNote(noteB5, QN);
  playNote(noteB5, HN+QN);
  playNote(Rest, QN);
  playNote(noteB5, HN+QN);
  playNote(noteB5, QN);
  playNote(noteD6, HN);
  playNote(noteB5, HN);
  playNote(noteC6, HN+QN);
  playNote(noteC6, QN);
  playNote(noteC6, HN);
  playNote(noteB5, QN);
  playNote(noteA5, QN);
  playNote(noteD5, WN);
  playNote(noteFs5, WN);
  playNote(noteG5, WN);
}

void playNote(int note, int duration)
// This custom function takes two parameters, note and duration to make playing songs easier.
// Each of the notes have been #defined in the notes.h file. The notes are broken down by 
// octave and sharp (s) / flat (b).
{
  tone(buzzerPin, note, duration);
  delay(duration);
}

/***********************************************************************
 * Troubleshooting for experiment 4.1
 * My code won't upload!
 * - Make sure that your USB cable is plugged into the robot and the
 * computer you're using to write code.
 * - Make sure that the "Power" switch is switched to "ON".
 * - Double check that you have the right serial port selected under the
 * "Tools" menu. The easiest way to check is to see which item
 * disappears from the menu when you unplug the USB cable, and select
 * that one when you plug the board back in.
 * - Make sure the Serial Select switch at the top edge of the board is 
 * switched to "XBEE SW SERIAL", even if you have an Xbee attached.
 * - Check that you have the right board selected under the "Tools" menu.
 * The RedBot is Uno-compatible, so select "Arduino Uno" from the list.
 * My motors aren't turning!
 * - This code demonstrates only the tone() commands; there's no code to
 * make the motors turn.
 ***********************************************************************/

This experiment will play the beginning of "Twinkle Twinkle Little Star" 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? Camptown Races? Amazing Grace? or When the Saints go Marching? Pick a piece to compose as your "theme" song for your RedBot!

Troubleshooting

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.