Code Snippets
Infrared emitter/detector pair:
The following code example will vary the speed at which the beeper
sounds as you move closer/ farther way from the sensor.
In this example the sensor is plugged into sensor port 2.
void main()
{
while (1) {
beep();
sleep(analog(2)/6);
}
}
Digital Compass (Dinsmore 1490):
The code example to the left, will display a 1 or 0 next to the letters
N, E, S, & W . The value of 1 indicates that the sensor is facing that
direction. If 1 is displayed twice, then the sensor is facing between
two of the cardinal directions such as SE or NW.
void main()
{
while (1) {
printf("N%d E%d S%dW%d\n",
digital(13), digital(28),
digital(27), digital(26));
sleep(0.15);
}
}
Passive Infrared Detector (PIR KC778B):
The following code example will trigger the Handy Board beeper when
motion occurs within the range of the PIR motion detector.
void main()
{
while (1) {
if (digital(8) == 0)
beep();
sleep(0.25);
}
}
Servo Motor:
The following line of code typed at the Interactive C command line will
move the servo to a different position. The first line just initiates the
servo controller, so it only needs to be typed once per session.
init_expbd_servos(1);
servo0 = 1500;
servo0 = 100;
servo0 = 3000;
Note that servos vary in the input range that they will accept.
You can do damage to the servo by sending values that are out
of range. It is best to start with a value of 1500, this is
approximately the middle. From here try adding and
subtracting 100 to see how far the servo will move without
hitting its limit (unit will vibrate indicating motor is trying
to move past its physical limit).
*Back to the Intro Page*