Way to combine functions with opposing For Statements? One is "<", one is ">"?

Okay, I've finally exhausted my search-for-answer capabilities. It's probably out there somewhere, I just don't know how to find it. And, please forgive me if my code is difficult to read; I'm posting from my ipad, so cut-n-paste from Arduino is not very simple.

I've written a sketch that I can easily manipulate to adjust the "fade up" and "fade down" patterns of multiple LEDs. I've gotten it whittled down to two main functions (one for "up", one for "down", and I can just insert which LEDs I want them to control when I call them from Loop.

I believe there must be a way to further simplify it so that I am calling only one function, and I can tell it whether to increase or decrease. I'm just not sure how.

Here's the code for the two functions I want to "combine" —

void FadeUp(int B, int L) {  // B = which "Brightness",  L = which "LED"

  // Loop until led "faded" up to full brightness: 

  for (int B = 1; B <= 255; B++) {

    // set the PWM brightness of led:

    analogWrite (L, B); 

    // change the brightness for next time through the loop:

    B = B + fadeAmount;

    // Verify potentiometer setting

    checkPot(); 

    // wait for "DelayTime" milliseconds to see the dimming effect  

      delay(DelayTime);
  }
}


void FadeDown(int B, int L) {  // B = which "Brightness",  L = which "LED"


  // Loop until ledRed faded down to zero brightness: 

  for (int B = 255; B >= 1; B--) {

    // set the PWM brightness of led:

    analogWrite(L, B); 

    // change the brightness for next time through the loop:

    B = B - fadeAmount;

    // Verify potentiometer reading

    checkPot();

    // wait for "DelayTime" milliseconds to see the dimming effect    

      delay(DelayTime);
  }     
}

The CheckPot function just takes a reading from potentiometer and uses it to adjust the DelayTime, which effectively speeds up or slows down the entire effect.

And, in case it helps, a small example of how I'm using FadeUp & FadeDown within void Loop() —

  FadeDown(brightnessBlue, ledBlue);    //ledBlue DOWN
    FadeUp(brightnessRed, ledRed);        //ledRed UP

  FadeDown(brightness2Red, led2Red);      //led2Red DOWN
    FadeUp(brightness2Blue, led2Blue);      //led2Blue UP

  FadeDown(brightnessRed, ledRed);      //ledRed DOWN
    FadeUp(brightnessRed, ledRed);        //ledRed UP

Let me know if you need more info.

Thanks in advance!

Ok, I'm an idiot....right? :blush:

I think a simple if/else would work here...if not as elegant as I'd like.

I could just have it fading in one direction (up?) unless I specifically tell it to fade the other direction (down).

I still think there has to be another way of doing it to make it more flexible and precise. But, this should work for now.

Still, if anyone has other ideas...please share! :slight_smile:

Fade by a direction. Fade up is fade by +1, fade down is fade by -1.

(untested)

#define UP 1
#define DOWN -1

void fade(unsigned char led, char direction)
{
  unsigned char brightness = direction == UP ? 0 : 255; // Select a starting value
  do {
    analogWrite(led, brightness);
    brightness += direction;
  } while (brightness > 0 && brightness < 255);
  analogWrite(led, brightness);  // and one last one to get the final brightness of 0 or 255
}

fade(10, UP);
fade(10, DOWN);

Or something like that anyway - you get the drift.

Another option is to fade between two values:

(again, untested)

void fadeBetween(unsigned char led, unsigned char a, unsigned char b)
{
  unsigned char brightness = a;
  do {
    analogWrite(led, brightness);
    brightness += ((a < b) ? 1 : -1);
  } while (brightness != b);
  analogWrite(led, brightness);
}

fadeBetween(10, 0, 255);
fadeBetween(10, 255, 128);
fadeBetween(10, 128, 192);
fadeBetween(10, 192, 0);