Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. int getDistanza(){
  2.     int distanza;
  3.   unsigned long tempo;
  4.  
  5.     digitalWrite(TRIG_PIN, HIGH);
  6.     delayMicroseconds(10);
  7.     digitalWrite(TRIG_PIN, LOW);
  8.  
  9.     tempo = pulseIn(ECHO_PIN, HIGH);
  10.     distanza = (0.033765 * tempo/2);
  11.     delayMicroseconds(10);
  12.  
  13.   return distanza;
  14. }
  15.  
  16.  
  17.  
  18.   int distanza;
  19.  
  20.   for(int i=0; i<10; i++){
  21.     distanza += getDistanza();
  22.   }
  23.  
  24.   distanza = distanza/10;
  25.   Serial.println("Distanza: " + String(distanza) + "cm");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement