Calculating the Fade Mathematics

Home/Projects/Lightive Project/Calculating the Fade Mathematics

Calculating the Fade Mathematics

A C++ program was written to aid the design of code to be used to calculate and then implement fades on the driver unit.

The program reads in all of the values which would be sent over a standard radio packet. This is | BAR | RED Val| GREEN Val | BLUE Val | Fade Time | . It then collects some information which the system will already know, this is the current values of the selected bar. Once it had this information it manually calls an “outUpdate” which will write the output to the TLC chip and in turn the LED’s. For this test the command call to the TLC to change the outputs was commented out and replaced with a cout command so that the output is printed to the console.

Here is the code:

Fade_Maths.cpp

#include
#include
using namespace std;

#define FADE_ITERATION_TIME 5

//DEFINE VARIABLES HERE
uint16_t command[5];
float currentval[16];
float RememberChanVal[16];
uint16_t stepcount[16];
float stepsize[16];

// PROTOTYPE FUNCTIONS
void cinCommand();
void setFade();
void stepFade(uint8_t chan);
void outUpdate();

int main()
{
    cinCommand();
    setFade();
    outUpdate();

    return 0;
}

void cinCommand()
{
    cout << "Enter Bar:" << endl;     cin >> command[0];
    cout << "Enter RED value:" << endl;     cin >> command[1];
    cout << "Enter GREEN value:" << endl;     cin >> command[2];
    cout << "Enter BLUE value:" << endl;     cin >> command[3];
    cout << "Enter Fade Time:" << endl;     cin >> command[4];
    cout << endl;
    for(int x = 0 ; x < 3 ; x++)
		{
        cout << "Current Val for selected bar " << command[0]
        << " colour " << x << " Array Element "
        << ((3*command[0])+x) << endl;         cin >> currentval[(3*command[0])+x];
		}
     cout << endl;
}

void setFade()
{
		float difference;

		if( command[4] < FADE_ITERATION_TIME) // If the fade time is set to less than 5, then instant change, so set to 5 which will mean change in single step.
            command[4] = FADE_ITERATION_TIME;

        for(int x = 0 ; x < 3 ; x++)
		{
			uint16_t sentval = command[x+1];
			difference = (( 4095 - sentval ) - currentval[(3*command[0])+x]);
			stepcount[(3*command[0])+x] =  command[4]  / FADE_ITERATION_TIME;
			stepsize[(3*command[0])+x] = difference / (float)stepcount[(3*command[0])+x];

			cout << "Colour " << x << " array element " << ((3*command[0])+x)
			<< " Current Value " <<  currentval[(3*command[0])+x]
			<< " difference " << difference << " stepcount "
			<<  stepcount[(3*command[0])+x] << " stepsize "
			<< stepsize[(3*command[0])+x] << endl;
		}

}

void stepFade(uint8_t chan)
	{
		RememberChanVal[chan]= currentval[chan] + stepsize[chan];
		if( RememberChanVal[chan] < 0 )
			RememberChanVal[chan] = 0;
		//Tlc.set( chan, int(RememberChanVal[chan]) );
		cout << "channel " << (int)chan << " Remember Chan Val "
		<< int(RememberChanVal[chan]) < 0)
				 {
					stepFade(chan);
					fadeincomplete = stepcount[chan];
			     }
				}

				//Tlc.update();
        }
    }

The console output of the above code for a simple example:

Enter Bar:
0
Enter RED value:
4075
Enter GREEN value:
4075
Enter BLUE value:
4075
Enter Fade Time:
100

Current Val for selected bar 0 colour 0 Array Element 0
0
Current Val for selected bar 0 colour 1 Array Element 1
0
Current Val for selected bar 0 colour 2 Array Element 2
0

Colour 0 array element 0 Current Value 0 difference 20 stepcount 20 stepsize 1
Colour 1 array element 1 Current Value 0 difference 20 stepcount 20 stepsize 1
Colour 2 array element 2 Current Value 0 difference 20 stepcount 20 stepsize 1
channel 0 Remember Chan Val 1
channel 1 Remember Chan Val 1
channel 2 Remember Chan Val 1
channel 0 Remember Chan Val 2
channel 1 Remember Chan Val 2
channel 2 Remember Chan Val 2
channel 0 Remember Chan Val 3
channel 1 Remember Chan Val 3
channel 2 Remember Chan Val 3
channel 0 Remember Chan Val 4
channel 1 Remember Chan Val 4
channel 2 Remember Chan Val 4
channel 0 Remember Chan Val 5
channel 1 Remember Chan Val 5
channel 2 Remember Chan Val 5
channel 0 Remember Chan Val 6
channel 1 Remember Chan Val 6
channel 2 Remember Chan Val 6
channel 0 Remember Chan Val 7
channel 1 Remember Chan Val 7
channel 2 Remember Chan Val 7
channel 0 Remember Chan Val 8
channel 1 Remember Chan Val 8
channel 2 Remember Chan Val 8
channel 0 Remember Chan Val 9
channel 1 Remember Chan Val 9
channel 2 Remember Chan Val 9
channel 0 Remember Chan Val 10
channel 1 Remember Chan Val 10
channel 2 Remember Chan Val 10
channel 0 Remember Chan Val 11
channel 1 Remember Chan Val 11
channel 2 Remember Chan Val 11
channel 0 Remember Chan Val 12
channel 1 Remember Chan Val 12
channel 2 Remember Chan Val 12
channel 0 Remember Chan Val 13
channel 1 Remember Chan Val 13
channel 2 Remember Chan Val 13
channel 0 Remember Chan Val 14
channel 1 Remember Chan Val 14
channel 2 Remember Chan Val 14
channel 0 Remember Chan Val 15
channel 1 Remember Chan Val 15
channel 2 Remember Chan Val 15
channel 0 Remember Chan Val 16
channel 1 Remember Chan Val 16
channel 2 Remember Chan Val 16
channel 0 Remember Chan Val 17
channel 1 Remember Chan Val 17
channel 2 Remember Chan Val 17
channel 0 Remember Chan Val 18
channel 1 Remember Chan Val 18
channel 2 Remember Chan Val 18
channel 0 Remember Chan Val 19
channel 1 Remember Chan Val 19
channel 2 Remember Chan Val 19
channel 0 Remember Chan Val 20
channel 1 Remember Chan Val 20
channel 2 Remember Chan Val 20

Process returned 0 (0x0)   execution time : 54.114 s
Press any key to continue.

The next stage is to adapt this to the Arduino library.

By | 2015-01-13T23:40:23+00:00 February 17th, 2011|Lightive Project|0 Comments

About the Author:

A PhD in Electronic Engineering. A love for photography (www.islou.co.uk). An interest in tinkering, electronics and design. (www.louisc.co.uk).

Leave a Reply