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. The Problem I have is that when I push buttonA the LED steps it's voltage by 0.5V 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. CODE IS DIFFERENT from another post I did
HERE is my code:
int ledPin = 9; // must be a PWM digital pin
int buttonApin = A2; //buttonA to analog 2
int buttonBpin = A1; // buttonB to analog 1
int maxBright = 255; // between 0 and 255
int ms = 1500; // delay in milliseconds
int buttonPushCounterA = 0; //counter for the number of button presses
int buttonStateA = 0; // current state of the button
int lastButtonStateA = 0; // previous state of the button
int buttonPushCounterB = 0; //counter for the number of button presses
int buttonStateB = 0; // current state of the button
int lastButtonStateB = 0; // previous state of the button
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW) //if buttonA is pushed
{
buttonStateA = digitalRead(buttonApin);
if (buttonStateA != lastButtonStateA)
{
if(buttonStateA == LOW)
{
buttonPushCounterA++;
}
}
// 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 buttonB is pushed
{
buttonStateB = digitalRead(buttonBpin);
if (buttonStateB != lastButtonStateB)
{
if(buttonStateB == LOW)
{
buttonPushCounterB--;
}
}
// 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);
}
}
lastButtonStateA = buttonStateA;
lastButtonStateB = buttonStateB;
}
counter
is changed; with this code, it will always be0
!