Controlling LED color fade with potentiometer

I have a code that takes multiple RBG values and fades them together at a given speed. My current goal with this project is to use a potentiometer to change the speed at which they fade together. (I have more uses to give potentiometers for the future of this project).

I am very new to writing code. the code I am including has been written by another as a online example. I have tweaked the code a bit to do what I want it to do (which helps me understand its purpose). If someone could explain how the fading works i would really appreciate that but its not the purpose of the post.

int redPin = 11; // Red LED, connected to digital pin 9
int grnPin = 5; // Green LED, connected to digital pin 10
int bluPin = 6; // Blue LED, connected to digital pin 11
int sensorPin = A0;
int sensorValue = 0;
int rbg;

// Color arrays
int black[3] = { 100, 100, 100 };
int white[3] = { 0, 0, 0 };
int red[3] = { 0, 100, 100 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
int greenBlue[3] = { 100, 0, 0 };
int purplePink[3] = { 0, 100, 0 };
int tangerine[3] = { 0, 88, 100 };
int lemonLime[3] = { 0, 0, 100 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int fade = 1; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat

// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

// Set up the LED outputs
void setup()
{
pinMode(sensorPin, INPUT);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);

if (DEBUG) { // If we want to see values for debugging…
Serial.begin(9600); // …set up the serial ouput
}
}

// Main program: list the order of crossfades
void loop()
{
rbg = (100 - map( analogRead(sensorPin), 0, 1023, 0, 100));

analogWrite(redPin, fade);
analogWrite(grnPin, fade);
analogWrite(bluPin, fade);
delay(15);

crossFade(greenBlue);
hold;
crossFade(purplePink);
hold;
crossFade(red);
hold;
crossFade(tangerine);
hold;
crossFade(lemonLime);

}

/* BELOW THIS LINE IS THE MATH – YOU SHOULDN’T NEED TO CHANGE THIS FOR THE BASICS
*

  • The program works like this:
  • Imagine a crossfade that moves the red LED from 0-10,
  • the green from 0-5, and the blue from 10 to 7, in
  • ten steps.
  • We’d want to count the 10 steps and increase or
  • decrease color values in evenly stepped increments.
  • Imagine a + indicates raising a value by 1, and a -
  • equals lowering it. Our 10 step fade would look like:
  • 1 2 3 4 5 6 7 8 9 10
  • R + + + + + + + + + +
  • G + + + + +
  • B - - -
  • The red rises from 0 to 10 in ten steps, the green from
  • 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
  • In the real program, the color percentages are converted to
  • 0-255 values, and there are 1020 steps (255*4).
  • To figure out how big a step there should be between one up- or
  • down-tick of one of the LED values, we call calculateStep(),
  • which calculates the absolute gap between the start and end values,
  • and then divides that gap by 1020 to determine the size of the step
  • between adjustments in the value.
    */

int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What’s the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}

/* The next function is calculateVal. When the loop value, i,

  • reaches the step size appropriate for one of the
  • colors, it increases or decreases the value of that color by 1.
  • (R, G, and B are each calculated separately.)
    */

int calculateVal(int step, int val, int i) {

if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive…
val += 1;
}
else if (step < 0) { // …or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}

/* crossFade() converts the percentage colors to a

  • 0-255 range, then loops 1020 times, checking to see if
  • the value needs to be updated each time, then writing
  • the color values to the correct pins.
    */

void crossFade(int color[3]) {
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;

int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);

for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);

analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);

delay(fade); // Pause for ‘wait’ milliseconds before resuming the loop

if (DEBUG) { // If we want serial output, print it at the
if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
Serial.print(“Loop/RGB: #”);
Serial.print(i);
Serial.print(" | “);
Serial.print(redVal);
Serial.print(” / “);
Serial.print(grnVal);
Serial.print(” / ");
Serial.println(bluVal);
}
DEBUG += 1;
}
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional ‘wait’ milliseconds before resuming the loop
}

rexhex: If someone could explain how the fading works i would really appreciate that but its not the purpose of the post.

What is it you were wanting help with, if that's not it?

Geoff

@OP: You're up to 20+ posts - do you think you could start using code tags please?

I haven't unpicked the logic in that code to see what it's trying to do. I can see you have two chunks of code that update the LED values, and I'm skeptical that they are both doing the right thing (to my mind, it should not be necessary to do these transitions in two places).

You are deriving a value 'rbg' from the analog input and then not doing anything with it. Presumably this should be the value that you use to control the speed of the transitions.

You have a variable 'fade' which controls how fast the transition occurs, but you also use this value to set the LED brightness in one of the chun ks of code doing this. This leaves me confused about what 'fade' is meant to be. I suspect that if you replaced the delay(fade) with delay(rbg) you would get behaviour along the lines you're asking for, although you'd need to sort out the scaling of rbg so that the range of potentiometer positions you want to use produced the right range of speeds. If you do this then I'd recommend giving these variables names that reflected their actual purpose since the current names clearly don't.