Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am using an Ardunio UNO and since the ardunio outputs 5 volts I am trying to c

ID: 663347 • Letter: I

Question

I am using an Ardunio UNO and since the ardunio outputs 5 volts I am trying to create a Code that increments and decrements the voltage using 2 buttons, ButtonA will add 0.5 volts each time it is pressed and ButtonB will subtract 0.5 volts each time it is pressed. my LED increments to 5 volts and decrements to 0 volts with both buttons the problem I have now is that button's A and B increment all the way from 0 - 5 and from 5 - 0 without taking steps. HOW do I make the LED Step once with each button press. Say I push button A once it should go from 0 - 0.5v etc. and when I push buttonB once it should go from 5 - 4.5. HERE is my code can you look at it and let me know how to make the button step not just step all at once thank you!!

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 = 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);
}
}
}

Explanation / Answer

Firstly, I suggest you to use the function debounce for checking that pin has been pressed correctly or not.And to keep the check the button A or B has been pressed correctly or not ,you can use the counter variable which we will be keep on increasing or decreasing depending upon the button pressed.If button A will pressed then counter counter will be incremented and if button B then it should be decremented.

Here i can mention the pseudo code for it => if(button A ==pressed) counter++;

elseif( buttonB == pressed ) counter--;

Bsically this ciunter variable will be used for regulating the output of the voltage. the function analogWrite() on the arduino uno is the duty cycle which takes in values from 0 to 255. So values for analogwrite function corresponding to duty cycle is analogWrite(0) = 0 Volt and analogWrite(255) = 5 Volt.You mentionned that it should require 10 presses to reach 5 Volt at the Arduino output, and the easiest way to accomplish this is to use Arduino's Map function.So you can use this Map function to get your desired result and the pseudo code for this Arduino Map function is:

Output = map(counter, 0, 10, 0, 255); analogWrite(Output);

The map function will limit the output voltage from 0 to 5 Volt so everything will be safe; however, the counter is not limited to 0 to 10. This means that even if the counter is at 15 (because you pressed button A too many times) the voltage will be 5 Volt and you would need to press button B at least 6 times to see the voltage go down! So to limit the counter you could easily implement your own function: if(counter >= 10) counter = 10; else if (counter<= 0) counter = 0.I think this explanation will help you solving this problem.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote