Variable declaration in order to keep the result of a division 0,

Hi!!! I'm writing because I'm experiencing some trouble with my arduino code. In the specific I need to normalize to the clear the data received from the TCS34725 sensor and I have to say that the data from the sensor are of 16 bit so that their range is 0-65535.
To normalize the data to clear I have to apply the following formula:
factor = 255 / clear value
Normalized value of red, green blue = value from sensor * factor.

Now, the result of factor is a 0 followed by a comma and the numbers, but is recognized as 0 so that the values of red, green and blue are 0!!

For example I receive the following data from the sensor:

red = 226
green= 381
blue= 451
clear= 1058

so that I have for red:

factor = 255/1058 = 0,2410
red = 226 * 0.2410

but I have just 0 for the values of red, green, blue as the factor would be 0.

the part of my arduino code to do that is :

float factor = 255 / c; /* Factor of Normalization */
  float rc = r*factor; /* value of red normalized to the clear */
  int redc = round(rc); /* Normalized value of red rounded to the nearest integer */
  float gc = g*factor; /* value of green normalized to the clear */
  int greenc = round(gc); /* Normalized value of green rounded to the nearest integer */
  float bc = b*factor; /*value of blue normalized to the clear */
  int bluec = round(bc); /* Normalized value of blue rounded to the nearest integer */

and before I've declared the variable in the following way:

float factor;
float rc;
float gc;
float bc;
int redc;
int greenc;
int bluec;

What I have to do in order to obtain a number different from zero for the variable factor? How Do I have to declare the variables?`

Really thanks for your help!!

Try:

1 Like

Really thanks :)!! Ok, I will try that and let's see!!

The C/C++ programming language does not recognize that format for fractional decimal values. Use the period character '.' instead of comma.

Also, expressions like 255/1058 use integer division, so zero is the correct and expected answer.

1 Like

Hi!! Yes, zero is the correct and expected answer: just asking to find a way to obtain 0.digit digit digit as results so that I can continue to work. I have to try using 255.0 to see what happens!! I will tell what my results will be!! Really thannks for your answer!!!

well floats in c++ don't have the same precision as on a pocket caclulator due to the number of bytes the value is stored into.

For more precision you could use variable-type double
or
using integervariables for large numbers and multiplying your numbers with a factor of 1.000.000

the result of your calculation 226 * 0.2410 = 54.466
if you use variable-type unsigned long your values can go up to 4.294.967.295

So with multiplying with 1_000_000
255*1000000/1058 = 241020.793950851

which in interger-math will be 241020 (the part before the decimal-point
next step
226 * 241020 = 54470520
226 * 241_020 = 54_470_520

At the beginning we multiplied with 10^6 = 1000000
a 1 with 6 zeros
Now you move the decimal-point 6 digits to the left

    123_456
 54_470_520
 54.47052

best regards Stefan

1 Like

Hi Stefan!!! I've tried with your advice (declaring the variables unsigned long and moltiplying and dividing for 1000000) and it works nice :)))!!! Really really thanks for the advices :)))

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.