Advertisement
Tempist

SurilliGSM Ultrasonic [HC-SR04 and 16x2 LCD]

Jul 10th, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. //Twitter: @ItsWizard | Instagram: @imWizardly | Discord: wizard#7815
  2.  
  3. #include <LiquidCrystal.h>
  4. //Make sure to include the LC lib
  5.  
  6. LiquidCrystal lcd(6, 11, 5, 12, 2, 3); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7).
  7.  
  8. const int trigPin = 9;
  9. const int echoPin = 10;
  10.  
  11. long duration;
  12.  
  13. int distanceCm, distanceInch;
  14.  
  15. void setup()
  16.  
  17. {
  18.    
  19.     lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display.
  20.     pinMode(trigPin, OUTPUT);
  21.     pinMode(echoPin, INPUT);
  22.    
  23. }
  24.  
  25. void loop()
  26.  
  27. {
  28.    
  29.     digitalWrite(trigPin, LOW);
  30.     delayMicroseconds(2);
  31.     digitalWrite(trigPin, HIGH);
  32.     delayMicroseconds(10);
  33.     digitalWrite(trigPin, LOW);
  34.     duration = pulseIn(echoPin, HIGH);
  35.     distanceCm= duration*0.034/2;
  36.     distanceInch = duration*0.0133/2;
  37.     lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed.
  38.     lcd.print("Distance: "); // Prints string "Distance" on the LCD.
  39.     lcd.print(distanceCm); // Prints the distance value from the Ultrasonic Sensor (HC-SR04).
  40.     lcd.print(" cm");
  41.     delay(10);
  42.     lcd.setCursor(0,1);
  43.     lcd.print("Distance: ");
  44.     lcd.print(distanceInch);
  45.     lcd.print(" inch");
  46.     delay(10);
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement