Advertisement
DrAungWinHtut

ultrasonic_sensor.ino

Jun 24th, 2022
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // C++ code
  2. // https://learn.adafruit.com/i2c-spi-lcd-backpack/arduino-i2c-use
  3. #include <Adafruit_LiquidCrystal.h>
  4.  
  5. int seconds = 0;
  6. int trigger = 6;
  7. int echo = 2;
  8. Adafruit_LiquidCrystal lcd_1(0);
  9.  
  10. void setup()
  11. {
  12.   lcd_1.begin(16, 2);
  13.   lcd_1.print("hello world");
  14.   pinMode(trigger,OUTPUT);
  15.   pinMode(echo,INPUT);
  16. }
  17.  
  18. void loop()
  19. {
  20.  
  21.   float distance    = 0.0;
  22.   float finalvalue  = 0.0;
  23.   digitalWrite(trigger,LOW);
  24.   delayMicroseconds(4);
  25.   digitalWrite(trigger,HIGH);
  26.   delayMicroseconds(10);
  27.   digitalWrite(trigger,LOW);
  28.  
  29.   distance = pulseIn(echo,HIGH);
  30.   finalvalue = (distance/28.57)/2;
  31.   lcd_1.setCursor(0, 1);
  32.   lcd_1.print(finalvalue);
  33.  
  34.  
  35.   seconds += 1;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement