29
{
delay(10); //delay 10ms
val=digitalRead(key);// Reads the button ,the level value assigns to val
if(val==LOW) //Double check the button is pressed
{
digitalWrite(beep,LOW);//The buzzer sounds
delay(50);//delay 50ms
while(!digitalRead(key))
//Determine if the button is released or not
digitalWrite(beep,HIGH);//mute
}
else
digitalWrite(beep,HIGH);//mute
}
}
/*main loop*/
void loop()
{
keysacn(); //Press the button to start
while(1)
{
/**************************************************************************************
Infrared signal back means there is something obstacled ,returns low level and led
lights up.
Infrared signal gone means there is nothing obstacled ,returns high level and led lights
off.
**************************************************************************************/
SR_2 = digitalRead(SensorRight_2);//Right infrared sensor detects the obstacle,then
LED[L5] light illuminates and otherwise it goes off.
SL_2 = digitalRead(SensorLeft_2);//Left infrared sensor detects the obstacle,then
LED[L4] light illuminates and otherwise it goes off.
if (SL_2 == LOW&&SR_2==LOW)// Black lines were not detected at the same time
run(); // go ahead
else if (SL_2 == HIGH & SR_2 == LOW)// Left sensor against white undersurface and
right against black undersurface , the car left off track and need to adjust to the right.
right();
else if (SR_2 == HIGH & SL_2 == LOW) // Rihgt sensor against white undersurface
and left against black undersurface , the car right off track and need to adjust to the left.
left();
else // Black lines were detected at the same time , the car stop.
brake();
}
}
Experimental steps:
1. We need to open the code of this experiment:
Tracking.ino
, click
“
√
”
under the menu
bar to compile the code, and wait for the word "
Done compiling
" in the lower right
corner, as shown in the figure below.