Hi !
I am using an Arduino nano 33 IoT, and I want to put the data coming from my accelerometer in a range from 0 to 100. I have tried map function, constrain function and both together, but I always end up having values that exceed 100.
Here is the relevant part of my code :
float x, y, z; //for Accelero
float a, b, c; //for Gyro
int degreesX = 0;
int degreesY = 0;
int degreesZ = 0;
int degreesXconstrain = 0;
void IMUGetData(){
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
}
if (x > 0.1) {
x = 100*x;
degreesX = x/1.095;
Serial.print("Tilting up ");
Serial.println(degreesX);
Serial.print ('\t');
}
if (x < -0.1) {
x = -100*x;
degreesX = x/1.095;
Serial.print("Tilting down ");
Serial.println(degreesX);
Serial.print ('\t');
}
degreesX = map(degreesX, 0, 365, 0, 100);
degreesXconstrain = constrain(degreesX, 0, 100);
}
Thanks for your help !