Download MicroCamp2.0 - SparkFun Electronics

Transcript
MicroCamp : ATmega8 Activity Kit Manual l 107
#include
#include
#include
#include
#include
<stdlib.h>
<motor.h>
<sleep.h>
<sound.h>
<analog.h>
// For convert type of data
// Motor control
// Delay
// Analog to Digital Converter
void main()
{
unsigned int sensor=0;
unsigned char i=0;
sleep(200); sound(4000,50);
while(1)
{
sensor=0;
for (i=0;i<5;i++)
{
sensor=(sensor+analog(0));
}
sensor=(sensor/5);
if (sensor>260)
{
backward(50);sleep(800);
s_left(50);sleep(600);
}
else
{
forward(50);
}
}
}
// Main Program
// start with beep
// Read GP2D120 5 Times
// Average Data
// data more than 10 CM ?
// away from object
// do not any object "Let'go"
Program description
(1) Start with beep a sound.
(2) Read the data from GP2D120 and store into sensor variable. Read 5 times to calculate
the average for more precision.
(3) Check the value of sensor more than 320 or not. If more than, it means the obstacle is
far from the robot less than 8cm. Control the robot to move backward 0.8 second and turn left
0.6 second. The speed is set at 50%. You can change to any value fro more suitable operation.
(4) If the detect value is less than 320, the robot still move forward.
(5) Loop the opertion.
Listing A11-1 : The C program code for controlling the MicroCamp robot to
avoid the obstacles without any physical contact with the application of
GP2D120 module.
INNOVATIVE EXPERIMENT