用的是nano板,单独连接一个hcsr04是正常的,但当我连接电机驱动板并输出控制信号就不正常了,总频率100Hz时返回的距离不时显示0cm,再高一点就一直返回0了,似乎这两个模块会互相干扰似的
附源码:
class Ultrasonic_ranging_module
{
public:
int m_trigPin;
int m_echoPin;
Ultrasonic_ranging_module(int trigPin,int echoPin){
m_trigPin = trigPin;
m_echoPin = echoPin;
pinMode(m_trigPin,OUTPUT);
pinMode(m_echoPin,INPUT);
Serial.println("超声波测距模块初始化完成");
}
int ranging_cm(){
digitalWrite(m_trigPin,LOW);
delayMicroseconds(5);
digitalWrite(m_trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(m_trigPin,LOW);
pinMode(m_echoPin,INPUT);
return (pulseIn(m_echoPin,HIGH)*0.034) / 2;
}
int ranging_inches(){
digitalWrite(m_trigPin,LOW);
delayMicroseconds(5);
digitalWrite(m_trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(m_trigPin,LOW);
pinMode(m_echoPin,INPUT);
return (pulseIn(m_echoPin,HIGH)*0.0135) / 2;
}
};
long cm;
Ultrasonic_ranging_module u1(A0,A1);
float w1 = 1;
float w2 = 1;
int Int1 = 0;
int Int2 = 100;
int Int3 = 0;
int Int4 = 100;
int Pin1 = 3;
int Pin2 = 11;
int Pin3 = 5;
int Pin4 = 9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Pin1,OUTPUT);
pinMode(Pin2,OUTPUT);
pinMode(Pin3,OUTPUT);
pinMode(Pin4,OUTPUT);
Serial.println("设备正常");
}
void loop() {
// put your main code here, to run repeatedly:
cm = u1.ranging_cm();
//if(cm <= 4){
//Int1 = 0;
//}
analogWrite(Pin1,Int1);
analogWrite(Pin2,Int2);
analogWrite(Pin3,Int3);
analogWrite(Pin4,Int4);
Serial.print(cm);
Serial.print("cm");
Serial.println();
}