homework3 : distance

I use a range sensor to calculate the distance like a bat through the theory of per centimeter need 29 milliseconds for sound .

 

 

 

 

 

 

const int pingPin = 7;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

Serial.begin(9600);

lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);

}

void loop()
{

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
int distance = pulseIn(pingPin, HIGH);
distance= distance/58;
delay(200);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Distance is”);
lcd.setCursor(0,1);
lcd.print(distance);
lcd.print(“cm”);
}

Leave a Reply

Your email address will not be published. Required fields are marked *