bangnaga

Arduino Sensor Ketinggian Air

Oct 10th, 2014
275
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const int pingPin = 7;
  3.  
  4. // Deklarasi Sensor PING
  5. const int hmax = 5; //5 cm
  6. const int tinggisensor = 150;
  7. int ketinggian;
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12. }
  13.  
  14. void loop()
  15. {
  16.  
  17.   //Baca Ketinggian Air Asusmsi ketinggian sensor ke permukaan 100 cm
  18.   int jarak;
  19.   jarak = bacajarak();
  20.   Serial.print("  Ketinggian: ");
  21.   Serial.println(jarak);
  22.   delay (500);
  23.  }
  24.  
  25.  
  26. int bacajarak(){
  27.   int result;
  28.   long duration, cm;
  29.   pinMode(pingPin, OUTPUT);
  30.   digitalWrite(pingPin, LOW);
  31.   delayMicroseconds(2);
  32.   digitalWrite(pingPin, HIGH);
  33.   delayMicroseconds(5);
  34.   digitalWrite(pingPin, LOW);
  35.   pinMode(pingPin, INPUT);
  36.   duration = pulseIn(pingPin, HIGH);
  37.   cm = mmtocm(duration);
  38.   ketinggian = tinggisensor - cm;
  39.   result = ketinggian;
  40. }  
  41.  
  42. long mmtocm(long microseconds)
  43. {
  44.   return microseconds / 29 / 2;
  45. }
RAW Paste Data