I am currently programming my 7³ RGB LED Cube and I coded several functions for it and I am having problems making a fuction that lets a LED(later if it works multiple LEDs) fade from a rgb to an other rgb value. I am having one problem: Inside the three DoubleIfs
float highest
gets declared with the value of difgl or difgl or difbl. When i execute
fade(0,0,0,255,254,253,0);
difgl should be 253.00 which was confirmed by the Serial Monitor. In the Red DoubleIf
highest = difgl;
gets executed. And the value of highest on the Serial Monitor is 0.00. Why?!
void fade(uint8_t startr, uint8_t startg, uint8_t startb, uint8_t endr, uint8_t endg, uint8_t endb, int wait, int lednumber) {
int difr = endr - startr;
int difg = endg - startg;
int difb = endb - startb;
float difrl = (float)difr;
float difgl = (float)difg;
float difbl = (float)difb;
float facr, facg, facb;
uint8_t rgb[3];
float highest;
if (difr > difg)
if (difr > difb)
highest = difr;
if (difg > difr)
if (difg > difb)
highest = difgl;
if (difb > difr)
if (difb > difg)
highest = difb;
facr = difrl / highest;
facg = difgl / highest;
facb = difbl / highest;
Serial.println(facr);
for (int i = 0; i < highest; i++) {
setLED(lednumber, startr + difr * facr, startg + difg * facg, startb + difb * facb);
delayMicroseconds(wait);
}
}
I know that the function does not work if the differences are the same but this should be easy to fix later.
Your code to call fade misses a parameter and it does not want to compile. I've added a value the wait parameter.
I'm not quite sure what your problem is. The below code shows the individual values; it does not seem to show the behaviour that you mention (difgl equals 254, not 253). highest gives 255 as expected.
An additional advise: learn to properly indent (use tools -> autoformat in the IDE); all your if statements are highly confusing if you don't. Also I suggest that you use curly braces; it's clearer what goes with what. E.g.
if (difr > difg)
{
if (difr > difb)
{
highest = difr;
}
}