I am trying to insert this equation in arduino ide, but arduino gives only 0.00 as output,
#include<math.h>
double c = -2.438;
double voltage;
void setup() {
Serial.begin(9600);
analogReference(EXTERNAL);//3.3v
// put your setup code here, to run once:
}
void loop() {
double a = analogRead(A0);
Serial.println(a);
double b = pow(a, c);
Serial.println(b);
// put your main code here, to run repeatedly:
}
output received in serial monitor is 0.00
0.00
0.00
0.00,
I am using sharp ir gp2y0a710k0f sensor which outputs analog values 0 - 1024 but practically values ranges from 200 - 1024 as far as I have seen, so "a" ranges from 200 - 1024
I have attached the serial monitor output in which if exponent value is positive and if exponent value is negative, I suppose Arduino IDE is not processing negative exponents in this way, may be there is some other way for it, may I know that?
Consider what values of a in this calculation - pow(a, -2.438)will give you anything other than 0.00, printed to two digits after the decimal. I calculate that a must be from 1 to 8, inclusive, to get anything other than 0.00. If values are from 200 to 1023, think about the magnitude of the result - 1 / (200^2) is 0.000025; that number printed to two decimal places is 0.00, and it will be bigger than 1 / (200^2.438). Numbers larger than 200 will yield even smaller results, and they'll print as 0.00, too.
If you multiply the result by 38610000, you'll get non-zero results for any value the ADC can provide.
If you're uncertain, print some test data:
#include<math.h>
double c = -2.438;
void setup() {
Serial.begin(9600);
for (uint16_t i = 0; i < 1024; i++) {
double a = (double)i;
Serial.print("a: ");
Serial.print(a);
Serial.print(" b: ");
double b = pow(a, c);
Serial.print(b);
Serial.print(" ");
Serial.print(38610000*b);
Serial.println();
}
}
void loop() {}
MarkT:
There is no double support on the Arduino, just float.
double is redefined as float (32 bit)
using double iso float will use 64bit double if available
standard println() does not support scientific notation, however you can do println(a, 40); which will print even the smallest Arduino float. but you need a wide screen .