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 fadeValue = value;
float counter = 0;
void setup() {
pinMode(buttonApin, INPUT);
pinMode(buttonBpin, INPUT);
pinMode(PWMPin, OUTPUT);
}
void loop() {
int port = analogRead(0);
port = map(port, 0, 10, 0, 255);
analogWrite(6, port);
if (digitalRead(buttonApin) == LOW, fadeValue) {
if (counter >= 10) {
counter = 10;
}
// fade from min to max in increments of 25.5 points: basically (0.5 volts)
for(fadeValue = 0 ; fadeValue <= 255; fadeValue +=25.5) {
counter = fadeValue;
// sets the value (range from 0 to 255):
analogWrite(PWMPin, fadeValue);
delay(100);
}
}
if (digitalRead(buttonBpin) == LOW, fadeValue) {
if (counter <= 0) {
counter = 0;
}
// fade from max to min in increments of 25.5 points: basically (0.5 volts)
for(fadeValue = 255 ; fadeValue >= 0; fadeValue -=25.5) {
counter = fadeValue;
// sets the value (range from 0 to 255):
analogWrite(PWMPin, fadeValue);
delay(100);
}
}
}