Datasheet

BOE Shield-Bot Navigation • Chapter 4
Robotics with the BOE Shield-Bot 121
of -32,768 to 32,767. Here, the term int Hz in the parentheses defines a parameter for
the
pitch function; in this case, it declares a local variable Hz of data type int.
Local variables, remember, are declared within a function, and can only be seen and used inside
that function. If a local variable is created as a parameter in the function declaration, as void
pitch(int Hz)
is here, initialize it by passing a value to it each time the function is called. For
example, the call pitch(3500) passes the integer value 3500 to the pitch function’s int Hz
parameter.
So, when the first function call to pitch is made with pitch(3500), the integer value 3500
gets passed to
Hz. This initializes Hz to the value 3500, to be used during this trip through
the
pitch function’s code block. The second call, pitch(2000), initializes Hz to 2000 during
the sketch’s second trip through the
pitch function’s code block.
void setup() {
Serial.begin(9600);
pitch(3500);
Serial.println("Playing high pitch tone...");
delay(1000);
pitch(2000);
Serial.println("Playing lower pitch tone...");
delay(1000);
}
void loop()
{
}
void pitch(int Hz)
{
Serial.print("Frequency = ");
Serial.print(Hz);
tone(4, Hz, 1000);
delay(1000);
}
Example Sketch FunctionCallWithParameter
Read the sketch, and predict what you will see and hear when you run it.
Create, save, and run FunctionCallWithParameter, then open the Serial Monitor.
Watch your terminal and listen to the tones.
(1) Call sends sketch to function…
(3) Function executes with
Hz = 3500.
(4) No more codereturn to next instruction in sketch.
(5) Pass 2000 to Hz.
(6) Function
executes with
Hz = 2000
(2)…passing 3500 to Hz.