Skip to main content
1 of 3

Arduino UNO button LED brightness vary

I am using an Arduino UNO and I am trying to vary the brightness by steps by 0.5 volts till it gets to a max of 5.0 V using buttonA and decrease the brightness of the LED by 0.5 volts with buttonB I believe that I am close the only Problem I have is that when I push buttonA the LED steps it's voltage all the way to 5.0V and with buttonB its steps down to 0. I would like to have the LED step each time I press the button not all at once. Here is my code that I have so far can someone help me with telling the Arduino to Step each time a button is pressed.

HERE is my code:

int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
pinMode(ledPin, OUTPUT);  
pinMode(buttonApin, INPUT_PULLUP);  
pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
  if (digitalRead(buttonApin) == LOW)  
  {  
  if (counter >= 10)  
  {  
   counter = 10;  
  }  
 // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
 for (float n = 0; n <= maxBright; n +=25.5)   
 {  
  // sets the value (range from 0 to 255):  
  analogWrite(ledPin,n);  
  delay(ms);  
 }  
}  

 if(digitalRead(buttonBpin) == LOW)  
 {  
   if (counter <= 0)  
   {  
   counter = 0;  
   }  
   // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
   for (float n = maxBright; n >= 0; n -= 25.5)   
   {  
    // sets the value (range from 0 to 255):  
    analogWrite(ledPin, n);  
    delay(ms);  
   }  
 }  
}