void forward_for_n(float n) {
forward(); /* note that you can call any function from within another */
sleep(n);
ao();
}
void forward( ) {
fd(LEFT_MOTOR);
fd(RIGHT_MOTOR);
}
In the function forward_for_n, "n" is a placeholder for a specific value that
we expect someone to supply when the function is actually needed. Note
the function header:
void forward_for_n(float n)Inside of the parentheses we include the declaration
float nThis tells Interactive C that when the function is called, the caller will supply a value and the value is expected to be a real number.
To use the function (say, to make a robot go forward for 3 seconds), all you need to do is supply the value 3.0 in parentheses:
forward_for_n(3.0);We call "n" in the example above a formal parameter. We say that 3.0 is an actual parameter.
The syntax for supplying an actual parameter to a function should seem familiar, as you've seen it in the fd, bk, motor, and sleep function, among others.
All messages are printed to the LCD on the Handy Board. We can print cute messages like "I am a happy handy board" (recall Lab 2). We can also print status information that might be useful in debugging our programs.
To print a message such as "I'm printing...":
printf("I'm printing...\n");
Recall our program from last time:
/* A. Danyluk */
/* September 2006 */
int LEFT_MOTOR = 3; /* the left motor port */
int RIGHT_MOTOR = 1; /* the right motor port */
/* Move forward, spin a bit, straighten out, then slow to a stop */
/* when the stop button is pressed. */
void main( ) {
start_press();
forward();
sleep(3.0);
spin_a_bit();
forward();
stop_press();
slow_stop();
}
/* Move forward */
void forward( ) {
fd(RIGHT_MOTOR);
fd(LEFT_MOTOR);
}
/* Spin for 5 seconds */
void spin_a_bit( ) {
fd(LEFT_MOTOR);
bk(RIGHT_MOTOR);
sleep(5.0);
off(LEFT_MOTOR);
off(RIGHT_MOTOR);
}
/* Slow to a stop */
void slow_stop( ) {
motor(RIGHT_MOTOR, 20);
motor(LEFT_MOTOR, 20);
sleep(5.0);
off(RIGHT_MOTOR);
off(LEFT_MOTOR);
}
Let's modify it by adding a printf at the beginning of each function:
/* A. Danyluk */
/* September 2006 */
int LEFT_MOTOR = 3; /* the left motor port */
int RIGHT_MOTOR = 1; /* the right motor port */
/* Move forward, spin a bit, straighten out, then slow to a stop */
/* when the stop button is pressed. */
void main( ) {
printf("Starting main\n");
start_press();
forward();
sleep(3.0);
spin_a_bit();
forward();
stop_press();
slow_stop();
}
/* Move forward */
void forward( ) {
printf("In function forward\n");
fd(RIGHT_MOTOR);
fd(LEFT_MOTOR);
}
/* Spin for 5 seconds */
void spin_a_bit( ) {
printf("In function spin_a_bit\n");
fd(LEFT_MOTOR);
bk(RIGHT_MOTOR);
sleep(5.0);
off(LEFT_MOTOR);
off(RIGHT_MOTOR);
}
/* Slow to a stop */
void slow_stop( ) {
printf("In function slow_stop\n");
motor(RIGHT_MOTOR, 20);
motor(LEFT_MOTOR, 20);
sleep(5.0);
off(RIGHT_MOTOR);
off(LEFT_MOTOR);
}
Now as the program runs, the LCD will display where we are in the execution
of the program. This is generally not necessary in short programs, but can
be very useful when we're trying to debug a large, complex program and want to
pinpoint where the program fails.
In addition to printing messages, we can print values -- for example, sensor readings, results of computations, etc.
To print the value of a sensor - for instance, the value of the digital sensor in port 8:
printf("%d\n", digital(8));
To print a message and a value:
printf("Digital %d is %d\n", 10, digital(10));
if the value of digital sensor 10 is 0
In summary: