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: Write a command that plays a random song.
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). Note that each motor port on the handy board has 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 |
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).
| 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 |
| if (digital(7)) bk(1); | turn motor on 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 for several seconds. Then return to the default behavior. If this were connected to a robot what kind of behavior would be produced?
Analog Sensors
Attach all the rest of your sensors to the handy board's analog area. Put the light sensors in slots 0-1 and skip slot 2. Put the infra red sensors in slots 3-4 and skip slot 5. Finally put the temperature sensor in slot 6. Rather than testing each of these individually we will use a previously written testing program. Go to the File menu and select Load. Find hbtest.c and Open it. To see the contents of this file, go back to the File menu and select Open. Again select the same file and Open it. Find the function testanalogs() and read through it. Then execute the function.
| testanalogs(); | follow the prompts |
Notice that unused ports will show the maximum value of 255. Which sensor's value is displayed first? 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. 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
Come up with a catchy name for your team. Open up a new file and give it your team name appended with a ".c" Practice creating some of your own functions modeled after the ones in hbtest.c. You could start with the ever popular hello world program. Then load your file and test your functions.
Exercises for this week
In the next lab session, you will demonstrate three simple creatures. All the creatures can use the same vehicle base. All three of them will require a single light sensor.
Creature#1: Timid
Timid is capable of moving forward in a straight line. It has one threshold light sensor, pointing up. When the light sensor detects light, the creature moves forward, otherwise, it stays still. The threshold of the light sensor can be adjusted using the HandyBoard knob. The threshold of the light sensor should be set to ambient light. That way, when the creature can "see" the light, it will move. When it enters a shadow (which can be cast by a hand or another object) it stops. If whatever is casting the shadow is moved, the creature will move again. Therefore, timid is a shadow seeker.
Creature#2: Indecisive
Indecisive is similar to Timid, except, it never stops: its motors are always running, either in forward direction, or in reverse direction, controlled by the threshold light sensor. When the light sensor detects light, it moves forward, otherwise, it moves backwards. When you run this creature, you will notice that it tends to oscillate back and forth at shadow edges. Thus, Indecisive is a shadow edge seeker.
Creature#3: Paranoid
Paranoid is capable of turning. This is accomplished by moving the right motor forward and moving the left motor in reverse direction at the same time. Its threshold light sensor is pointed up and is attached on an arm that extends forward a few inches from the body. When the sensor detects light, it moves forward. When the sensor enters a shadow, it reverses the direction of its left motor, thus turning right. Soon the sensor will swing around, out of the shadow. When that heppens, it resumes its forward motion. Paranoid, is a shadow fearing creature.
Collecting Reactions
Once completed, you should invite some friends to observe the three creatures and record their reactions for discussion.
Backgroud: These creatures were designed by David Hogg, Fred Martin, and Mitchel Resnick of the MIT Media Laboratory. They are inspired by a set of thought experiments designed by Valentino Braitenberg in his book, Vehicles: Experiments in Synthetic Psuchology, MIT Press, 1984. In the book, Braitenberg describes a set of increasingly complex vehicles built from simple mechanical and electronic components. Each of the imaginary vehicles mimics intelligent behavior in some way and each is given a name that corresponds to the behavior it imitates: Fear, Love, Values, Logic, etc.Braitenberg uses these thought experiments to explore psychological ideas and the nature of intelligence. Intricate behaviors seem to emerge from interactions of simple component parts. Hogg et al used specialized electronic LEGO bricks to build these creatures. For more details, see their paper Braitenberg Creatures.