#include <LiquidCrystal.h>
// กำหนด LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonBlue = 8; // ปุ่มสีน้ำเงิน
const int buttonYellow = 9; // ปุ่มสีเหลือง
const int analogPin = A0; // อ่านแรงดัน
void setup() {
lcd.begin(16, 2);
pinMode(buttonBlue, INPUT);
pinMode(buttonYellow, INPUT);
lcd.clear();
showPressButton();
}
void loop() {
if (digitalRead(buttonBlue) == HIGH) {
showTimeInSecond();
delay(500); // หน่วงเพื่อป้องกันการกดซ้ำ
showPressButton();
}
if (digitalRead(buttonYellow) == HIGH) {
showVoltMeasure();
delay(1000);
showPressButton();
}
}
void showPressButton() {
lcd.clear();
lcd.setCursor(2, 0); // ตรงกลางข้อความ (ตำแหน่งคร่าว ๆ)
lcd.print("Press Button");
}
void showTimeInSecond() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("time in second");
for (int i = 0; i <= 3; i++) {
lcd.setCursor(7, 1);
lcd.print(" "); // ลบค่าเก่า
lcd.setCursor(7, 1);
lcd.print(i);
delay(1000);
}
}
void showVoltMeasure() {
float voltage = analogRead(analogPin) * (5.0 / 1023.0);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Volt measure");
lcd.setCursor(5, 1);
lcd.print(voltage, 2); // ทศนิยม 2 ตำแหน่ง
lcd.print(" V");
}