#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define RedSW 14
#define YellowSW 13
#define ledPin 5
int freq = 5000; // ความถี่ในการสร้างสัญญาณ PWM
int ledChannel = 0; // ช่อง 0-15
int resolution = 2; // ความละเอียด 0-16 bit
int PWM_step = 1;
int i = 0;
int old01, now01 ;
int old02, now02 ;
String L[5] = {" 0%", " 33%", " 66%", "100%"};
void setup() {
ledcSetup(ledChannel, freq, resolution); // ตั้งค่า LED PWM ช่อง 0 ความถี่ 5000hz ความละเอียด 8bit หรือค่า 0-255
ledcAttachPin(ledPin, ledChannel);// กำหนดขา led ที่ต้องการควบคุม
pinMode(RedSW, INPUT_PULLUP);
pinMode(YellowSW, INPUT_PULLUP);
lcd.init();
Serial.begin(115200);
}
void loop() {
//Serial.println(i);
Serial.print("LED brightness = ");
Serial.println(L[i]);
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("LED brightness");
lcd.setCursor(7, 1);
lcd.print("=");
lcd.setCursor(9, 1);
lcd.print(L[i]);
ledcWrite(ledChannel, i );
/*i = i + PWM_step;
if (i <= 0 || i >= 3) {
PWM_step = -PWM_step;
}*/
old01 = now01;
now01 = digitalRead(RedSW);
if (old01 != now01 && now01 == 0 && i < 3) {
i++;
}
old02 = now02;
now02 = digitalRead(YellowSW);
if (old02 != now02 && now02 == 0 && i > 0) {
i--;
}
}