Unable to find what is wrong

Hi everyone. i have compliled a code for MPU9250 so as to get the data oth on the serial moitor and SSD1306 OLED screen , however when I compile I get multiple errors . I am not proficient in coding and hence have compiled this code from various sources, looks like I have made some mistakes but am unable to identify them . i am attaching the sketch please let me know where i am wrong.

sketch_newapr10a.ino (3.59 KB)

Please post the code and the error messages

I have attached the .ino file with the code. Here is the error message

Arduino: 1.8.2 (Windows XP), Board: "Arduino Pro or Pro Mini, ATmega328 (3.3V, 8 MHz)"

WARNING: library SparkFun_MPU-9250-DMP_Arduino_Library-master claims to run on (samd) architecture(s) and may be incompatible with your current board which runs on (avr) architecture(s).
C:\Documents and Settings\INDIAN\My Documents\Arduino\sketch_newapr10a\sketch_newapr10a.ino: In function 'void setup()':

sketch_newapr10a:37: error: 'Address' was not declared in this scope

Wire.beginTransmission(Address);

^

sketch_newapr10a:38: error: 'Register' was not declared in this scope

Wire.write(Register);

^

sketch_newapr10a:42: error: 'Nbytes' was not declared in this scope

Wire.requestFrom(Address, Nbytes);

^

sketch_newapr10a:45: error: 'Data' was not declared in this scope

Data[index++] = Wire.read();

^

sketch_newapr10a:52: error: 'Data' was not declared in this scope

Wire.write(Data);

^

sketch_newapr10a:58: error: a function-definition is not allowed here before '{' token

{

^

sketch_newapr10a:84: error: a function-definition is not allowed here before '{' token

{

^

sketch_newapr10a:144: error: expected '}' at end of input

}

^

sketch_newapr10a:144: error: expected '}' at end of input

sketch_newapr10a:144: error: expected '}' at end of input

exit status 1
'Address' was not declared in this scope

I have attached the .ino file with the code. Here is the error message

Why not simply post the code, and increase your chances of getting help?

Hi

I'm learning and struggling a bit too.

There is some advice for troubleshooting Arduino on line, but the lack of 'trace' and 'break' points makes it more difficult. I have found the shorthand macro for inserting and switching off trace messages useful - you need to pull them out if your application is timing critical and this lets you do that. See my earlier post about a problem I was having.

Within your program, try to put blocks of code that you can test separately into functions, and get them working one at a time.

If you need to ask for help, make it easy. Isolate the problem in question, and post just that code in full, having got it to compile if possible. People will not try to get their head around a whole project with compilation errors so they cannot see what is happening.

Hope that helps

inquisitive:
I have attached the .ino file with the code. Here is the error message

If you use the AutoFormat tool to lay out your program consistently you will see that you are missing a lot of { and }. Too many to list them all here.

For example I believe this line

void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data);

should be

void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data) {

and then there will need to be a corresponding } somewhere.

...R

waljoh:
There is some advice for troubleshooting Arduino on line, but the lack of 'trace' and 'break' points makes it more difficult. I have found the shorthand macro for inserting and switching off trace messages useful - you need to pull them out if your application is timing critical and this lets you do that. See my earlier post about a problem I was having.

There's no point in talking about trace and breakpoints when the code doesn't even compile.

There is no need for trace/breakpoints period!

Mark

holmes4:
There is no need for trace/breakpoints period!

Mark

Breakpoints I can often live without, but trace prints are invaluable, once the code compiles

inquisitive:
I have attached the .ino file with the code. Here is the error message

The problem is that not everybody can download files; e.g. those that use cell phones.

Your code (after auto format)

#include "quaternionFilters.h"
#include "MPU9250.h"
#include <EEPROM.h>

