What is required is controlling the brightness of an LED using both Buttons and a Potentiometer, Button A: Increase
Button B: Decrease /Dim
Potentiometer: Increase and Decrease,
This however must happen on 1 single circuit and must display the results on an LCD.
If I do this separately, meaning buttons without potentiometer and visa versa, I win, the moment I combine the two. I get flickering when working the buttons, due to the potentiometer.
Alternative Research:https://www.instructables.com/id/Smooth-Potentiometer-Input/
#include <LiquidCrystal.h>
#include <Wire.h>
// ------------------------------------------------------------
int LED1 = 9;
int ButtonLeft = 8;
int ButtonRight = 7;
int brightness = 0;
// ------------------------------------------------------------
byte buttonState1 = 0;
byte lastButtonState1 = 0;
byte buttonState2 = 0;
byte lastButtonState2 = 0;
unsigned long switchMillis;
// ------------------------------------------------------------
int potValue1 = 0;
int potValue2 = 0;
int potPin= A0;
int readValue;
int writeValue;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// ------------------------------------------------------------
void setup() {
lcd.begin(16, 2);
pinMode (potPin, INPUT);
pinMode(LED1,OUTPUT);
pinMode(ButtonLeft,INPUT);
pinMode(ButtonRight, INPUT);
lcd.clear ();
}
// ------------------------------------------------------------
void loop(){
if (millis() - switchMillis >= 5)
{
switchMillis = millis();
checkSwitches();
}
readValue = analogRead(potPin);
writeValue = (255./1023.) * readValue;
analogWrite(LED1, writeValue);
potValue1 = analogRead(potPin) / 10;
potValue2 = potValue1 / 1.02;
lcd.setCursor(0,0);
lcd.print("Potent=");
lcd.print(potValue1);
lcd.print("%");
delay(100);
lcd.print(" ");
delay(1);
}
void checkSwitches() {
analogWrite(LED1, brightness);
delay(10);
buttonState1 = digitalRead(ButtonLeft);
if(buttonState1 == HIGH){
brightness--;
if(brightness < 0){
brightness = 0;
}
}
buttonState2 = digitalRead(ButtonRight);
if(buttonState2 == HIGH){
brightness++;
if(brightness > 255){
brightness = 255;
}
}
lcd.setCursor (0,1);
lcd.print ("Brightness=");
lcd.print (brightness);
lcd.print (" ");
}