Void function was not declared

i already saw several posts with the same topic, but i can't find any solution for mine. i know there are void setup and void loop, I've been working on determine offset value from mpu6050 using I2Cdev lib by Jeff Rowberg, beside the setup and loop there 2 other void function: void meansensor and void calibration. I'm using platform IO instead of arduino IDE, and when I paste the program code there's a problem

it said the void function was not declared, so I thought i must rearrange the code (which I think it's not the deal, but I try), I move the 2 void functions above the void setup, but it still said not declared, how to solve this problem? could it be the void functions must be inside the void loop or void setup?

#include <Arduino.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Wire.h>

///////////////////////////////////   CONFIGURATION   /////////////////////////////
//Change this 3 variables if you want to fine tune the skecth to your needs.
int buffersize=1000;     //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int acel_deadzone=8;     //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone=1;     //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

// default I2C address is 0x68
MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az,gx, gy, gz;

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

///////////////////////////////////   SETUP   ////////////////////////////////////
void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  // initialize serial communication
  Serial.begin(115200);

  // initialize device
  accelgyro.initialize();

  // wait for ready
  while (Serial.available() && Serial.read()); // empty buffer
  while (!Serial.available()){
    Serial.println(F("Send any character to start sketch.\n"));
    delay(1500);
  }                
  while (Serial.available() && Serial.read()); // empty buffer again

  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  delay(2000);
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  delay(3000);
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  delay(1000);
  // reset offsets
  accelgyro.setXAccelOffset(0);
  accelgyro.setYAccelOffset(0);
  accelgyro.setZAccelOffset(0);
  accelgyro.setXGyroOffset(0);
  accelgyro.setYGyroOffset(0);
  accelgyro.setZGyroOffset(0);
}

///////////////////////////////////   LOOP   ////////////////////////////////////
void loop() {
  if (state==0){
    Serial.println("\nReading sensors for first time...");
    meansensors();
    state++;
    delay(1000);
  }

  if (state==1) {
    Serial.println("\nCalculating offsets...");
    calibration();
    state++;
    delay(1000);
  }

  if (state==2) {
    meansensors();
    Serial.println("\nFINISHED!");
    Serial.print("\nSensor readings with offsets:\t");
    Serial.print(mean_ax); 
    Serial.print("\t");
    Serial.print(mean_ay); 
    Serial.print("\t");
    Serial.print(mean_az); 
    Serial.print("\t");
    Serial.print(mean_gx); 
    Serial.print("\t");
    Serial.print(mean_gy); 
    Serial.print("\t");
    Serial.println(mean_gz);
    Serial.print("Your offsets:\t");
    Serial.print(ax_offset); 
    Serial.print(",\t");
    Serial.print(ay_offset); 
    Serial.print(",\t");
    Serial.print(az_offset); 
    Serial.print(",\t");
    Serial.print(gx_offset); 
    Serial.print(",\t");
    Serial.print(gy_offset); 
    Serial.print(",\t");
    Serial.println(gz_offset); 
    Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
    Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
    Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)");
    while (1);
  }
}

///////////////////////////////////   FUNCTIONS   ////////////////////////////////////
void meansensors(){
  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

  while (i<(buffersize+101)){
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    
    if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
      buff_ax=buff_ax+ax;
      buff_ay=buff_ay+ay;
      buff_az=buff_az+az;
      buff_gx=buff_gx+gx;
      buff_gy=buff_gy+gy;
      buff_gz=buff_gz+gz;
    }
    if (i==(buffersize+100)){
      mean_ax=buff_ax/buffersize;
      mean_ay=buff_ay/buffersize;
      mean_az=buff_az/buffersize;
      mean_gx=buff_gx/buffersize;
      mean_gy=buff_gy/buffersize;
      mean_gz=buff_gz/buffersize;
    }
    i++;
    delay(2); //Needed so we don't get repeated measures
  }
}

void calibration(){
  ax_offset=-mean_ax/8;
  ay_offset=-mean_ay/8;
  az_offset=(16384-mean_az)/8;

  gx_offset=-mean_gx/4;
  gy_offset=-mean_gy/4;
  gz_offset=-mean_gz/4;
  while (1){
    int ready=0;
    accelgyro.setXAccelOffset(ax_offset);
    accelgyro.setYAccelOffset(ay_offset);
    accelgyro.setZAccelOffset(az_offset);

    accelgyro.setXGyroOffset(gx_offset);
    accelgyro.setYGyroOffset(gy_offset);
    accelgyro.setZGyroOffset(gz_offset);

    meansensors();
    Serial.println("...");

    if (abs(mean_ax)<=acel_deadzone) ready++;
    else ax_offset=ax_offset-mean_ax/acel_deadzone;

    if (abs(mean_ay)<=acel_deadzone) ready++;
    else ay_offset=ay_offset-mean_ay/acel_deadzone;

    if (abs(16384-mean_az)<=acel_deadzone) ready++;
    else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

    if (abs(mean_gx)<=giro_deadzone) ready++;
    else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

    if (abs(mean_gy)<=giro_deadzone) ready++;
    else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

    if (abs(mean_gz)<=giro_deadzone) ready++;
    else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

    if (ready==6) break;
  }
}

Hi @sinus_20 ,

please post your code in code tags:

image

The keyword "void" just tells the compiler that a function does not return any value.

An error message "... was not declared in this scope" tells you that a variable or a function has been called in the code which the compiler does not know.

ec2021

Hi @ec2021, sure, i just edit my post by adding the code

Thanks, I'll will check it ... :wink:

The sketch compiles on Wokwi (a online Arduino Emulator) ...

Which IDE are you using?

i'm not using Arduino IDE, I use platform.io which is a extension availble in microsoft visual studio

In that case you might have to declare the functions before they are used. The Arduino IDE provides some special support for developers so one does not have to do this.

Here your code in an Arduino IDE environment:

https://wokwi.com/projects/425502846553763841

You might need a forward declaration...
Arduino does that for you...

As @build_1971 wrote you can try forward declaration:

Put this before the main loop and leave the real declaration where it is:

void meansensors();
void calibration();


1 Like

well actually there's a problem where i can't use arduino ide

  1. my pc is run out of memory space and I've tried to install some library with arduino before but it kept error so i uninstall it
  2. i feel easier with platform.io interface which i've been working for my project for several months

can anybody please suggest me how to solve this with platform.io? 'cause I will use some void functions beside loop and setup for my upcoming project

alright, i'm gonna try it

Try what I've posted in #9 ...

it works!!, thank u so much @ec2021 @build_1971, the program is running rn

i wanna ask if somebody have tried to calibrate mpu6050 module using the same library from Jeff Rowberg? does the process take a long time? cause the program still calculate the offset (it's been like 5 minutes and still go on as i type this)

You can use the Wokwi link (post #7) to check if it takes longer in your environment than in the emulation. Usually Wokwi is quite close to the "real thing" ...

Just two hints:

  • You added the code in code tags in post #1; it would be better next time to do this in a new post. That allows other members to understand the flow of the thread
  • If your problem has been solved mark the relevant post as the "solution". That again shows other members that this thread does not require further assistance.

Good luck and have fun with Arduino!
ec2021

1 Like

thank so much for ur advice, well silly me, i forgot that I change the MPU6050 i2c address to 0x69, now i get my offset value, thank u so much

Don't worry, things happen ...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.