Hello, I am pragramming the Adafruit 9-DOF IMU Breakout. I am programming the magnetometer right now and I have a problem. The code which gets the angle is this, I get it from the adafruit web page and it works fine.
mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
angleStart = orientation.heading;
}
I have modified it so I want the magnetometer to start reading the angle constantly, and when he has turned 90 degress it sends me a message. This is my code.
void setup()
{
Serial.begin(9600);
mag.begin(); //initialize the magnetometer
}
void loop()
{
getMagnetometer();
}
void getMagnetometer(){
sensors_event_t mag_event;
sensors_vec_t orientation;
float angleTurning = 0.0; //when you turn the sensor it shows you the angle --> it gets the angle while turning the device/sensor
float angleFinal = 0.0; //the angle of reference --> when the magnetometer reaches this angle it shows the message "Finish"
mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
angleTurning = orientation.heading; // gets the angle
angleFinal = angleTurning + 90; // the magnetometer should turn 90 degrees and than stop
}
/* angleTurning is less then the final angle
so it has to keep turning until it is bigger*/
/when angleTurning is bigger than the angleFinal it means that the magnetometer has been
turned so long until the angleTurning value is bigger than the final angle/
It's hard to see what you're trying to do, but try this
static float angleTurning = 0.0; //when you turn the sensor it shows you the angle --> it gets the angle while turning the device/sensor
static float angleFinal = 0.0; //
Why do you thumb your nose at our forum rules? Do you think that makes you more likely or less likely to get good help if you make the code harder to read?
Is there ant way that I can interrupt the void loop for a part of my code. I mean for example a void that I have created (void getMagnetometer) only happens once?
jac4:
Is there ant way that I can interrupt the void loop for a part of my code. I mean for example a void that I have created (void getMagnetometer) only happens once?
You could have a variable that marks whether or not it is time to do something.
boolean didItAlready = false;
void loop(){
if (didItAlready == false){
// do your thing once
didItAlready = true;
}
}