#include <Arduino.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include "Wire.h"
#define MPU9250_ADDRESS 0x68
#define ACC_FULL_SCALE_2_G 0x00
#define SerialPort Serial
Adafruit_SSD1306 display = Adafruit_SSD1306();
#include <SparkFunMPU9250-DMP.h>
MPU9250_DMP imu;
double roll , pitch, yaw;
long int pre_ts = 0;
void setup()
{

  {
    SerialPort.begin(115200);

    if (imu.begin() != INV_SUCCESS)
    {
      while (1)
      {
        SerialPort.println("Unable to communicate with MPU-9250");
        SerialPort.println("Check connections, and try again.");
        SerialPort.println();
        delay(3000);
      }
    }
    void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data);


    // Set register address
    Wire.beginTransmission(Address);
    Wire.write(Register);
    Wire.endTransmission();

    // Read Nbytes
    Wire.requestFrom(Address, Nbytes);
    uint8_t index = 0;
    while (Wire.available())
      Data[index++] = Wire.read();

    void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data);

    // Set register address
    Wire.beginTransmission(Address);
    Wire.write(Register);
    Wire.write(Data);
    Wire.endTransmission();



    void setup()
    {
      Wire.begin()
      I2CwriteByte(MPU9250_ADDRESS, 29, 0x06);
      I2CwriteByte(MPU9250_ADDRESS, 28, ACC_FULL_SCALE_2_G);
      I2CwriteByte(MPU9250_ADDRESS, 108, 0x2F);
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
      display.clearDisplay();
      display.setTextSize(3);
      display.setTextColor(WHITE);

      imu.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);


      imu.setGyroFSR(250); // Set gyro to 2000 dps
      // Accel options are +/- 2, 4, 8, or 16 g
      imu.setAccelFSR(2); // Set accel to +/-2g
      imu.setLPF(10); // Set LPF corner frequency to 5Hz
      imu.setSampleRate(10); // Set sample rate to 10Hz
      imu.setCompassSampleRate(50); // Set mag rate to 10Hz
    }

    pre_ts = millis();
    {

      //}
      void loop()
      {
        if ( imu.dataReady() )
        {
          imu.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
          printIMUData(millis() - pre_ts);
          pre_ts = millis();
        }
      }

      void printIMUData(long int dt)
      {

        float accelX = imu.calcAccel(imu.ax);
        float accelY = imu.calcAccel(imu.ay);
        float accelZ = imu.calcAccel(imu.az);
        float gyroX = imu.calcGyro(imu.gx) / 57.3;
        float gyroY = imu.calcGyro(imu.gy) / 57.3;
        float gyroZ = imu.calcGyro(imu.gz) / 57.3;
        float magX = imu.calcMag(imu.mx);
        float magY = imu.calcMag(imu.my);
        float magZ = imu.calcMag(imu.mz);

        SerialPort.println("Accel: " + String(accelX) + ", " + String(accelY) + ", " + String(accelZ) + " g");
        SerialPort.println("Gyro: " + String(gyroX) + ", " + String(gyroY) + ", " + String(gyroZ) + " dps");
        SerialPort.println("Mag: " + String(magX) + ", " + String(magY) + ", " + String(magZ) + " uT");
        SerialPort.println("Time: " + String(imu.time) + " ms");

        //Euler angle from accel


        pitch = atan2 (accelY , ( sqrt ((accelX * accelX) + (accelZ * accelZ))));
        roll = atan2(-accelX , ( sqrt((accelY * accelY) + (accelZ * accelZ))));

        // yaw from mag

        float Yh = (magY * cos(roll)) - (magZ * sin(roll));
        float Xh = (magX * cos(pitch)) + (magY * sin(roll) * sin(pitch)) + (magZ * cos(roll) * sin(pitch));

        yaw =  atan2(Yh, Xh);


        roll = roll * 57.3;
        pitch = pitch * 57.3;
        yaw = yaw * 57.3;

        Serial.println("pitch"  + String( pitch) );
        Serial.println("yaw" + String( yaw ));
        Serial.println("roll" + String( roll));

      }
      void loop() {
        I2Cread(MPU9250_ADDRESS, 0x3B, 14, Buf);
        int16_t ay = -(Buf[2] << 8 | Buf[3]);
        yangle = ay / 182.04;
        display.clearDisplay();
        display.setCursor(0, 10);
        display.print(yangle);
        display.display();
        delay(100);

      }

Please note the indentations after applying auto format. The last } should be at the beginning of a line; that it is not, indicates a problem with mismatching curly braces.

