在下载到开发板之前要选择好板和端口,具体参见:
代码
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,7,6,5,4,3);
int Echo = A1;
int Trig =A0;
int Front_Distance = 0;
int Left_Distance = 0;
int Right_Distance = 0;
int Left_motor_go=8;
int Left_motor_back=9;
int Right_motor_go=10;
int Right_motor_back=11;
int key=A2;
int beep=A3;
int servopin=2;
int myangle;
int pulsewidth;
int val;
void setup()
{
Serial.begin(9600);
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
pinMode(key,INPUT);
pinMode(beep,OUTPUT);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
lcd.begin(16,2);
pinMode(servopin,OUTPUT);
}
void run()
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,165);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,160);
}
void brake(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
delay(time * 100);
}
void left(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,180);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void spin_left(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void right(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
delay(time * 100);
}
void spin_right(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,150);
delay(time * 100);
}
void back(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,150);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void keysacn()
{
int val;
val=digitalRead(key);
while(!digitalRead(key))
{
val=digitalRead(key);
}
while(digitalRead(key))
{
delay(10);
val=digitalRead(key);
if(val==HIGH)
{
digitalWrite(beep,HIGH);
while(!digitalRead(key))
digitalWrite(beep,LOW);
}
else
digitalWrite(beep,LOW);
}
}
float Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
return Fdistance;
}
void Distance_display(int Distance)
{
if((2<Distance)&(Distance<400))
{
lcd.home();
lcd.print(" Distance: ");
lcd.setCursor(6,2);
lcd.print(Distance);
lcd.print("cm");
}
else
{
lcd.home();
lcd.print("!!! Out of range");
}
delay(250);
lcd.clear();
}
void servopulse(int servopin,int myangle)
{
pulsewidth=(myangle*11)+500;
digitalWrite(servopin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
delay(20-(pulsewidth*0.001));
}
void front_detection()
{
for(int i=0;i<=5;i++)
{
servopulse(servopin,90);
}
Front_Distance = Distance_test();
}
void left_detection()
{
for(int i=0;i<=15;i++)
{
servopulse(servopin,175);
}
Left_Distance = Distance_test();
}
void right_detection()
{
for(int i=0;i<=15;i++)
{
servopulse(servopin,5);
}
Right_Distance = Distance_test();
}
void loop()
{
keysacn();
while(1)
{
front_detection();
if(Front_Distance < 30)
{
brake(2);
back(2);
brake(2);
left_detection();
Distance_display(Left_Distance);
right_detection();
Distance_display(Right_Distance);
if((Left_Distance < 30 ) &&( Right_Distance < 30 ))
spin_left(0.7);
else if(Left_Distance > Right_Distance)
{
left(3);
brake(1);
}
else
{
right(3);
brake(1);
}
}
else
{
run();
}
}
}