Declaring float is acting crazy

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.

Thanks in advance

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.

void setup()
{
  Serial.begin(115200);

  fade(0, 0, 0, 255, 254, 253, 0, 0);
}

void loop()
{
  // put your main code here, to run repeatedly:

}


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;
  Serial.print("difrl: "); Serial.println(difrl);
  Serial.print("difgl: "); Serial.println(difgl);
  Serial.print("difbl: "); Serial.println(difbl);
  Serial.print("Highest: "); Serial.println(highest);
  facr = difrl / highest;
  facg = difgl / highest;
  facb = difbl / highest;
  Serial.print("facr: "); Serial.println(facr);
  Serial.print("facg: "); Serial.println(facg);
  Serial.print("facb: "); Serial.println(facb);
  //for (int i = 0; i < highest; i++) {
  //  setLED(lednumber, startr + difr * facr, startg + difg * facg, startb + difb * facb);
  //  delayMicroseconds(wait);
  //}
}

Output

difrl: 255.00
difgl: 254.00
difbl: 253.00
Highest: 255.00
facr: 1.00
facg: 1.00
facb: 0.99

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;
    }
  }

And you can write it shorter :wink:

  if (difr > difg && difr > difb)
  {
    highest = difr;
  }

In the Red DoubleIf

What IS a "Red DoubleIf"?