0

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/

Code Belowenter image description here

#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 ("   ");
}
  • This is like having two players, play a single player game, by both of them pressing the buttons on the same single controller. Of course the game character is making weird moves. PS, I think a rotary encoder might be more appropriate to use here. – Gerben Apr 16 at 13:20
3

Your code doesn't make much sense.

You set the LED to 2 different brightness values that don't have anything to do with each other: The pot brightness and the button-based brightness. You only adjust the LED based on switches once every 5 mS. Thus the LED is going to flicker between 2 different brightness values.

What do you want it to do? How do you want to control the brightness based on input from both the pot and your switches? (What is the relationship between the buttons and the pot? Should the button input alter max/min value of the pot up and down? Should holding the button down change the brightness by 1 value per a specific time unit? Say 2 increments/decrements per second?)

Describe the user interface you want to achieve, and then think through how you would do that in code.

  • 1
    "Describe the user interface you want to achieve, and then think through how you would do that in code." +1. – VE7JRO Apr 15 at 23:19
  • Hi Duncan, the flickering only occurs upon addition of the pot, if I ran the code without the pot, everything works accordinging. This is my second code, I could present you with the first but I still get the same problem. Even if I removed the switchMillis section in the code. Even before I was told the arduino couldnt understand what to do because I didnt have buttonstates, but I added those to no success. Bottom line is, I need to control the brightness of an LED using both Pot and Buttons, alone each I can do, but I dont know how to do it all within the same code. – Boi G Apr 16 at 7:46
  • 1
    @BoiG can you describe to us exactly how the buttons and pot interact? If I decrease the pot, then use the increase button, what should happen? If I then increase the pot? What if I’m at max via the button, then crank the pot up? Then back down? It’s not clear what you’re really trying to achieve. – RubberDuck Apr 16 at 10:11
  • You're trying to build something and don't have a clear idea of what it is. You need to define, in clear precise detail, what you want to do, and then think through how to achieve that in code. We can't help you unless you can be clear about how you want your program to behave. – Duncan C Apr 16 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.