#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16 , 2);
#define pot 15
#define sw 23
int state = LOW;
int swState;
int lest_swState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(115200);
pinMode(sw , INPUT);
lcd.init();
lcd.backlight(); //ให้ lcd สว่าง
lcd.clear();
}
void loop() {
int val = analogRead(pot);
int degree = map(val,0,4095,0,100);
lcd.setCursor(0,0);
lcd.print("Value PoT:");
lcd.setCursor(10,0);
lcd.print(degree);
//----------------------------------------------
int reading = digitalRead(sw);
if (reading != lest_swState){
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay){
if (reading != swState){
swState = reading;
if (swState == LOW) {
state =!state;
}
}
}
lest_swState = reading;
if (state = LOW){
lcd.setCursor(0,1);
lcd.print("off");
}
else {
lcd.setCursor(0,1);
lcd.print("on");
}
}