# Mapping float values without losing accuracy.

I have been using the onboard accelerometer on the Adafruit CircuitPlayground which returns float values between -9.8 and 9.8. However I want to map these raw accelerometer values to a larger range (0 to 180). The way I’ve written it right now, the Serial monitor always prints out values between 0 and 180 but when I move the accelerometer the mapped values change by 10’s, and thus loses a lot of accuracy. Is there a way to map float values to a larger range without consecutive values changing by such a large margin? The mapped values are going to be used for servo angles, so having each angle change by 10 is not the smooth rotation that I would like.

``````#include <Adafruit_CircuitPlayground.h>
float accelerometer;
float mappedAccel;
void setup() {
CircuitPlayground.begin();
Serial.begin(9600);
}

void loop() {
accelerometer = CircuitPlayground.motionX();
mappedAccel = map(accelerometer,-9.8,9.8,0,180);
Serial.println(mappedAccel);
}
``````

Thanks!
-Skyler

I've never seen an accelerometer that returns a float.

Here is some information about the accelerometer on the Circuit Playground. It says calling CircuitPlayground.motionX() will return a floating point variable.

My comment in reply #1 still stands (hint)

I am not sure I quite understand, could you please elaborate?

Thanks again!

The device is internally returning a binary value. The library is presumably doing the conversion to float.

But most importantly you are using map() on floats which it doesn't support.

You can scale floats by multipling. map() is there because you can't do arbitrary scaling in C's integer
arithmetic without taking care with the truncating behaviour, use map() for integers only.

Thank you! Got it working.

Good.

I've used this map function for floats several times.

``````// map() for floats, see https://www.arduino.cc/en/Reference/Map
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

/Mogens