#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
long duration,distCM,temp_distCM=0;
void setup()
{
lcd.begin();
lcd.setCursor(0,0);
lcd.print("Dist = cm");
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
}
void loop()
{
digitalWrite(triggerPin,LOW);
delayMicroseconds(2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
duration = pulseIn(echoPin,HIGH);
distCM = duration/58;
if (temp_distCM != distCM)
{
lcd.setCursor(7,0);
lcd.print(" ");
}
lcd.setCursor(7,0);
lcd.print(distCM);
temp_distCM = distCM;
delay(300);
}
Code 2
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
long duration,distCM;
void setup()
{
lcd.begin();
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
}
void loop()
{
digitalWrite(triggerPin,LOW);
delayMicroseconds(2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
duration = pulseIn(echoPin,HIGH);
distCM = duration/58;
int x = constrain(distCM,4,19);
lcd.clear();
lcd.home();
for (int i=4;i<=x;i++)
lcd.print("*");
delay(150);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น