calling variable from other tab

Hello,

We are making a quadcopter for a project at school, but we are having some beginners issues.
We are using the library and example from Jeff Rowberg and trying to make a led go on (in another tab) when the yaw value reaches 30
We also have tried to put "extern" before we make float imu, but it both doesnt really seem to work.....

We really dont know what and how to change the code and hope you can help us get it working.

but we are having some beginners issues.

Starting with terminology. You call functions. You do not call variables.

We really dont know what and how to change the code and hope you can help us get it working.

First, NEVER post pictures of code. You don't want a picture of page two of an answer do you? The one that says "This page intentionally left blank"?

Second, banish the phrase "but it both doesnt really seem to work" from your vocabulary. Something happened. Describe exactly what happened. You expected something to happen. Explain exactly what you expected.

If both files are .ino, extern is not needed. The IDE will join all .ino files together, starting with the one named the same as the sketch ( so the compiler sees it as one big .cpp file. ).

If you place the float variable above the functions it is used in ( maybe the top of the main .ino. ) then you should have some luck hopefully. extern is only needed to share variables between c++ .cpp source files.

Your setup is only a guess however, post the code from both tabs.

Okay, i have changed the float to be in the main .ino file but the led still wont go on.
Yhe main .ino is an example of jeff rowberg.
I cant put it on the forum because it is longer than 9500 characters, but here is a link to his github:

https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_DMP6/MPU6050_DMP6.ino

i have edited the example so that "float imu=ypr[0]" is beneath "float ypr[3]" (line 139)

Second, banish the phrase "but it both doesnt really seem to work" from your vocabulary. Something happened. Describe exactly what happened. You expected something to happen. Explain exactly what you expected.

We wanted to use the example of jeff rowberg, making a led blink when the yaw value, goes over 30. We tried adding a float"imu", in another tab and adding an if statement, but the led doesnt go on when any of the values (yaw, pitch and roll) goes over 30.
We tested the led by using the blink example and the led was fine.

Sorry for the "bad" post

I cant put it on the forum because it is longer than 9500 characters, but here is a link to his github:

Is it Jeff's code that you are having problems with? No it is not. It is YOUR code. See that Additional Options text just below this box? (Yes, you have to start to reply.) Click that, and use the Attach section to attach a zip file of all of YOUR code.

i have edited the example so that "float imu=ypr[0]" is beneath "float ypr[3]" (line 139)

Show me, don't tell me.

Is it Jeff's code that you are having problems with? No it is not. It is YOUR code. See that Additional Options text just below this box? (Yes, you have to start to reply.) Click that, and use the Attach section to attach a zip file of all of YOUR code.

I never told that there was a problem with jeff's code, that was just a in reply of pyro_65. But anyways, i have attached the .zip file.

i have edited the example so that "float imu=ypr[0]" is beneath "float ypr[3]" (line 139)

Show me, don't tell me.

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
float imu=ypr[0];

MPU6050_DMP6_ino.zip (5.91 KB)

VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
float imu=ypr[0];

These are global variables. The values are assigned when the program starts. Clearly, the yaw value will be 0 at this time.

If you want to diddle with an LED pin at some other point in the execution of the program, don't base your decisions on imu. Unless you assign a new value to imu first.

In a project with several .ino files the extra files are loaded in alphabetical order after the principal file. Global variables in an earlier file can be seen by a later file, but not vice versa.

...R

If you want to diddle with an LED pin at some other point in the execution of the program, don't base your decisions on imu. Unless you assign a new value to imu first.

But where should we place the "float imu=ypr[0]" in MPU6050_dmp6.ino so that it is able to be used in ledimu.ino? Because if we place it in this if statement:

