3.2.2.5 Software Design
In the above steps, we have tested the car's driving and obstacle avoidance module respectively, they
have achieved the desired results, here the "infrared obstacle avoidance" actually has been explained in this
section, but we have not put the programs of two parts together, so we now integrates the program of the
two parts and complete this great "infrared obstacle avoidance" project. First, let's read the complete program:
int
E1
= 5;
//PWMA
int
M1
= 9;
//DIRA****************************************left
int
E2
= 6;
//PWMB
int
M2
= 10;
//DIRB****************************************right
/* Define 4 motor control terminals, connected to IN1-IN4 on the motor drive board。*/
const int
leftPin
const int
rightPin
float
dl;
float
dr;
// Define two margins to store the values read by both sensors
void
setup()
{
Serial.begin(9600);
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
delay(1000);
}
void
loop()
{
dl
= analogRead(leftPin);
dr
= analogRead(rightPin);
them to the defined variables。
if
(dl
>=
38
&&
dr
or equal to 38 and the right value is less than or equal to 38, the following program
in {} is executed (dl> = 38, there is no obstacle on the left, dr <= 38 shows that
there is an obstacle on the right, so at this time the car is turning to the side
without obstacles (ie, turning to the left). From Figure 3.2.11, we know that the
simulated value will drop below about 35 in the event of an obstacle , But in order to
reduce the error, we set the threshold at 38 to prevent the car from judging the error
because of the error. We can also customize other values. If we use the digital port to
= A3;
= A4;
// Define the two signal receiving ends of the sensor
// Set the serial port baud rate to 9600,
// Set the working mode of two sensor pins, namely "input"
// Read the values collected by both sensors and assign
<= 38)
/* If the value collected by the left sensor is greater than
54
Need help?
Do you have a question about the Hummer-Bot and is the answer not in the manual?