Beeper, Display, Loops, Numbers
beep(); | beeps once |
while (1) { beep(); sleep(1.0); } | beeps, waits one second, repeats; click on stop sign to end infinite loop |
{int i; for (i=0; i<5; i++){beep(); sleep(.3)}} | beeps five times in quick succession |
tone(60.0,1.0); | tone takes a frequency and length |
tone(250.0,2.0); | use frequencies in range 20.0-300.0 |
beeper_off(); | turns beeper off |
printf("hello world\n"); | prints message to LCD on board |
printf("%d %d %d",2,2*2,2*4); | prints 2 4 8 to LCD on board |
random(10); | returns an int in range 0-9 |
Exercise: Use a for loop to play a random
song. Since tone expects a float and random returns
an int, you'll need to cast the result. For exampe:
(float)random(10).
Motors
Attach the motor connectors to the two lego motors and connect them to the handy board ports 1 and 3 (refer back to figure 1 in the manual if necessary). The port closest to the telephone cable is port 0. Note that each motor port on the handy board as an associated pair of lights, one green and one red. Green indicates forward motion and red backward.
{fd(1); sleep(2.0); off(1);} | motor 1 on full forward for 2 seconds |
{bk(1); bk(3);} | motors 1 & 3 on full backward |
alloff(); | all motors off |
ao(); | shorthand version of same command |
motor(1,-50); | motor 1 at half power backward |
motor(3,80); | power ranges from -100 to 100 |
off(1); | turns a specific motor off |
Exercise Write a series of commands to make your robot move forward for 1 second, turn right 90 degrees and then stop.
Digital Sensors
Attach the two touch sensors (or switches) to the handy board digital ports 7 and 15 (refer back to figure 1 in the manual if necessary). Remember to select "stop" to interrupt execution of infinite loops.
while(1) {printf("%d\n", digital(7)); sleep(.1);} | continuously show seventh digital value enter this in IC as a single line |
{while(!digital(15)) {printf("clear\n");} printf("touch\n");} | wait for last digital sensor to be triggered |
while (1) {if (digital(7)) bk(1); else off(1);} | reverse motor when digital sensor is triggered |
Exercise: Create a loop where the default behavior is for both motors to be on full forward. When one of the touch sensors is depressed, reverse each motor a separate random amount between 50 and 100 for several seconds. Then return to the default behavior.
Analog Sensors
Analog ports 0 and 1 are reserved for attaching an expansion board. So you should only use analog ports 2 through 6. Put a light sensor in slot 2. Put an infra red sensor in slot 3. Put a bend sensor in slot 4. Finally put a temperature sensor in slot 6.
Rather than testing each of these individually we will use a previously written testing program. Select "open" and find the file hbtest.c. Select "download". After the download is complete, turn your board off and on to run the program. Follow the instructions on the LCD. First turn the knob to select testing of analogs, and press the start button. Then turn the knob to select which port. Our board only has ports 2-6, but this program assumes we have an expansion board with many more ports. Select each port 2-6 in turn and see how each sensor responds.
Notice that unused ports will show the maximum value of 255. Check out the sensitivity of the light sensors by covering them with your finger, pointing them at the ceiling lights, or using the flashlight. Check out the sensitivity of the infra red sensors by moving them towards and away from a solid object such as your open hand or the table top. Check out the sensitivity of the bend sensor by bending it in both directions. Which direction is more sensitive? Finally test out the sensitivity of the temperature sensor by putting it close to the flashlight when it is on or by breathing on it. Press the STOP button on the handy board to quit.
Files and Functions
Select "new" to get a blank file and try creating a simple controller for your robot so that it will react to some change in the sensors. Note: When using the ic editor, avoid using the tab key, it seems to cause problems. If your program has a main procedure, then after you download it and turn the handyboard on and off, the program will automatically begin executing. If your program does not have a main procedure, then you will have to invoke it from the interaction window.
void main() { ... }