รูปที่ 1
โค้ดที่12.1 (Code Arduino)
#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);
}
โค้ดที่12.2 (Code Arduino)
#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);
}
รูปที่ 2
โค้ดที่12.3 (Code Arduino)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
int buzzerpin = 11;
long duration,distCM;
void setup()
{
lcd.begin();
pinMode(buzzerpin,OUTPUT);
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
digitalWrite(buzzerpin,HIGH);
}
void loop()
{
digitalWrite(triggerPin,LOW);
delayMicroseconds(2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
duration = pulseIn(echoPin,HIGH);
distCM = duration/58;
lcd.home();
if (distCM > 400)
{
lcd.clear();
lcd.print("**!!!Alarm!!!**");
tone(buzzerpin,700,500);
delay(500);
digitalWrite(buzzerpin,HIGH);
delay(70);
lcd.setCursor(0,1);
lcd.print(distCM);
}
else
{
lcd.print("<-------------->");
lcd.setCursor(0,1);
lcd.print(distCM);
}
}
โค้ดที่12.4 (Code Arduino)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
int buzzerpin = 11;
long duration,distCM;
void setup()
{
lcd.begin();
pinMode(buzzerpin,OUTPUT);
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
digitalWrite(buzzerpin,HIGH);
}
void loop()
{
digitalWrite(triggerPin,LOW);
delayMicroseconds(2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
duration = pulseIn(echoPin,HIGH);
distCM = duration/58;
lcd.home();
if ((distCM>=4)&&(distCM<=30))
{
lcd.clear();
lcd.print("#####Alarm#####");
int dl = distCM*20;
tone(buzzerpin,600,dl);
delay(dl);
digitalWrite(buzzerpin,HIGH);
delay(70);
}
else
{
lcd.print(" Dist > 30 cm ");
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น