#include <LiquidCrystal.h>
#include <math.h>
const int button = 0; // set button pin number 0
const int potentiometer = A0; //set potentiometer pin number to A0
int buttonvalue = 0;
int potentvalue = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.print("Switch & Analog");
delay(1000);
lcd.clear();
Serial.begin(9600); //sets up serial port & data rate to 9600bps
pinMode(button, INPUT); //sets up button as input
}
void loop() {
buttonvalue = digitalRead(button); //reads button state
potentvalue = analogRead(potentiometer); //reads potentiometer state
//print state values of button and potentiometer
lcd.setCursor(0,0);
lcd.print("Switch");
if (buttonvalue == HIGH) {
// Display "ON" on LCD
lcd.setCursor(7, 0);
lcd.print("ON");
delay(500); // delay for stability
} else {
// Display "OFF" on LCD
lcd.setCursor(7, 0);
lcd.print("OFF");
delay(500); // delay for stability
}
lcd.setCursor(0,1);
lcd.print("V=");
lcd.print(potentvalue);
}