根据吧里某位大神提供的代码改的
但不知道做在哪里
求教QAQ
const int EchoPin = 7;
const int leftmotorpin1 = 3;
const int leftmotorpin2 = 5;
const int enablePin1=4;
const int rightmotorpin1 =10;
const int rightmotorpin2 = 9;
const int enablePin1=8;
int currDist = 0; // 距离
void setup()
{
pinMode(EchoPin, OUTPUT);
pinMode(leftmotorpin1, OUTPUT);
pinMode(leftmotorpin2, OUTPUT);
pinMode(enablePin1,OUTPUT);
digitalWrite(enablePin1,HIGH);
pinMode(rightmotorpin1, OUTPUT);
pinMode(rightmotorpin2, OUTPUT);
pinMode(enablePin2,OUTPUT);
digitalWrite(enablePin2,HIGH);
}
void loop()
{
currDist = MeasuringDistance(); //距离
if(currDist > 20)
{
nodanger();
}
else if(currDist < 15)
{
backup();
randTrun();
}
else
{
randTrun();
}
} //测量距离 单位厘米
ong MeasuringDistance()
{
long duration;
digitalWrite(TrigPin, LOW); //初始化,延时2ms
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH); //输出高电平,延时10ms
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH); //接受返回信号
return duration /58; //计算距离
}
//定义各动作
// 前进
void nodanger()
{
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, LOW);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, LOW);
return;
}
//后退
void backup()
{
digitalWrite(leftmotorpin1, LOW);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, LOW);
digitalWrite(rightmotorpin2, HIGH);
delay(1000);
}
//暂停0.5s
void totalhalt() {
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, HIGH);
return;
delay(500);
}
//左转
void body_lturn()
{
digitalWrite(leftmotorpin1, LOW);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, LOW);
delay(1500);
totalhalt();
}
//右转
void body_rturn()
{
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, LOW);
digitalWrite(rightmotorpin1, LOW);
digitalWrite(rightmotorpin2, HIGH);
delay(1500);
totalhalt();
}
//产生随机数
void randTrun()
{
long randNumber;
randomSeed(analogRead(0));
randNumber = random(0, 10);
if(randNumber > 5)
{
body_rturn();
}
else
{
body_lturn();
}
}
但不知道做在哪里
求教QAQ
const int EchoPin = 7;
const int leftmotorpin1 = 3;
const int leftmotorpin2 = 5;
const int enablePin1=4;
const int rightmotorpin1 =10;
const int rightmotorpin2 = 9;
const int enablePin1=8;
int currDist = 0; // 距离
void setup()
{
pinMode(EchoPin, OUTPUT);
pinMode(leftmotorpin1, OUTPUT);
pinMode(leftmotorpin2, OUTPUT);
pinMode(enablePin1,OUTPUT);
digitalWrite(enablePin1,HIGH);
pinMode(rightmotorpin1, OUTPUT);
pinMode(rightmotorpin2, OUTPUT);
pinMode(enablePin2,OUTPUT);
digitalWrite(enablePin2,HIGH);
}
void loop()
{
currDist = MeasuringDistance(); //距离
if(currDist > 20)
{
nodanger();
}
else if(currDist < 15)
{
backup();
randTrun();
}
else
{
randTrun();
}
} //测量距离 单位厘米
ong MeasuringDistance()
{
long duration;
digitalWrite(TrigPin, LOW); //初始化,延时2ms
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH); //输出高电平,延时10ms
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH); //接受返回信号
return duration /58; //计算距离
}
//定义各动作
// 前进
void nodanger()
{
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, LOW);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, LOW);
return;
}
//后退
void backup()
{
digitalWrite(leftmotorpin1, LOW);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, LOW);
digitalWrite(rightmotorpin2, HIGH);
delay(1000);
}
//暂停0.5s
void totalhalt() {
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, HIGH);
return;
delay(500);
}
//左转
void body_lturn()
{
digitalWrite(leftmotorpin1, LOW);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, LOW);
delay(1500);
totalhalt();
}
//右转
void body_rturn()
{
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, LOW);
digitalWrite(rightmotorpin1, LOW);
digitalWrite(rightmotorpin2, HIGH);
delay(1500);
totalhalt();
}
//产生随机数
void randTrun()
{
long randNumber;
randomSeed(analogRead(0));
randNumber = random(0, 10);
if(randNumber > 5)
{
body_rturn();
}
else
{
body_lturn();
}
}