Problem making new "Illumination Fraction" variable for Color Mixing Lamp tutori

I've successfully completed the "Color Mixing Lamp" tutorial on page 53 of the Arduino Projects Book. Now I'm just tweaking the code to see if I can make some improvements.

I would like to show how much each of the photoresistors contributes to the overall sensor voltage, so that I can express the value in an "Illumination Fraction:" field.

To do this, I've added a few new float variables to the code:

int totalSensorValue = 0;
float redFraction = 0.00;
float greenFraction = 0.00;
float blueFraction = 0.00;

I've also added this to the void loop() section:

totalSensorValue = redSensorValue + greenSensorValue + blueSensorValue;
  redFraction = redSensorValue/totalSensorValue;
  greenFraction = greenSensorValue/totalSensorValue;
  blueFraction = blueSensorValue/totalSensorValue;
  Serial.print("\t Total Sensor Value: ");
  Serial.print(totalSensorValue);
  Serial.print("\t Illumination Fraction: \t Red: ");
  Serial.print(redFraction);
  Serial.print("\t Green: ");
  Serial.print(greenFraction);
  Serial.print("\t Blue: ");
  Serial.println(blueFraction);

Here's the problem. The values I get back are all zero:

 Total Sensor Value: 1062	 Illumination Fraction: 	 Red: 0.00	 Green: 0.00	 Blue: 0.00

What's going on here? I'm using an Arduino Ono R3 with the Aduino IDE 1.0.6. I will post the entire sketch if you need more info.

Try something like:

  redFraction = (float) redSensorValue/totalSensorValue;
  greenFraction = (float) greenSensorValue/totalSensorValue;
  blueFraction = (float) blueSensorValue/totalSensorValue;

Yes! It's working beautifully now. Thank you luisilva :smiley:

Did you know why now work?
In your original code you did an integer division where 3/5 = 0 (with remaining of 3).
By adding the cast for float, you are saying that one operator is float, and so, the operation must be made in float.

Yes I can see that now! It was given integers and trying to keep the same format, even though I declared "redFraction, bluefraction, and greenFraction" as floats. I will remember this for future projects. Thank you so much :smiley: