程式碼部分:
void setup(){
Serial.begin(9600);
pinMode(9, OUTPUT);//trig
pinMode(10, INPUT);//echo
pinMode(7, OUTPUT);//蜂鳴器高電平
pinMode(4, OUTPUT);//蜂鳴器低電平
digitalWrite(4, LOW);
}
void loop(){
digitalWrite(9, LOW);
delayMicroseconds(2);
digitalWrite(9, HIGH);
delayMicroseconds(10);
digitalWrite(9, LOW);
float d = pulseIn(10, HIGH) / 58.00;
delay(10);
if(d<15){//距離小於15報警
digitalWrite(7, HIGH);
}
Serial.println(d);//串列埠列印距離
delay(80);
digitalWrite(7, LOW);
}
最新評論