Last active
August 11, 2016 13:18
-
-
Save izumogeiger/67dc31c313433b8c3821156e96a1c528 to your computer and use it in GitHub Desktop.
hcsr04_lcd.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(3, 4, 5, 6, 7, 8); | |
int Trig = 9; | |
int Echo = 10; | |
int Duration; | |
float Distance; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(Trig,OUTPUT); | |
pinMode(Echo,INPUT); | |
lcd.begin(16, 2); | |
} | |
void loop() { | |
digitalWrite(Trig,LOW); | |
delayMicroseconds(1); | |
digitalWrite(Trig,HIGH); | |
delayMicroseconds(11); | |
digitalWrite(Trig,LOW); | |
Duration = pulseIn(Echo,HIGH); | |
if (Duration>0) { | |
Distance = Duration/2; | |
Distance = Distance*340*100/1000000; // ultrasonic speed is 340m/s = 34000cm/s = 0.034cm/us | |
Serial.print(Duration); | |
Serial.print(" us "); | |
Serial.print(Distance); | |
Serial.println(" cm"); | |
lcd.setCursor(0,0); | |
lcd.print(Distance); | |
lcd.setCursor(6,0); | |
lcd.print("cm"); | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment