Computer Science 014 |
Lecture 3: More on Interactive C
Date: Friday, January 5, 2007
Today's lecture location: TCL 206
1+2;
1.0+2.0;
1+2.0;
1+(int)(2.0);
printf("\nTwenty %d",20);
printf("\nTwenty %f",20.0);
void main() { printf("\nHello, world!"); }We can print more than just simple text. Replace
printf("\nHello, world!");with the lines
int i; for (i=0; i<5; i=i+1 ) { printf("%d ",i);} }This program uses a simple for loop to count from 0 to 4 and print the current value of the looping variable, i, on the LCD screen. The for loop requires a variable to contain the count, a start and end condition for the loop, and some operation to change the loop counter at the end of each loop. In this example, i is the loop variable, it is set equal to 0 at the start by the expression i=0, the loop continues as long as i is less than 5, and the loop variable is incremented by i=i+1 . Note that braces (curly brackets) are used to enclose the statements that are to be repeated by the loop.
void main() { int reading; while ( ! stop_button() ) { reading = analog(3); printf("\nLight level = %d", reading); sleep(0.5); } printf("\nAll done"); }Download and run the program. Vary the amount of light shining on the photoresistor and observe how the reading on the Handy Board LCD changes. When you are ready, hold down the Stop button on the Handyboard to terminate the program.
reading = analog(3); printf("\nLight level = %d", reading);are replaced by the single line
printf("\nLight level = %d", analog(3) );the program will still do the same thing. The use of the name reading in this program, however, does make it a bit clearer what the program is doing. If we had instead used the name brightnessReading it would make our intent even clearer. Generally, associating names with the values a program uses is a good thing because it allows the programmer to make the roles of the values clearer. For example, you could introduce names such as SENSOR_PORT and DELAY_TIME to refer to the values 3 and 0.5 that indicate which port the light sensor is attached to and how long the program should pause between readings. Change the program to use such names.
void main(){ int THRESHOLD = 20; int LIGHT_SENSOR = 3; int TOUCH_SENSOR = 8; int MOTOR_PORT = 0; int SPEED = 60; int reading; while ( ! digital( TOUCH_SENSOR ) ) { reading = analog( LIGHT_SENSOR ); if ( reading > THRESHOLD ) { motor( MOTOR_PORT, SPEED ); } else { motor( MOTOR_PORT, -SPEED ); } motor(MOTOR_PORT, 0); }Run this program. Vary the amount of light that strikes the photocell and observe. When the cell is in bright light, the motor should run in one direction. When the photocell is covered, the motor should reverse. The if statement instruction tells the program how to choose between the statements that determine the motor's direction. It states that if the reading's value is greater than the number associated with threshold, the speed should be positive and that if the reading is less than or equal to the threshold, then the speed should be negative. The loop causes the program to follow this rule for setting the motor's direction over and over again until the touch sensor is pressed.
int itsDark( int sensor ) { int THRESHOLD = 20; int reading; reading = analog( sensor ); return reading > THRESHOLD; }After downloading the program, in the interaction window, type
itsDark(3);and press return while covering the light sensor. The result should be 1. In Interactive C, 1 stands for true and 0 stands for false. The last line of the function says to return whether reading > threshold is true or false as the result of the function evaluation. The result 1 therefore says that it is true that it is dark. Try it a few more times while alternately exposing the sensor to light or covering it. Notice how this experiment resembles the experiments you performed earlier in the lab with the analog and digital functions. The definition you just typed in has expanded the collection of builtin functions with our itsDark function.
reading = analog( LIGHT_SENSOR ); if ( reading > THRESHOLD ) {by the single line
if ( itsDark( LIGHT_SENSOR ) ) {Download the revised program and run it to make sure it still works.
#use "music.c" void main() { int playerProcess; /* global variable for play_pp() process id */ playerProcess = start_process( play_pp() ); /* turn on the music */ stop_press(); /* wait for stop button to be pressed */ kill_process(playerProcess); /* stop pressed, so kill the music */ beep(); /* needed to kill the tone generator */ printf("\nAll done!"); /* say we're done */ } void play_pp() { printf("\nPlaying..."); while(1) { pp(); /* Play the Pink Panther */ } }Download and run this program. The music from The Pink Panther should play repeatedly. When you get tired of this, press the stop button. The music should stop, and the "All done!" message will be printed on the LCD screen.