Lost in code: voltage controlled LEDs

Hi,
I am new here and right off the batt I have to say I have no knowledge about programming.
I am trying to put up the code for a simple behaviour:

Arduino listens on one of it’s pins for voltage. If the voltage is higher then… the corresponding LED lights up. When the voltage is lower, again, the other LED lights up. That is simple. But I am trying to bring up the light gradually. And here the problem starts.

No matter what I try, Arduino senses the voltage and regarding of the threshold it executes fading up the corresponding diode. But then it kind of gets stuck. When I change the voltage, nothing happens. I came that far that it will react when I change the voltage from higher to lower and higher voltage LED stops lighting, the lower voltage LED fades up but it does not work the other way (from lower voltage back to higher). Even though the code for both LEDs is identical (except slightly different variable names). What’s going on?

#define ledHigh 3
#define ledLow 5
int fadeValueUp1 = 0;
int fadeValueUp2 = 0;


void setup()
{
  Serial.begin(9600);

  pinMode(ledHigh, OUTPUT);
  digitalWrite(ledHigh, LOW);

  pinMode(ledLow, OUTPUT);
  digitalWrite(ledLow, LOW);

}

void loop() {

  batLow();
  batCharging();



}
void batLow() {
  int sensorValue = analogRead(A0); //read the A0 pin value
  float voltage1 = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.
  if (voltage1 < 2.00 && fadeValueUp1 != 254) //set the voltage considered low battery here
  {
  
if (fadeValueUp1 > 250)
    int fadeValueUp1 = 0;
    while (fadeValueUp1 < 255) {

      fadeValueUp1++;
      Serial.println(fadeValueUp1);
      analogWrite(ledLow, fadeValueUp1);
      delay (10);
      if (fadeValueUp1 == 254) {
        break;
      }
    }


  } 
} 



void batCharging() {
  int sensorValue = analogRead(A0); //read the A0 pin value
  float voltage2 = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.
  if (voltage2 > 2.00 && fadeValueUp2 != 254) //set the voltage considered low battery here
   {
   
if (fadeValueUp2 > 250)
    int fadeValueUp2 = 0;
    while (fadeValueUp2 < 255) {

      fadeValueUp2++;
      Serial.println(fadeValueUp2);
      analogWrite(ledHigh, fadeValueUp2);
      delay (10);
      if (fadeValueUp2 == 254) {
        break;
      }
    }


  } 
}

Thank you for ideas, community.

Your code is convoluted and has no provisions for turning the LEDs off. Your indentation is off, meaning it's hard to see what code runs at what time.
Here's what I can surmise though:

BatLow does this:
1: read voltage
2: if voltage is low and LED not already lit (254):
3: if LED is above 250, set it to 0 (why? It should be 0 already or statement 2 wouldn't let you enter this block at all)
4: Loop LED from 0 to 254
5: but break when it reaches 254 (why not have the While instruction end the loop with the correct value instead?)

Conclusions:
There are no instructions for turning the LEDs off.
2-5 would work better with a for loop. Then you can set the initial and final number of your variable in the loop setup, and don't need 3 nor 5.
There is no hysteresis, so if there's noise on the input and it's right at the threshold, you may find your lights fading randomly.

Fix:
The loop: use a for loop with conditions that keep the fade values within bounds without break instructions.
The LEDs: give the program a way to turn them off if they're on but their paramenters for being on are no longer met. You decide if you want them to fade out or just turn off.
Hysteresis: make it so that when a LED is on, it takes a slightly different voltage to turn it off again.

Hello ardy23,
Welcome to the forum.

++Karma for posting your code correctly on your first post :slight_smile:

if (fadeValueUp1 > 250)
    int fadeValueUp1 = 0;
    while (fadeValueUp1 < 255) {
...
if (fadeValueUp2 > 250)
    int fadeValueUp2 = 0;
    while (fadeValueUp2 < 255) {

You check if fadeValueUp1 is > 250. If it is, then you create a NEW variable, also called fadeValueUp1, and set it to 0. Then you check if this NEW fadeValueUp1 is < 255, which, of course it is, since you set it to 0 when you created it.

You do exactly the same for fadeValueUp2…

Regards,
Ray L.

Thank you everyone for your ideas. I have made a few alterations to the code as suggested. I have used For loop (as suggested by GalFisk). It almost works now. But just almost. With the code as it is now, I have to keep the Break statements otherwise the program does not react to the voltage change on pin A0. I suppose it gets stuck in a perpetual loop. I don’t know a better workaround how to get out of the loop (I have to add that I have tried the same approach using While, Do while always with the same results - break was needed).

Now what goes well: LEDS are fading up / turn off when voltage changes at the threshold.

What does not go well: at certain voltage values both LEDs do not fade up once and keep turned on but instead keep fading up forever in a loop. if I change voltage a bit, this strange behaviour stops. But also it seems this behaviour occurs only at certain specefic voltage values.

Example:

void BatLow executes from voltage readings 0 - 199
somewhere in a range analogRead (aka voltage) 80 - 160 the low voltage LED fades up and keeps looping forever until the voltage drops a bit below. The exact same behaviour happens at higher voltage above the threshold, although in a much smaller area (320 - 330). For the voltage lows or highs I am using a DC DC step down converter. Could this be the problem and not the code? I have tried to investigate with the multimeter but could not find any anomaly.

And here is the code:

#define ledHigh 3
#define ledLow 5
int fadeValueUp1 = 0;
int fadeValueUp2 = 0;


void setup()
{
  Serial.begin(9600);

  pinMode(ledHigh, OUTPUT);
  digitalWrite(ledHigh, LOW);

  pinMode(ledLow, OUTPUT);
  digitalWrite(ledLow, LOW);

}

void loop() {

  batLow();
  batCharging();



}
void batLow() {
  int sensorValue1 = analogRead(A0); //read the A0 pin value
 int x = 1;
  if (sensorValue1 < 200) //set the voltage considered low battery here
  {
  digitalWrite(ledHigh, LOW);
  digitalWrite(ledLow, LOW);
for (int fadeValueUp1 = 0 ; fadeValueUp1 <= 255; fadeValueUp1 += 1 + x)
    {
      Serial.println(sensorValue1);
      analogWrite(ledLow, fadeValueUp1);
      if (fadeValueUp1 >= 254) {
        x = -1; 
      }
      delay (10);

      int sensorValue1 = analogRead(A0); //read the A0 pin value
      delay (10);
       if (fadeValueUp1 == 254 && sensorValue1 > 200) {
       break;
      }
    }


  } 
} 



void batCharging() {
  int sensorValue2 = analogRead(A0); //read the A0 pin value
  int x=1;
  if (sensorValue2 > 305) //set the voltage considered low battery here
   {
   digitalWrite(ledLow, LOW);
   digitalWrite(ledHigh, LOW);
for (int fadeValueUp2 = 0 ; fadeValueUp2 <= 255; fadeValueUp2 += 1 + x)
    {
      Serial.println(sensorValue2);
      analogWrite(ledHigh, fadeValueUp2);
      if (fadeValueUp2 >= 254) {
        x = -1; 
      }
      delay (10);

      int sensorValue2 = analogRead(A0); //read the A0 pin value
      delay (10);
      if (fadeValueUp2 == 254 && sensorValue2 < 295) {
      break;
      }
    }


  } 
}

Thank you!

It looks like the DC DC step down converter is to blame. So it's solved. Thank you again.