আল্ট্রাসনিক সেন্সর' কোড ।


আল্ট্রাসনিক সেন্সর' কোড ।



#include <LiquidCrystal.h> // includes the LiquidCrystal Library
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int trigPin = 6;   // defines pins numbers
const int echoPin = 7;
long duration;
int distanceCm, distanceInch;  // defines variables
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);  // Sets the trigPin as an Output
pinMode(echoPin, INPUT);// Sets the echoPin as an Input
}
void loop() {
digitalWrite(trigPin, LOW); 
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(500);

}

No comments

Theme images by 5ugarless. Powered by Blogger.