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.

(((i think i posted in the wrong forum originally)))…

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
}

I have tweaked the code a bit to do what I want it to do (which helps me understand its purpose).

I need some help understanding part of the code.

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

Please explain what (you think) the 2nd, 4th, 6th, and 8th lines are doing.

right now they are not doing anything. but if int hold = 0 becomes something like 1 or 10 it will hold the color for a period of time.

right now they are not doing anything. but if int hold = 0 becomes something like 1 or 10 it will hold the color for a period of time.

So, substituting the value for the variable, supposing that hold is 10:

crossFade(greenBlue);
10;
crossFade(purplePink);
10;
crossFade(red);
10;
crossFade(tangerine);
10;

I really don’t think that is going to cause any kind of delay().

so hold does not need to be written? what about delay before 10? i just need to write 10 for it to do a delay?

do you know how i could write some code to have a pot control the speed in wich the colors fade together. i want this to be interactive code thats not set in stone once uploaded. eventually im going to want to write code for a pot to control the hold speed and maybe even the colors that it fades through. rough ex. red, orange, blue, and green at 5 volts. red, orange, blue at 4v. red, orange at 3. and red only at 2 and below. but really the main thing i want to accomplish is the fade speed.

i have been studying this code most of the day and see why you dont understand hold; after each crossfade. it is written at the bottom to hold with out me telling it to do it again. it seems that if im to do what i want i need to assign the pots to the delay(fade); and delay(hold);

can someone point me in a direction to figure this out? i could use the help.

Read this before posting a programming question

Code tags.

look i apologize about my newbness..but can we talk about the topic? im working on getting in the groove of things around here as im new to programing and online forums at that... the reason i started the new thread was because i thought it made sense. guess it didnt.. glad thats straightened out.

what dont you like about the code tags?

let me re state whats going on here… I would like to use a pot to change the speed at which LEDs fade together. i dont want it to be pre determined in the code. i dont want it to only have one speed. but to have multiple speeds that are controlled by turning a pot. this is the code i have now

int redPin = 11;
int grnPin = 5;
int bluPin = 6;
int potPin = A0;

int sensorValue = 0;
int outputValue = 0;

int black[3] = {255, 255, 255 };
int red[3] = { 0, 255, 255 }; 
int greenBlue[3] = { 255, 0, 0 };
int purplePink[3] = { 0, 255, 0 };
int tangerine[3] = { 0, 226, 255 };
int lemonLime[3] = { 0, 0, 255 };
int guesswork[3] = { 200,50,250};

int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int fade = 4;
int hold = 1; 
int DEBUG = 1; 
int loopCount = 60; 
int repeat = 0;
int j = 0;

int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

void setup()
{

  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  
  if (DEBUG){
    Serial.begin(9600); 
  }
}
    
    void loop()
 {

      
      crossFade(greenBlue);
      crossFade(purplePink);
      crossFade(tangerine);
      crossFade(lemonLime);
      crossFade(red);
      crossFade(guesswork);
    
 
      
    }
    
    int calculateStep(int prevValue, int endValue)
    {
      int step = endValue - prevValue;
      if (step) 
      {
        step = 1020/step;
      }
      return step; 
    }
    
    int calculateVal(int step, int val, int i) 
{
if ((step) && i % step == 0)
{
  if (step > 0)
  { 
    val += 1;
  }
  else if (step < 0) 
  {
    val -= 1;
  }
}

if (val > 255)
{
  val = 255;
}
if (val < 0)
{
  val = 0;
}
return val;
}

void crossFade(int color[3])
{ 
  int R = color[0];
  int G = color[1];
  int B = color[2];
  
  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);
 analogWrite(grnPin, grnVal);
 analogWrite(bluPin, bluVal);


// delay(fade);  is what works and what i put 
// before the } is what im working with to control the fade

sensorValue = analogRead(potPin);
 outputValue = map(sensorValue, 0, 1023, 0, 500);
delay = analogWrite( redVal, grnVal, bluVal);
  }

 

 
 
{
  
 
   if (DEBUG)
 {
   if (i == 0 or i % loopCount == 0)
   {
     Serial.print("loop/RGB: #");
     Serial.print(i);
    Serial.print(" | ");
   Serial.print(redVal);
  Serial.print(" / ");
 Serial.print(grnVal);
Serial.print(" / ");
Serial.print(bluVal);
   }
   DEBUG += 1; 
 }
  }
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  delay(hold);
}

what dont you like about the code tags?

I wanted the original post in this thread (the one with the code in it) to be wrapped in code tags. It makes it easier to read.

Here:

    sensorValue = analogRead(potPin);
    outputValue = map(sensorValue, 0, 1023, 0, 500);
    delay = analogWrite( redVal, grnVal, bluVal);

delay is a library function. You can't assign to it like that. Choose another name.

May I suggest you read this thread?

http://www.gammon.com.au/blink

You can incorporate delays without using "delay". You do it with time intervals.

Thank you for giving me some advice as well as something to read. The unsigned long function is interesting and im glad to now know about it. i cant seem to get it to work for what im trying to do though. also the code you gave me does not work but you said it wouldn't because of delays limitations. its good to know that the outputValue is what needs to have the map. at school today I thought this might be my direction for the map..

int fade = potFade;
int hold = potHold;
pot Control[3] = sensorValue;
{
potControl[0] = potFade;
potControl[1] = potHold;
potControl[2] = potColor;
}
map(potControl, 0, 1023, 0, 100)

i was also thinking this might be some direction to getting it all to work with multiple pots doing multiple things.

maybe i should just get buttons to do this for me until i understand this more? or is this simple?

Maybe you need to re-read the map documentation. It returns a value that you discard. If you aren't interested in the output, then don't bother calling the function. If you are, don't discard the output.