Need help with a coding problem

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.

sensors_event_t mag_event;
sensors_vec_t orientation;
float angleStart = 0.0;

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.

sensors_event_t mag_event;
sensors_vec_t orientation;
float angleStart = 0.0;
float angleFinal = 0.0;

mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
angleStart = orientation.heading;
angleFinal = angleStart + 90;
}

if(angleStart >= angleFinal){

Serial.println("Finish");

}

But angleStart never reaches angleFinal because of the void loop, it never ends so angleFinal increases when angleStart increases.

Could somenone help me out?

Thank you very much.

It's unclear what the scope of the variables and their initialisers is.
Can you post all your code and use code tags to make it easier to read?

I hope this is more understandable:

#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_9DOF.h>

Adafruit_9DOF dof = Adafruit_9DOF();
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);

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*/

if(angleTurning <= angleFinal){
Serial.println("Keep turning");
}

/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
/

else{
Serial.println("Finish turning");
}

delay(500);
}

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;     //

Groove:
use code tags

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?

Thank you groove for your interest, but I that's not going to work because I have tried it before.
But thank you again.

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;
   }
}

If you only want it to happen once, put that code in the setup() function. That's what it is there for.

Thank you for your time, I am going to try this.