/* fadetoColor(int row[n], int col[n], int toColor[n][3], totalSteps) - a function to fade the color of specific LEDs within the screenInfo array from its current color to the input color.

The row and col arrays list the LEDs that will be faded. Obviously, the sizes must equal.

The input color must be given as a n by 3 array with the form {{red, green, blue}, ...} where red, green, blue are the number values of the respective colors brightness. These are listed in the same ordering as their respective LED in the row and col arrays

The current LED colors are stored in an array called screenInfo[row][col][3]. This holds a {r,g,b} for each LED, the LED locations being the values of the row and col. This is the array that the display interrupts reference to produce the actual LED Matrix Display,

the totalSteps input defines how many steps the fade will go through. Each step is defined in the code to take 1/10th of a second, so a 10 step fade will take 1 second. (this is easy to change)

*/

void fadetoColor ( int row [ ] , int col [ ] , toColor [ ] [ 3 ] , totalStep ) {

/*

First we size the row array. The size of this array tells us how many LED's we are manipulating. This is then stored in a variable that we can use in the following control

structures.

*/

int countLEDs = sizeof ( row [ ] ) ;

/*

Then, we gather the "old" colors from the LEDs - i think this step could be removed, but i wanted to make sure that I wouldnt run into errors while i manipulate screenInfo

*/

int oldRGB [ countLEDs ] [ 3 ] ; //initilize the array to hold the old colors

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

//We use the row/col arrays to locate the correct LED in screenInfo

oldRGB [ led ] [ i ] = screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] ;

}

}

/*

Now that we have the old colors, and the size of the LED arrays, we can calculate the absolute difference between the color values (as an integer)

*/

int absDif [ countLED ] [ 3 ] ; //Initilize the array to hold the absolute differences

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

//Fill the array with the differences

absDif [ led ] [ i ] = abs ( oldRGB [ led ] [ i ] - toColor [ led ] [ i ] ) ;

}

}

/*

We also need to know whether the LED will be increasing, or decreasing in brightness. We

create an array to store this data, which will be used later to control a loop

*/

int upDown [ countLEDs ] [ 3 ] ; //Initilize the upDown storage array

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

if ( oldRGB [ led ] [ i ] < toColor [ led ] [ i ] ) {

upDown [ led ] [ i ] = 1 ; //1 indicates that the color is increasing

}

else {

upDown [ led ] [ i ] = 0 ; //0 indicates that the color is decreasing

}

}

}

/*

In addition to the upDown, oldRGB, toColor, we need to calculate the increment by which the

color brightness' will be increased or decreased. To do this, we fill an array with

values of absDif/total step, and store the result as an integer.

This is a problem area, need to make sure the integer is a realistic value to produce a fade. If the total step is set too high compared to the absDif, then the truncation errors may mess up the look of the fade.

*/

int changeStep [ countLEDs ] [ 3 ] ; //initilize the array for the step values

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

//Fill the array with the step values

changeStep [ led ] [ i ] = integer ( absDif [ led ] [ i ] / totalStep [ led ] [ i ] ) ; /*need the actual command to

convert a float to an integer. Problem area */

}

}

/*

Now that we have the necessary data to fade, we can being the loop that actually changes the screenInfo array.

*/

int stepCount ; //used to control the while loop

while ( stepCount < totalStep ) {

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

if ( upDown [ led ] [ i ] == 1 ) {

screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] =

screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] + changeStep [ led ] [ i ] ;

}

else {

screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] =

screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] - changeStep [ led ] [ i ] ;

}

}

}

stepCount ++;

delay ( 100 ) ; //delay to allow the display interrupt to update a few times.

}

/*

Finally, we send a command to display the final color, just to be sure it is shown

*/

for ( int led = 0 ; led < countLEDs ; led ++ ) {

for ( int i = 0 ; i < 3 ; i ++ ) {

screenInfo [ row [ led ] ] [ col [ led ] ] [ i ] = toColor [ led ] [ i ] ;

}