Push Buttons To Control LED's Brightness

Hi!

I am doing a project were when I click button1, the LED get 10% brighter each time I click it. And when every time that I click button2, the LED should get 10% lower. My issue is when I click button1, the LED keeps on going up by 10 infinity, and doesn't stop at my stopping point of 255. For button2, it keeps on going down by 10 infinity, and doesn't go down my only 10. If anyone can help me that would be great!

Project Link (Code + Wiring): Push Button (LED Dimmer using Buzzer) - Wokwi Arduino and ESP32 Simulator

Code:

int BUTTON1 = 7;            // Button input at pin 7.
int BUTTON2 = 6;            // Button input at pin 6.
int RED = 3;   
int BLUE = 10;      
int BUTTONRead1;      // to store input value of the first button (0, 1) 
int BUTTONRead2;      // to store input value of the second button (0, 1) 
int j;
int h;

void setup(){
  Serial.begin(9600);     // initializing serail communication.
  pinMode(RED, OUTPUT);   // Defining LED pin as output.
  pinMode(BLUE, OUTPUT);  // Defining LED pin as output.
  pinMode(BUTTON1, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.
  pinMode(BUTTON2, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.
}

void loop(){
  BUTTONRead1 = digitalRead(BUTTON1);    // Reading Button1 Input.
  BUTTONRead2 = digitalRead(BUTTON2);    // Reading Button2 Input.

   if(BUTTONRead1 == 0 && j<=255) {

     if(j<=255){

 for (j<255; j=j+10;){

  Serial.println(j);
  analogWrite(RED, j);
  delay(10); 
   } 
    } 
      }
   

    if(BUTTONRead2 == 0) {

           if(j<=255){

   for (h>255; h=h-10;){
  


  Serial.println(h);
  analogWrite(RED, h);
  delay(10); 

  }
    }
      }
 
  delay(300);                      // delay to make output readable on serial monitor. 
}

Try this:

Replace:

brightness++;

By:

brightness += 3;

And:

brightness--;

By:

brightness -= 3;
    if(BUTTONRead1 == 0 && j<=255) {
        if(j<=255){

the test for j < 255 is is redundant

this condition may be true as long as the button is being pressed. need to check for a button press (change of state AND LOW)

            for (j<255; j=j+10;){

a for statement has 3 parts separated by semicolons (;). the 2nd is the condition for repeating, j=j+10, which is always true, making it an infinite loop

consider


int BUTTON1 = 7;
int BUTTON2 = 6;
int RED     = 3;

byte butLst1;
byte butLst2;
int  ledRed;

void loop ()
{
    byte but = digitalRead (BUTTON1);    // up
    if (butLst1 != but)  {
        butLst1 = but;
        delay (20);         // debounce

        if (LOW == but)  {
            ledRed += 10;
            if (255 < ledRed)
                ledRed = 255;
            Serial.println (ledRed);
            analogWrite (RED, ledRed);
        }
    }

    but = digitalRead (BUTTON2);    // down
    if (butLst2 != but)  {
        butLst2 = but;
        delay (20);         // debounce

        if (LOW == but)  {
            ledRed -= 10;
            if (0 > ledRed)
                ledRed = 0;
            Serial.println (ledRed);
            analogWrite (RED, ledRed);
        }
    }
}

void setup ()
{
    Serial.begin (9600);     // initializing serail communication.
    Serial.println ("rdy");

    pinMode (RED, OUTPUT);   // Defining LED pin as output.

    pinMode (BUTTON1, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.
    pinMode (BUTTON2, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.

    butLst1 = digitalRead (BUTTON1);
    butLst2 = digitalRead (BUTTON2);
}

Thank you so much! This code works perfectly, and helped me a lot with my project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.