I am creating a function that will ease all the R, G, and B values of an array of RGB Leds simultaneously. The functions that I have now will ease one base color at a time, ie I can ease R to 100, then to 0, then green to 100, but I cant ease green and red to 100 at the same time. Those work fine :D. The function that can change all of them simultaneously, however, is not working. I am getting alot of seemingly random output on the LEDS in the array, I cannot fathom it
Here is the code for the simultanios code and the code that changes one at a time, in that order:
void easeAllRGB(int rTarget, int gTarget, int bTarget, int time) //Simultanious
{
//Declare the vars to use
int prevValuesOfAllLeds[__numLedsInArray][3]; //All are 2D to hold the LED number and the pin state
int diffBetweenCurrTarget[__numLedsInArray][3];
float ledStep[__numLedsInArray][3];
float ledBuffer[__numLedsInArray][3];
int numOfLoops = 100;
//Set all of the previous states into temporary arrays for later use (differance between target and current, step size, etc)
for(int i=0; i<__numLedsInArray; i++)
{
for(int b=0; b<3; b++)
{
prevValuesOfAllLeds[i][b]=ledStates[i][b];
ledBuffer[i][b]=ledStates[i][b];
}
}
//Get differance between the current states and the target for R, G, and B
for(int i=0; i<__numLedsInArray; i++)
{
diffBetweenCurrTarget[i][__RED]= rTarget - ledStates[i][__RED];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__GREEN]= gTarget - ledStates[i][__GREEN];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__BLUE]= bTarget - ledStates[i][__BLUE];
}
//put the step size for each LED into another array. It is floating point, for thoes fun little decimals!
for(i=0; i<__numLedsInArray; i++)
{
for(b=0; b<3; b++)
{
ledStep[i][b]= (float)diffBetweenCurrTarget[i][b]/(float)numOfLoops; //EG: Target = 100; Current = 50. Differance = Tar-Curr; Diff = 50; StepSize = Diff/Loops; Loops=100; Diff/Loops = .5; ie .5 added to Current 100 Times increses it by 50. 50+50 = 100, the target :)
}
}
//Add the step to the specific LED pin and delay whatever time is needed. Notice the use of the buffer to take care of decimals, by typecasting as an (int).
for(int b=0; b<numOfLoops; b++)
{
for(i=0; i<__numLedsInArray; i++)
{
for(int c=0; c<3; c++)
{
ledBuffer[i][c]=ledBuffer[i][c] + ledStep[i][c];
ledStates[i][c]=(int)ledBuffer[i][c];
}
}
delay(time);
}
//Set to the target just to take care of any bad decimals. Also cleans up the numbers for the ISR to take care of.
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__RED]=rTarget;
}
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__GREEN]=gTarget;
}
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__BLUE]=bTarget;
}
return; //Done!
}
void easeAllSingleColor(int target, int time, int color) //One at a time
{
//Declare the vars to use
int prevValuesOfLED[__numLedsInArray];
int diffBetweenCurrTarget[__numLedsInArray];
float ledStep[__numLedsInArray];
float ledBuffer[__numLedsInArray];
int numOfLoops = 100;
//Set all of the previous states
for(int i=0; i<__numLedsInArray; i++)
{
prevValuesOfLED[i]=ledStates[i][color];
ledBuffer[i]=ledStates[i][color];
}
//Put previous values & the buffer values into an array
for(int i=0; i<__numLedsInArray; i++)
{
prevValuesOfLED[i]=ledStates[i][color];
ledBuffer[i]=ledStates[i][color];
}
/*
Serial.print("prevValuesOfLED[0]: ");
Serial.println(prevValuesOfLED[0]);
Serial.print("ledBuffer[0]: ");
Serial.println(ledBuffer[0]);
*/
//Put the differance between the previous values and the target value into arrays
for(i=0; i<__numLedsInArray; i++)
{
diffBetweenCurrTarget[i]= target - ledStates[i][color];
}
/*
Serial.print("diffBetweenCurrTarget[0]: ");
Serial.println(diffBetweenCurrTarget[0]);
*/
//Put the step size into an array (will put into the float, they're hardly ever nice even numbers)
for(i=0; i<__numLedsInArray; i++)
{
ledStep[i]= (float)diffBetweenCurrTarget[i]/(float)numOfLoops;
}
/*
Serial.print("ledStep[0]: ");
Serial.println(ledStep[0]);
*/
//Add the step amount into the FLOAT buffer, then typecast it and set the current LED state as that.
for(int b=0; b<numOfLoops; b++)
{
for(i=0; i<__numLedsInArray; i++)
{
ledBuffer[i]=ledBuffer[i] + ledStep[i];
ledStates[i][color]=(int)ledBuffer[i];
}
delay(time);
}
//Set the LEDS to the target value just to brush out any loose floats that didnt make it for some reason or another
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][color]=target;
}
return;
}
Thats my code, thanks for your help! If I figure it out, I'll post
If your having trouble with the code to change all the LEDs at the same time that is the one we really need to see rather than one that does not do what you want.
By the way 'Easing' is not the right word for what you want. The correct word is fading.
Sorry! I should have been more specific. The code includes the function that is not working. It is the very first one. Ill seperate them:
Code that does not work (fades all 3 colors at the same time):
void easeAllRGB(int rTarget, int gTarget, int bTarget, int time)
{
//Declare the vars to use
//Declare prev vars for all the colors
int prevValuesOfAllLeds[__numLedsInArray][3];
int diffBetweenCurrTarget[__numLedsInArray][3];
float ledStep[__numLedsInArray][3];
float ledBuffer[__numLedsInArray][3];
int numOfLoops = 100;
//Set all of the previous states into temporary arrays for later use (differance between target and current, step size, etc)
for(int i=0; i<__numLedsInArray; i++)
{
for(int b=0; b<3; b++)
{
prevValuesOfAllLeds[i][b]=ledStates[i][b];
ledBuffer[i][b]=ledStates[i][b];
}
}
//Get differance between the current states and the target for R, G, and B
for(int i=0; i<__numLedsInArray; i++)
{
diffBetweenCurrTarget[i][__RED]= rTarget - ledStates[i][__RED];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__GREEN]= gTarget - ledStates[i][__GREEN];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__BLUE]= bTarget - ledStates[i][__BLUE];
}
//put the step size for each LED into another array. It is floating point, for thoes fun little decimals!
for(i=0; i<__numLedsInArray; i++)
{
for(b=0; b<3; b++)
{
ledStep[i][b]= (float)diffBetweenCurrTarget[i][b]/(float)numOfLoops; //EG: Target = 100; Current = 50. Differance = Tar-Curr; Diff = 50; StepSize = Diff/Loops; Loops=100; Diff/Loops = .5; ie .5 added to Current 100 Times increses it by 50. 50+50 = 100, the target :)
}
}
//Add the step to the specific LED pin and delay whatever time is needed. Notice the use of the buffer to take care of decimals, by typecasting as an (int).
for(int b=0; b<numOfLoops; b++)
{
for(i=0; i<__numLedsInArray; i++)
{
for(int c=0; c<3; c++)
{
ledBuffer[i][c]=ledBuffer[i][c] + ledStep[i][c];
ledStates[i][c]=(int)ledBuffer[i][c];
}
}
delay(time);
}
//Set to the target just to take care of any bad decimals. Also cleans up the numbers for the ISR to take care of.
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__RED]=rTarget;
}
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__GREEN]=gTarget;
}
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][__BLUE]=bTarget;
}
return; //Done!
}
And the code that does work (fades one at a time):
void easeAllSingleColor(int target, int time, int color)
{
//Declare the vars to use
int prevValuesOfLED[__numLedsInArray];
int diffBetweenCurrTarget[__numLedsInArray];
float ledStep[__numLedsInArray];
float ledBuffer[__numLedsInArray];
int numOfLoops = 100;
//Set all of the previous states
for(int i=0; i<__numLedsInArray; i++)
{
prevValuesOfLED[i]=ledStates[i][color];
ledBuffer[i]=ledStates[i][color];
}
//Put previous values & the buffer values into an array
for(int i=0; i<__numLedsInArray; i++)
{
prevValuesOfLED[i]=ledStates[i][color];
ledBuffer[i]=ledStates[i][color];
}
/*
Serial.print("prevValuesOfLED[0]: ");
Serial.println(prevValuesOfLED[0]);
Serial.print("ledBuffer[0]: ");
Serial.println(ledBuffer[0]);
*/
//Put the differance between the previous values and the target value into arrays
for(i=0; i<__numLedsInArray; i++)
{
diffBetweenCurrTarget[i]= target - ledStates[i][color];
}
/*
Serial.print("diffBetweenCurrTarget[0]: ");
Serial.println(diffBetweenCurrTarget[0]);
*/
//Put the step size into an array (will put into the float, they're hardly ever nice even numbers)
for(i=0; i<__numLedsInArray; i++)
{
ledStep[i]= (float)diffBetweenCurrTarget[i]/(float)numOfLoops; //EG: Target = 100; Current = 50. Differance = Tar-Curr; Diff = 50; StepSize = Diff/Loops; Loops=100; Diff/Loops = .5; ie .5 added to Current 100 Times increses it by 50. 50+50 = 100, the target :)
}
/*
Serial.print("ledStep[0]: ");
Serial.println(ledStep[0]);
*/
//Add the step amount into the FLOAT buffer, then typecast it and set the current LED state as that.
for(int b=0; b<numOfLoops; b++)
{
for(i=0; i<__numLedsInArray; i++)
{
ledBuffer[i]=ledBuffer[i] + ledStep[i];
ledStates[i][color]=(int)ledBuffer[i];
}
delay(time);
}
//Set the LEDS to the target value just to brush out any loose floats that didnt make it for some reason or another
for(i=0; i<__numLedsInArray; i++)
{
ledStates[i][color]=target;
}
/*
Serial.print("Done with easeAllRed; target: ");
Serial.println(target);
Serial.println("----------------");
*/
return;
}
void pattern_ledDebug() {
//This fades in and out the individual colors on the LED's.
for(int ledNumberID=0; ledNumberID < __numLedsInArray; ledNumberID++)
{
for(int ledLoopNum=0; ledLoopNum < 3; ledLoopNum++)
{
int i =0;
while(i <= 100)
{
ledStates[ledNumberID][ledLoopNum]=i;
i++;
delay(3);
}
i=100;
while(i>=0)
{
ledStates[ledNumberID][ledLoopNum]=i;
i--;
delay(3);
}
}
}
}
I'll change the code naming to reflect that it fades, thanks for the tip ;D
//Get differance between the current states and the target for R, G, and B
for(int i=0; i<__numLedsInArray; i++)
{
diffBetweenCurrTarget[i][__RED]= rTarget - ledStates[i][__RED];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__GREEN]= gTarget - ledStates[i][__GREEN];
}
for(int b=0; b<__numLedsInArray; b++)
{
diffBetweenCurrTarget[i][__BLUE]= bTarget - ledStates[i][__BLUE];
}