You have (in some way) two loop() functions and two setup() functions. What is the intention?

I used the auto formatted code but it still does not compile.

Anyway here is the my code.

[code]
#include "quaternionFilters.h"
#include "MPU9250.h"
#include <EEPROM.h>

#include <Arduino.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include "Wire.h"
#define MPU9250_ADDRESS 0x68
#define ACC_FULL_SCALE_2_G 0x00
#define SerialPort Serial
Adafruit_SSD1306 display = Adafruit_SSD1306();
#include <SparkFunMPU9250-DMP.h>
MPU9250_DMP imu;
double roll , pitch, yaw;
long int pre_ts=0;
void setup() 
{

    {
  SerialPort.begin(115200);

 if (imu.begin() != INV_SUCCESS)
  {
    while (1)
    {
      SerialPort.println("Unable to communicate with MPU-9250");
      SerialPort.println("Check connections, and try again.");
      SerialPort.println();
      delay(3000);
    }
  }
 void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data);

 
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.endTransmission();

  // Read Nbytes
  Wire.requestFrom(Address, Nbytes);
  uint8_t index = 0;
  while (Wire.available())
    Data[index++] = Wire.read();

void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data);

  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.write(Data);
  Wire.endTransmission();
 


void setup() 
{
  Wire.begin()
  I2CwriteByte(MPU9250_ADDRESS, 29, 0x06);
  I2CwriteByte(MPU9250_ADDRESS, 28, ACC_FULL_SCALE_2_G);
  I2CwriteByte(MPU9250_ADDRESS, 108, 0x2F);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(3);
  display.setTextColor(WHITE);

 imu.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);

 
  imu.setGyroFSR(250); // Set gyro to 2000 dps
  // Accel options are +/- 2, 4, 8, or 16 g
  imu.setAccelFSR(2); // Set accel to +/-2g
  imu.setLPF(10); // Set LPF corner frequency to 5Hz
  imu.setSampleRate(10); // Set sample rate to 10Hz
  imu.setCompassSampleRate(50); // Set mag rate to 10Hz
}

  pre_ts=millis();
{

//}
void loop() 
{
    if ( imu.dataReady() )
  {
    imu.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
    printIMUData(millis()-pre_ts);
    pre_ts=millis();
  }
}

void printIMUData(long int dt)
{  
  
  float accelX = imu.calcAccel(imu.ax);
  float accelY = imu.calcAccel(imu.ay);
  float accelZ = imu.calcAccel(imu.az);
  float gyroX = imu.calcGyro(imu.gx)/57.3;
  float gyroY = imu.calcGyro(imu.gy)/57.3;
  float gyroZ = imu.calcGyro(imu.gz)/57.3;
  float magX = imu.calcMag(imu.mx);
  float magY = imu.calcMag(imu.my);
  float magZ = imu.calcMag(imu.mz);

  SerialPort.println("Accel: " + String(accelX) + ", " + String(accelY) + ", " + String(accelZ) + " g");
  SerialPort.println("Gyro: " + String(gyroX) + ", " + String(gyroY) + ", " + String(gyroZ) + " dps");
 SerialPort.println("Mag: " + String(magX) + ", " + String(magY) + ", " + String(magZ) + " uT");
  SerialPort.println("Time: " + String(imu.time) + " ms");

//Euler angle from accel

 
   pitch = atan2 (accelY ,( sqrt ((accelX * accelX) + (accelZ * accelZ))));
   roll = atan2(-accelX ,( sqrt((accelY * accelY) + (accelZ * accelZ))));

   // yaw from mag

   float Yh = (magY * cos(roll)) - (magZ * sin(roll));
   float Xh = (magX * cos(pitch))+(magY * sin(roll)*sin(pitch)) + (magZ * cos(roll) * sin(pitch));

   yaw =  atan2(Yh, Xh);


    roll = roll*57.3;
    pitch = pitch*57.3;
    yaw = yaw*57.3;
   
  Serial.println("pitch"  + String( pitch) );
  Serial.println("yaw" + String( yaw ));
  Serial.println("roll" + String( roll));

}
void loop() {
  I2Cread(MPU9250_ADDRESS, 0x3B, 14, Buf);
  int16_t ay = -(Buf[2] << 8 | Buf[3]);
  yangle = ay / 182.04;
  display.clearDisplay();
  display.setCursor(0, 10);
  display.print(yangle);
  display.display();
  delay(100);

}

[/code]

this is the error message

Arduino: 1.8.2 (Windows XP), Board: "Arduino Pro or Pro Mini, ATmega328 (3.3V, 8 MHz)"

WARNING: library SparkFun_MPU-9250-DMP_Arduino_Library-master claims to run on (samd) architecture(s) and may be incompatible with your current board which runs on (avr) architecture(s).
C:\Documents and Settings\INDIAN\My Documents\Arduino\sketch_newapr10a\sketch_newapr10a.ino: In function 'void setup()':

sketch_newapr10a:37: error: 'Address' was not declared in this scope

Wire.beginTransmission(Address);

^

sketch_newapr10a:38: error: 'Register' was not declared in this scope

Wire.write(Register);

^

sketch_newapr10a:42: error: 'Nbytes' was not declared in this scope

Wire.requestFrom(Address, Nbytes);

^

sketch_newapr10a:45: error: 'Data' was not declared in this scope

Data[index++] = Wire.read();

^

sketch_newapr10a:52: error: 'Data' was not declared in this scope

Wire.write(Data);

^

sketch_newapr10a:58: error: a function-definition is not allowed here before '{' token

{

^

sketch_newapr10a:84: error: a function-definition is not allowed here before '{' token

{

^

sketch_newapr10a:144: error: expected '}' at end of input

}

^

sketch_newapr10a:144: error: expected '}' at end of input

sketch_newapr10a:144: error: expected '}' at end of input

exit status 1
'Address' was not declared in this scope

void setup()
{

    {
  SerialPort.begin(115200);

{How {many {curly {braces {are {needed {at {the {start {of {a {function {?}}}}}}}}}}}}

    while (1)
    {
      SerialPort.println("Unable to communicate with MPU-9250");
      SerialPort.println("Check connections, and try again.");
      SerialPort.println();
      delay(3000);
    }

Checking connections is not going to make that infinite loop end. A sledgehammer will.

 void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data);

What the f**k is a function prototype doing in the middle of setup()?

  Wire.beginTransmission(Address);

You don't seem to be able to take a hint, so I'm going to be (even more than usually) blunt. Just where the f**k DID you declare and initialize Address?

You didn't. Why the hell not?

inquisitive:
I used the auto formatted code

I somehow have my doubts. If I load the code from reply #10 and do an auto format, I still see that the last } is not at the beginning of a line. Auto format is not a tool that magically fixes your code but makes it easier to spot errors.

You get advise from everybody but you seem to ignore it; what happened with Robin's advise in reply #5 about the I2Cread()?

You were asked why you had two loop() functions and two setup() functions (in some way) and you don't answer that.

thanks all for your suggestions will work on them. it will take me some time to understand and implement them.

Hi,
If you read the error list, the first lines are;

Arduino: 1.8.2 (Windows XP), Board: "Arduino Pro or Pro Mini, ATmega328 (3.3V, 8 MHz)"

WARNING: library SparkFun_MPU-9250-DMP_Arduino_Library-master claims to run on (samd) architecture(s) and may be incompatible with your current board which runs on (avr) architecture(s).

It is telling you that the board you have and the library you are using are not compatible, that is the library is for another board with different architecture.

What board are you programming for?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom.. :slight_smile:

Why not start with the simple tutorial sketches that come with the IDE? Make an LED blink - that sort of thing.

I'd also suggest that you do a C++ tutorial. There are plenty of them online.

You are not going to have much luck mashing together two sketches that you don't understand in a language that you don't know well and trying to make it go by asking for bits and pieces of advice on a forum. We can fix narrow, specific issues here, but not "my whole sketch is wrong and doesn't even compile", which is what your problem is.

PaulS:
I'm going to be (even more than usually) blunt.

OK, I'm ready with my ear plugs installed. :o