Hi all,
So for a project i need de divide the ADC output with a constant.
- The ADC output range is 0 to 475
- The output variable needs to be 0-100
At the Arduino reference they advise against using floats as this will slow down the Arduino. The thing is. In the example below the 4,75 is a float if i'm not mistaking. When i measure the time it takes the output is always 4uS.
int awnser = 0;
int ADCval = 238;
unsigned long time1 = 0;
unsigned long time2 = 0;
void setup() {
Serial.begin(115200); // Start serial comminucation
}
void loop() {
time1 = micros();
awnser = ADCval / 4.75 ;
time2 = micros();
Serial.print ("the awnser is: ");
Serial.println(awnser);
Serial.print ("time passed: ");
Serial.println(time2 - time1);
delay (2000);
}
Even if i run the math 50 times, the time taken remains 4uS, (less than the resolution of the micros function. Therefore i see no problem in using the float. Am i doing something wrong here? Or is the speed of the math that fast?
Anyway, i can prevent using float doing the following:
unsigned int Factor = 475;
awnser = ADCval * 100 / Factor;
I have to use a unsigned integer to prevent overflow it the ADC*100 value. This works fine as if seems to force the math into using unsigned integers instead of integers.
The thing is, I have no way to tell if there is any difference in the time it takes compared to using floats because both seem to work very fast.
Am i doing something wrong here, or is there really no issue using floats?