Sensor Ultrasonik With LED & LCD
Sensor Ultrasonik With LED & LCD
Code :
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
int trigPin = 10;
int echoPin = 11;
int led1 =3;
int led2 =12;
int led3 =13;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= (duration/2) / 29.1;
distanceInch = (duration/2) / 74;
lcd.setCursor(0,0);
lcd.print("CORIS ");
lcd.print(distanceCm);
lcd.print(". ");
delay(10);
lcd.setCursor(0,1);
lcd.print(" 10 TKJ 1 ");
lcd.print(distanceInch);
lcd.print(" . ");
delay(10);
;
{
if (distanceCm >= 150)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3,LOW);
}
else if (distanceCm >= 100)
{
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3,LOW);
}
else
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3,HIGH);
}
}
}
Komentar
Posting Komentar