#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int upButton = 2;
const int downButton = 3;
const int wiperPin = 5;
const int upLed = 7;
const int downLed = 8;
int wiperValue = 0;
void setup() {
// put your setup code here, to run once:
//initialize lcd screen
lcd.init();
// turn on the backlight
lcd.backlight();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(wiperPin, OUTPUT);
pinMode(upLed, OUTPUT);
pinMode(downLed, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// tell the screen to write on the top row
lcd.setCursor(0, 0);
// tell the screen to write “hello, from” on the top row
lcd.print("Pot=");
if (wiperValue < 100) {
lcd.setCursor(7, 0);
lcd.print(" ");
}
if (wiperValue < 10) {
lcd.setCursor(6, 0);
lcd.print(" ");
}
lcd.setCursor(5, 0);
lcd.print(wiperValue);
digitalWrite(upLed, LOW);
digitalWrite(downLed, LOW);
if ((digitalRead(upButton)) == LOW) {
digitalWrite(upLed, HIGH);
if (wiperValue < 255) {
wiperValue = wiperValue + 5;
lcd.setCursor(15, 0);
lcd.print((char) 43);
analogWrite(wiperPin, wiperValue);
delay(100);
}
}
lcd.setCursor(15, 0);
lcd.print((char) 0);
if ((digitalRead(downButton)) == LOW) {
digitalWrite(downLed, HIGH);
if ((wiperValue >= 5) && (wiperValue != 0)) {
wiperValue = wiperValue - 5;
lcd.setCursor(15, 1);
lcd.print((char) 45);
analogWrite(wiperPin, wiperValue);
delay(100);
}
}
lcd.setCursor(15, 1);
lcd.print((char) 0);
}