Sharp Proximity Sensor and Exponents [SOLVED]

A quick question here: how do you use exponents in the web IDE? I’m trying to implement the calculation: distance = 41.543 * (voltage+ 0.30221) ^ -1.5281; where distance is in cm. The equation was found on Sparkfun for the Sharp proximity sensor. Anyway, how do I code the exponent into the web IDE? I’ve tried it as written in the equation, pow(base,exponent), exp(), e^(), value**()… but I keep getting a compile error stating that pow(), exp(), etc is not defined in this scope.

Any help would be very much appreciated. Thank you!

You need to include math.h:

#include <math.h>

double distance;
double voltage;
uint16_t adc_reading;

void setup() {
    pinMode(A0,INPUT);
}

void loop() {
    adc_reading = analogRead(A0);
    voltage = 3.3 * (double)adc_reading / 4095.0;
    // distance = 41.543 (voltage+ 0.30221) ^ -1.5281; 
    distance = 41.543 * pow((voltage + 0.30221), -1.5281);
    delay(1000);
}
3 Likes

Excellent! Thank you @bko.

1 Like