1. #define PIN_TRIG 12  
  2. #define PIN_ECHO 11  
  3.     
  4. float cm;  
  5. float temp;  
  6.     
  7. void setup() {    
  8.   Serial.begin(9600);    
  9.   pinMode(PIN_TRIG, OUTPUT);    
  10.   pinMode(PIN_ECHO, INPUT);    
  11. }    
  12.     
  13. void loop() {  
  14.   digitalWrite(PIN_TRIG, LOW);  
  15.   delayMicroseconds(2);  
  16.   digitalWrite(PIN_TRIG, HIGH);  
  17.   delayMicroseconds(10);  
  18.   digitalWrite(PIN_TRIG, LOW);  
  19.       
  20.   temp = float(pulseIn(PIN_ECHO, HIGH));  
  21.   cm = (temp * 17 )/1000;  
  22.     
  23.   Serial.print("Echo = ");    
  24.   Serial.print(temp);  
  25.   Serial.print(",  Distance = ");    
  26.   Serial.print(cm);  
  27.   Serial.println("cm");    
  28.   delay(300);    
  29. }   
  30. Arduino-----超声波测距

相关文章:

  • 2022-12-23
  • 2021-12-23
  • 2022-01-02
  • 2022-01-02
  • 2022-01-02
  • 2021-11-18
  • 2021-12-29
猜你喜欢
  • 2021-12-06
  • 2021-07-05
  • 2021-06-27
  • 2021-11-28
  • 2021-10-02
  • 2022-12-23
相关资源
相似解决方案