# Arduino Uno coding problem voltage varying [duplicate]

I was wondering whether anyone could help me... I am using an Arduino Uno and I am writing my code in the Arduino IDE. I am trying to implement the code so that the output voltage can be varied from 0 - 5 volts using 2 pushbuttons. I want to be able to increment the voltage by +0.5 volts from 0-5 volts every time I push Button A. Also I want to decrement the voltage by -0.5 volts from 5-0V every time I push Button B.

Example: (push Button A): output voltage 0.5: (push Button A): output voltage 1.0 etc.

So far when I upload my code the LED as my test for output just flickers between 2 - 4 volts. Can anyone tell me what I am doing wrong? I am not that good at coding so any help would be awesome.

Here is my code.

``````int PWMPin = 6; // output pin supporting PWM
int buttonApin = 8; // buttonA to pin 9 PWM
int buttonBpin = 10; // buttonB to pin 10 PWM
float value = 0; // read the value at 0
float counter = 0;

void setup() {
pinMode(buttonApin, INPUT);
pinMode(buttonBpin, INPUT);
pinMode(PWMPin, OUTPUT);
}
void loop() {
port = map(port, 0, 10, 0, 255);
analogWrite(6, port);
if (counter >= 10) {
counter = 10;
}
// fade from min to max in increments of 25.5 points: basically (0.5 volts)
// sets the value (range from 0 to 255):
delay(100);
}
}
if (counter <= 0) {
counter = 0;
}
// fade from max to min in increments of 25.5 points: basically (0.5 volts)
// sets the value (range from 0 to 255):
delay(100);
}
}
}
``````
• Why would you use a LED to test your output instead of, say, a DMM? – Ignacio Vazquez-Abrams Jun 10 '15 at 16:50
• Because it would be the same thing when I connect it to a different source I will still be able to vary voltage IF i can get this running. – A-UNO help Jun 10 '15 at 17:06
• Varying the voltage on a LED does almost nothing. – Ignacio Vazquez-Abrams Jun 10 '15 at 17:07
• okay so what would use for an output? – A-UNO help Jun 10 '15 at 17:15
• Something that gives you an actual number. – Ignacio Vazquez-Abrams Jun 10 '15 at 17:16

There's a number of very basic things wrong with your code - things that aren't doing what you think, or you just telling it completely the wrong things.

Start by formatting your code properly (INDENTS!) and match up all your brackets. You'll soon see what I see as far as what is wrong.

I'll give you some clues:

``````{
``````

``````for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=25.5)
``````

C is not Python!

``````digitalWrite(PWMPin, fadeValue);
``````

Writing an analog value to a digital pin?!

``````Serial.println(PWMPin);
``````

Printing the pin number - now what is the point of that?

``````... fadeValue += 25.5 ...
``````

Adding a float to an integer - bad practice (and ultimately pointless) - pick one, float or integer, and stick to it.

And then there is the whole logic of your program. I can't work out what it's supposed to be achieving. It looks like you've tried to get it to do all sorts of parts of different things and not got it to do anything at all. Strip it right out and get it to do ONE thing - provide a specific, STATIC, voltage. Then add in some way of changing that voltage afterwards.

• I dont get it? what brackets should I take out – A-UNO help Jun 10 '15 at 17:22
• You have brackets where you shouldn't have, and you're lacking brackets where you should have them. – Majenko Jun 10 '15 at 17:23
• And that's just a start... – Majenko Jun 10 '15 at 17:37
• That's no help at all. Please edit the code in your original question so I can see it in context – Majenko Jun 10 '15 at 17:39
• You forgot to add. It's useless to scale an analog output by a loop without delays! – ps95 Jun 12 '15 at 19:03