|
程序代码如下
#include <DCMotor.h>
DCMotor motor0(M0_EN, M0_D0, M0_D1);
DCMotor motor1(M1_EN, M1_D0, M1_D1);
const int TrigPin1 = 1;
const int EchoPin1 = 2;
const int TrigPin2 = 5;
const int EchoPin2 = 6;
float cm;
float ccm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin1, OUTPUT);
pinMode(EchoPin1, INPUT);
pinMode(TrigPin2, OUTPUT);
pinMode(EchoPin2, INPUT);
}
void loop()
{
motor0.setSpeed(-50.0);
motor1.setSpeed(50.0);
digitalWrite(TrigPin1, LOW); //低高低电平发一个短时光脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin1, LOW);
cm = pulseIn(EchoPin1, HIGH) / 58.0; //将回波时光换算成cm
cm = (int(cm * 100.0)) / 100.0; //保存两位小数
Serial.print(cm);
Serial.print("cm");
Serial.println();
digitalWrite(TrigPin2, LOW); //低高低电平发一个短时光脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin2, LOW);
ccm = pulseIn(EchoPin2, HIGH) / 58.0; //将回波时光换算成cm
ccm = (int(ccm * 100.0)) / 100.0; //保存两位小数
Serial.print(ccm);
Serial.print("cm");
Serial.println();
cm = min(cm,ccm);
if ( cm >=10)
else
motor0.brake();
motor1.brake();
delay(1000);
}
请大神们帮帮忙 |
|