Datasheet

Chapter 4 • BOE Shield-Bot Navigation
122Robotics with the BOE Shield-Bot
// Robotics with the BOE Shield FunctionCallWithParameter
// This program demonstrates a function call with a parameter.
void setup() {
Serial.begin(9600);
Serial.println("Playing higher pitch tone...");
pitch(3500); // pitch function call passes 3500 to Hz parameter
delay(1000);
Serial.println("Playing lower pitch tone...");
pitch(2000); // pitch function call passes 2000 to Hz parameter
delay(1000);
}
void loop()
{
}
void pitch(int Hz) // pitch function with Hz declared as a parameter
{
Serial.print("Frequency = ");
Serial.println(Hz);
tone(4, Hz, 1000);
delay(1000);
}
Was your prediction correct? If so, great! If not, take a closer look at the sketch and make
sure you can follow the code from each function call to the function and back. For
clarification, take another look at the previous diagram.
Your Turn Expand Function to Two Parameters
Here is a modified pitch function that accepts two parameters: Hz and ms. This new pitch
function controls how long the tone lasts.
void pitch(int Hz, int ms)
{
Serial.print("Frequency = ");
Serial.println(Hz);
tone(4, Hz, ms);
delay(ms);
}
Here are two calls to the modified pitch function, one for a 0.5 second 3500 Hz tone, and the
other for a 1.5 second 2000 Hz tone: