#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
long int x = 5 ;//259200 =
void setup() {
lcd.init();
lcd.backlight();
// 1=ดับ เเละ 0=ติด เพราะว่าต่อledขาบวกกับไฟ 5v
TCCR1A = 0;//เริ่มต้นใช้ timer1
TCCR1B = 0;//ให้ timer1 เป็นเเบบ normal mode (ดูจากตาราง 2-2)
OCR1A = 62499;//ให้ timer1 มีเป่าหมายที่ 1HZ
TIMSK1 = 0;//ปิดการใช้งาน interrupts ทั้งหมด
TCNT1 = 0x0;//Reset Counter
TIFR1 = 0x6;//เครีค่า interrupts ทั้งหมด
TCCR1B |= (1 << WGM12);//ให้ timer1 เป็นเเบบ CTC mode (ดูจากตาราง 2-2)
TCCR1B |= (1 << CS12);//กำหนด prescaler เป็น 256 (ดูจากตาราง 2-1)
lcd.setCursor(3,0);
lcd.print(x);
}
void loop() {
// put your main code here, to run repeatedly:
if(TIFR1 & (1 << OCF1A))//เช็คการ match interrupt ของ timer1
{
TIFR1 = (1 << OCF1A);//เครียค่า match interrupt ของ timer1
if (x > 0)
{
x -= 1 ;
lcd.setCursor(3,0);
lcd.print(x);
}
if (x == 9)
{
lcd.setCursor(4,0);
lcd.print(" ");
}
if (x == 0)
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(4,0);
lcd.print("It's time to");
lcd.setCursor(1,1);
lcd.print("charge battery now.");
}
}
}