請依下圖接線 請鍵入下列程式:(以 com 串列訊息室窗觀察) #define trigP 6 #define echoP 7 const int T25 = 29 , samplePeriod = 50; int Distance, Duration; unsigned long t0 = 0; void setup() { Serial.begin(9600); pinMode(trigP,OUTPUT); pinMode(echoP,INPUT); } void loop() { if(millis()-t0>=samplePeriod){ usonicRead(); t0 = millis(); Serial.print("距離: ");Serial.print(Distance);Serial.println(" 公分."); } } void usonicRead(){ digitalWrite(trigP,HIGH); delayMicroseconds(10); digitalWrite(trigP,LOW); Duration = pulseIn(echoP,HIGH)/2; Distance = Duration/T25; } |