ใบงานทดลองครั้งที่ 10 LDR แสดงผลด้วย LCD ส่วนประกอบงานวิชา การเขียนโปรแกรมคอมฯ
ใบงานทดลองครั้งที่ 10
#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,16,2);
int aVal=0,tempaVal=0;
void setup()
{
     lcd.begin();
}
void loop() 
{
      aVal = analogRead(A0);
  if (tempaVal != aVal)
  {
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("LDR");
       lcd.setCursor(0,1);
       lcd.print(aVal);
       delay(300);
  }
  tempaVal = aVal;
}
   เมื่อวงจรทำงาน เราสามารถปรับค่า LDR หรือ ตัวต้านทานเปลี่ยนค่าตามแสง ให้ค่าตรงตามที่ต้องการ ซึ่งในวงจรนี้คือให้ ค่าLDR(tempaVal) ไม่เท่ากับ 0 จะทำให้หน้าจอ LCD ขึ้นคำว่า LDR ซึงสามารถตรวจสอบได้โดยโค้ดนี้#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int aVal=0,tempaVal=0;
void setup()
{
lcd.begin();
}
void loop()
{
aVal = analogRead(A0);
if (tempaVal != aVal)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(aVal);
lcd.setCursor(0,1);
if (aVal<100)
lcd.print("Dark");
else
lcd.print("Light");
delay(300);
}
tempaVal = aVal;
}
เมื่อตัวแปล temaVal ไม่เท่ากับ aVal จะทำการปริ้นLcdคำว่าaVal ถ้าตัวแปล aVal ต่ำกว่า100ปริ้นคำว่า Dark ถ้าไม่ตรงปริ้นคำว่า Light

 
 
ความคิดเห็น
แสดงความคิดเห็น