Dimmable LED Using 2 Push Buttons

Hello, I am new Arduino. I am trying to create a Dimmable LED Using 2 Push Buttons
The problem i am facing is that button 2 reading is always show 0 even if the button is click or not
(Left button = button 1 and right button = button 2)
This is my code

int i= 0;// i = led brightness
int green_ledpin = 9;
int button1_pin = 6;
int button2_pin = 8;
int Button1_Read ;
int Button2_Read ;
int dl = 100;
int x = 5;
int dl2 = 200;
void setup() {
  // put your setup code here, to run once:
pinMode(green_ledpin, OUTPUT);
pinMode(button1_pin, INPUT);
pinMode(button2_pin, INPUT);
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
Button1_Read = digitalRead(button1_pin);
Button2_Read = digitalRead(button2_pin);

Serial.print("Button1_Read = ");
Serial.print(Button1_Read);
Serial.print(", ");
Serial.print("Button2_Read = ");
Serial.println(Button2_Read);
//Serial.println("");
delay(dl);

if(Button1_Read == 0)
{
  i = i + x;
  analogWrite(green_ledpin, i);
  delay(dl2);
  if(i> 255)
  {
    i = 255;
  }
Serial.println(i);

}

if(Button2_Read == 0)
{
   i = i - x;
  analogWrite(green_ledpin, i);
  delay(dl2);
  if(i< 0)
  {
    i = 0;
  }
Serial.println(i);


}
}





Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum

Try connecting to 2 diagonally opposite pins on both buttons. I suspect that you have not connected across to the contacts on one of them

2 Likes

Move the yellow jumper wire from breadboard column 4 to column 2.

Thanks, Now it works

1 Like