Some Useful Routines for Interactive C

 

Function Purpose

Function Syntax

Turns on motor m at power level p. Power levels from 100 for full forward to -100 for full back.

void motor(int m, int p)

example: motor(0,59);

Turns off motor m.

void off(int m)

example: off(1);

Turns off all motors. ao is a short form for all off.

void ao()

example: ao();

Returns value of sensor port p, as a true/false (1=TRUE, 0=FALSE). If the reading is zero volts, digital() function will returns TRUE.

int digital(int p)

example: x = digital(7); % x is integer var

Returns value of button labeled Stop

int stop_button()

example: while (!stop_button)

Returns value of button labeled Start

int start_button()

example: while (!start_button)

Waits for the Stop button to be pressed, then released. Then issues a short beep and returns.

void stop_press()

example: stop_press();

Like stop_press(), but for the Start button

void start_press()

example: start_press();

Returns value of sensor port numbered p. Result is integer between 0 and 255.

int analog(int p)

example: s = analog(2); % s is integer var

Returns a value from 0 to 255 based on the position of a potentiometer.

int knob()

example: x = knob();

Resets the count of system time to zero milliseconds.

void reset_system_time()

example: reset_system_time();

Returns the count of system time in milliseconds.

long mseconds()

z = mseconds(); % z is long variable

Returns the count of system time in seconds, as a floating point number. Resolution is one millisecond.

float seconds()

example: t = seconds(); % t is float

Waits for an amount of time equal to or slightly greater than sec seconds.

void sleep(float sec)

example: sleep(1.2);

Waits for an amount of time equal to or slightly greater than msec milliseconds.

void msleep(long msec)

example: msleep(42873);

Produces a tone of 500 Hertz for a period of 0.3 seconds. Returns when the tone is finished.

void beep()

example: beep();

Produces a tone at pitch freq Hz for length seconds. Returns when the tone is finished.

void tone(float freq, float length)

example: tone(440.0, 0.2);

Sets the beeper tone to be freq Hz. The subsequent function is then used to turn the beeper on.

void set_beeper_pitch(float freq)

example: set_beeper_pitch(440.0)

Turns on the beeper at last frequency selected by the former function. The beeper remains on until the beeper_off function is executed.

void beeper_on()

example: beeper_on(); sleep(0.8);

Turns off the beeper.

void beeper_off()

example: beeper_off();

 

Review Sheet, Useful Routines

 

Write lines of code to do the following:

 

Action to perform

Number of lines of code

Write code here

Print the state of digital input 7 (1 or 0). You'll need the printf statement

1

Set the speed of motor 1 to 1/3 of the reading from the knob.

1

Robot go forward at half speed.

2

 

 

 

Robot spin clockwise at full speed.

2

 

 

 

Wait for the start button, then play a 256 Hz tone unit you hit the stop button.

3

 

 

 

 

Wait for the start button, then go full speed clockwise for 1 second, the full speed counterclockwise for 1 second.

7

 

 

 

 

 

 

 

 

Play the 'fate motive' from Beethoven's Fifth Symphony:

E, E, E, C.

Middle C is 261.6 Hz , E is 329.6 Hz.

 

 

Not required: If you want to try "Mary had a Little Lamb", the notes are: E, D, C, D, E, E, E.

D is 293.7 Hz.

About 16