#ifdef OUTPUT_READABLE_REALACCEL
            // display real acceleration, adjusted to remove gravity
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetAccel(&aa, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
            Serial.print("areal\t");
            Serial.print(aaReal.x);
            Serial.print("\t");
            Serial.print(aaReal.y);
            Serial.print("\t");
            Serial.println(aaReal.z);
        #endif

we get an error from ledimu.ino that "imu was not declared in this scope"
We tried placing the "float imu=ypr[0]" outside the if statement, but inside the void loop, but we still get the same error ("imu was not declared in this scope" )

But where should we place the "float imu=ypr[0]" in MPU6050_dmp6.ino so that it is able to be used in ledimu.ino?

Why do you think you need to copy the value in ypr[0] to a different variable?

Instead of whining about "if I do this, that happens", post the code where you "do this" so we can SEE what is wrong.

Oh, and learn about scope. And, I'm not referring to the mouthwash.

Instead of whining about "if I do this, that happens", post the code where you "do this" so we can SEE what is wrong.

we tried this:

#ifdef OUTPUT_READABLE_YAWPITCHROLL
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
            float imu=ypr[0];
            Serial.print("ypr\t");
            Serial.print(ypr[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr[2] * 180/M_PI);
        #endif

and we tried this(at the bottom of the code you will find float imu=ypr[0]; ):

void loop() {
    // if programming failed, don't try to do anything
    if (!dmpReady) return;

    // wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt && fifoCount < packetSize) {
        // other program behavior stuff here
        // .
        // .
        // .
        // if you are really paranoid you can frequently test in between other
        // stuff to see if mpuInterrupt is true, and if so, "break;" from the
        // while() loop to immediately process the MPU data
        // .
        // .
        // .
    }

    // reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

    // get current FIFO count
    fifoCount = mpu.getFIFOCount();

    // check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        // reset so we can continue cleanly
        mpu.resetFIFO();
        Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

        // read a packet from FIFO
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;

        #ifdef OUTPUT_READABLE_QUATERNION
            // display quaternion values in easy matrix form: w x y z
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            Serial.print("quat\t");
            Serial.print(q.w);
            Serial.print("\t");
            Serial.print(q.x);
            Serial.print("\t");
            Serial.print(q.y);
            Serial.print("\t");
            Serial.println(q.z);
        #endif

        #ifdef OUTPUT_READABLE_EULER
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetEuler(euler, &q);
            Serial.print("euler\t");
            Serial.print(euler[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(euler[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(euler[2] * 180/M_PI);
        #endif

        #ifdef OUTPUT_READABLE_YAWPITCHROLL
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

            Serial.print("ypr\t");
            Serial.print(ypr[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr[2] * 180/M_PI);
        #endif


        #ifdef OUTPUT_READABLE_REALACCEL
            // display real acceleration, adjusted to remove gravity
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetAccel(&aa, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
            Serial.print("areal\t");
            Serial.print(aaReal.x);
            Serial.print("\t");
            Serial.print(aaReal.y);
            Serial.print("\t");
            Serial.println(aaReal.z);
        #endif

        #ifdef OUTPUT_READABLE_WORLDACCEL
            // display initial world-frame acceleration, adjusted to remove gravity
            // and rotated based on known orientation from quaternion
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetAccel(&aa, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
            mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
            Serial.print("aworld\t");
            Serial.print(aaWorld.x);
            Serial.print("\t");
            Serial.print(aaWorld.y);
            Serial.print("\t");
            Serial.println(aaWorld.z);
        #endif
    
        #ifdef OUTPUT_TEAPOT
            // display quaternion values in InvenSense Teapot demo format:
            teapotPacket[2] = fifoBuffer[0];
            teapotPacket[3] = fifoBuffer[1];
            teapotPacket[4] = fifoBuffer[4];
            teapotPacket[5] = fifoBuffer[5];
            teapotPacket[6] = fifoBuffer[8];
            teapotPacket[7] = fifoBuffer[9];
            teapotPacket[8] = fifoBuffer[12];
            teapotPacket[9] = fifoBuffer[13];
            Serial.write(teapotPacket, 14);
            teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
        #endif

        // blink LED to indicate activity
        blinkState = !blinkState;
        digitalWrite(LED_PIN, blinkState);
         float imu=ypr[0];
    }

}

In both cases, the variable is local to the else block.

You do NOT need to define imu. So, stop doing it.

In the function that diddles with the LED pin, base your decision on the value in ypr[0]. That value is updated every time you call mpu.dmpGetYawPitchRoll().

PaulS:
In the function that diddles with the LED pin, base your decision on the value in ypr[0]. That value is updated every time you call mpu.dmpGetYawPitchRoll().

thanks! this worked!