在下载到开发板之前要选择好板和端口,具体参见:
代码
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,7,6,5,4,3);
int Echo = A1;
int Trig =A0;
int 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;
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);
}
void run()
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,150);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,150);
}
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()
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,150);
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);
}
void spin_left(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,150);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,150);
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,150);
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);
}
}
void Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
Serial.print("Distance:");
Serial.println(Fdistance);
Distance = Fdistance;
}
void Distance_display()
{
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 loop()
{
keysacn();
while(1)
{
Distance_test();
Distance_display();
if(Distance < 40)
while(Distance < 40)
{
back(2);
right(1);
digitalWrite(beep,HIGH);
Distance_test();
Distance_display();
}
else
{
run();
digitalWrite(beep,LOW);
}
}
}