本人菜鸟,所以对这个语言方面还有诸多不熟悉的地方,就大神检查一下,这应该是个很简单的程序,我不知道哪不对,反正就是不工作。
代码如下:
int yellowtrig = 8;
int blackecho = 9;
int distance = 0;
int motorPin1 = 3;
int motorPin2 = 4;
void setup(){
pinMode(yellowtrig, OUTPUT);
pinMode(blackecho, INPUT);
Serial.begin(9600);
}
void loop(){
{
digitalWrite(8,LOW);
delayMicroseconds(2);
digitalWrite(8,HIGH);
delayMicroseconds(10);
digitalWrite(8,LOW);
distance = pulseIn(9, HIGH);
distance = distance/58;
Serial.print(distance);
Serial.print("cm");
Serial.println(" ");
delay(1000);
}
if(distance <= 15){
digitalWrite(3,LOW);
digitalWrite(4,LOW);
}else{
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
}
} |