Advertisement
thelostarc

HCSR04 with Cm Display with bargraph REVERSE

Jun 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #define trigPin 12
  2. #define echoPin 13
  3. #define MAX_DISTANCE 35
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6. #include <LcdBarGraphRobojax.h>
  7. LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  8. LcdBarGraphRobojax lbg(&lcd, 16, 0, 0);
  9.  
  10. void setup()
  11. {
  12. lcd.begin(); // initialize the lcd
  13. lcd.backlight();
  14. lcd.setCursor(0, 1);
  15. pinMode(trigPin, OUTPUT);
  16. pinMode(echoPin, INPUT);
  17. }
  18. void loop()
  19. {
  20. long duration, distance;
  21. digitalWrite(trigPin, LOW); // Added this line
  22. delayMicroseconds(2); // Added this line
  23. digitalWrite(trigPin, HIGH);
  24. delayMicroseconds(10); // Added this line
  25. digitalWrite(trigPin, LOW);
  26. duration = pulseIn(echoPin, HIGH);
  27. distance = duration*0.034/2;
  28.  
  29. lbg.clearLine(1);
  30. lbg.drawValue( 10, duration*0.034/2); //Jika value sepuluh 10 atau batas cm di tukar dengan duration sebelahnya maka graph aka terbalik dari kecil ke besar
  31. delay(100);
  32. lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
  33. lcd.print("Distance: "); // Prints string "Distance" on the LCD
  34. lcd.print(distance); // Prints the distance value from the sensor
  35. lcd.print(" cm ");
  36. delay(500);
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement