Why doesn't this code work?

Hi, I need help, I don't know why this code doesn't work, (I wanted to create a driver for VR based on how I tilt it so it tilts in the game, but it doesn't work)

Ready_Hand_v4.ino (5.6 KB)

Too little information.

For hints on how to successfully get help, please read and follow the directions in the post titled "How to get the best out of the forum".

I use Arduino Leonardo, (I'm interested in it for half a year, but I haven't made much progress in arduino) and writes to me it (to an error occurred while compiling with the board) Arduino 1.8.13

Could you also take a few moments to Learn How To Use The Forum.

Please take note of the sections about how to post your code using code tags and how to post the complete error message.

#include <MPU6050.h>
#include <Mouse.h>
#include <RGBLed.h>
#include <Wire.h>
int calculate_IMU_error();
const int MPU = 0x68;
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float a;
float G;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
int c = 0;

float elapsedTime, currentTime, previousTime;

RGBLed led(9, 10, 11,  RGBLed::COMMON_CATHODE);

// Mouse buttons
#define MOUSE_SWITCH 2
#define MOUSE_LEFT_BTN 5
#define MOUSE_RIGHT 3

#define MOUSE_X_AXIS_INPT A0
#define MOUSE_Y_AXIS_INPT A1

#define MOUSE_SENSITIVITY 3
#define JOYSTICK_DEADZONE 1



#define INPUT_LOW_MODE 1
#define INPUT_HIGH_MODE 2
#define INPUT_CHANGED_MODE 3


bool mouseActive = false;
bool lastSwitchState = HIGH;
bool lastMouseRightState = HIGH;
bool lastMouseLeftState = HIGH;




void setup() {
  Serial.begin(9600);
  Wire.begin ();
  Wire.beginTransmission (MPU);
  Wire.write (0x6B);
  Wire.write (0x00);
  Wire.endTransmission (true);
  pinMode(MOUSE_SWITCH, INPUT);
  pinMode(MOUSE_LEFT_BTN, INPUT);
  pinMode(MOUSE_RIGHT, INPUT);

  Wire.beginTransmission (MPU);
  Wire.write (0x1C);
  Wire.endTransmission (true);


  Wire.beginTransmission (MPU);
  Wire.write (0x1B);

  Wire.endTransmission (true);
  delay (20);


  calculate_IMU_error();
  delay(20);
}
void handleMouse() {
  int xReading = analogRead(MOUSE_X_AXIS_INPT);
  int yReading = analogRead(MOUSE_Y_AXIS_INPT);

  int mouseX = map(xReading, 0, 1024, -MOUSE_SENSITIVITY, MOUSE_SENSITIVITY);
  int mouseY = map(yReading, 0, 1024, MOUSE_SENSITIVITY, -MOUSE_SENSITIVITY);


  if (mouseX > JOYSTICK_DEADZONE || mouseX < -JOYSTICK_DEADZONE || mouseY < -JOYSTICK_DEADZONE || mouseY > JOYSTICK_DEADZONE)
  {
    Mouse.move(mouseX, mouseY, 0);

  }
}


bool readMouseButton(int button, bool &lastState, unsigned char mode = INPUT_LOW_MODE) {
  bool ret = false;
  bool state = digitalRead(button);
  if (state != lastState) {
    if ((mode == INPUT_LOW_MODE && state == LOW) || (mode == INPUT_HIGH_MODE && state == HIGH) || mode == INPUT_CHANGED_MODE) {
      ret = true;
    }
  }
  lastState = state;
  return ret;
}

void loop() {
  Wire.beginTransmission (MPU);
  Wire.write (0x3B);
  Wire.endTransmission (false);
  Wire.requestFrom (MPU, 6, true);


  AccX = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  AccY = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  AccZ = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  accAngleX = (atan (AccY / sqrt (pow (AccX, 2) + pow (AccZ, 2))) * 180 / PI) - 0.58;
  accAngleY = (atan (-1 * AccX / sqrt (pow (AccY, 2) + pow (AccZ, 2))) * 180 / PI) + 1.58;

  Wire.beginTransmission (MPU);
  Wire.write (0x43);
  Wire.endTransmission (false);
  Wire.requestFrom (MPU, 6, true);

  GyroX = (Wire.read () << 8 | Wire.read ()) / 131.0;
  GyroY = (Wire.read () << 8 | Wire.read ()) / 131.0;
  GyroZ = (Wire.read () << 8 | Wire.read ()) / 131.0;

  GyroX = GyroX + 0.16;
  GyroY = GyroY - 4.22;
  GyroZ = GyroZ - 0.26;

  gyroAngleX = gyroAngleX + GyroX * elapsedTime;
  gyroAngleY = gyroAngleY + GyroY * elapsedTime;
  yaw = yaw + GyroZ * elapsedTime;

  roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
  pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;

  G = sqrt (AccX * AccX + AccY * AccY + AccZ * AccZ) - 1.03;

  a = 9.81 * G;




  if (readMouseButton(MOUSE_SWITCH, lastSwitchState)) {
    if (mouseActive) {
      Mouse.end();
      mouseActive = false;
    } else {
      Mouse.begin();
      mouseActive = true;
    }
    if (mouseActive) {
      handleMouse();


      if (readMouseButton(MOUSE_LEFT_BTN, lastMouseLeftState, INPUT_CHANGED_MODE)) {
        if (lastMouseLeftState == HIGH && !Mouse.isPressed()) {
          Mouse.press();
          led.flash(255, 0, 0, 200), Mouse.press();

        } else if (lastMouseLeftState == LOW && Mouse.isPressed())
          Mouse.release();
      }

      if (readMouseButton(MOUSE_RIGHT, lastMouseRightState, INPUT_HIGH_MODE)) {
        Mouse.press(MOUSE_RIGHT);
        led.flash(0, 0, 255, 200), Mouse.press();
        delay(2000);
        Mouse.release(MOUSE_RIGHT);
      }
    }
    {
      void calculate_IMU_error(); {


        while (c < 200) {
          Wire.beginTransmission(MPU);
          Wire.write(0x3B);
          Wire.endTransmission(false);
          Wire.requestFrom(MPU, 6, true);
          AccX = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
          AccY = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
          AccZ = (Wire.read() << 8 | Wire.read()) / 8192.0 ;

          AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
          AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
          c++;
          AccErrorX = AccErrorX / 200;
          AccErrorY = AccErrorY / 200;
          c = 0;
          while (c < 200) {
            Wire.beginTransmission(MPU);
            Wire.write(0x43);
            Wire.endTransmission(false);
            Wire.requestFrom(MPU, 6, true);
            GyroX = Wire.read() << 8 | Wire.read();
            GyroY = Wire.read() << 8 | Wire.read();
            GyroZ = Wire.read() << 8 | Wire.read();
            GyroErrorX = GyroErrorX + (GyroX / 131.0);
            GyroErrorY = GyroErrorY + (GyroY / 131.0);
            GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
            c++;
          }
          GyroErrorX = GyroErrorX / 200;
          GyroErrorY = GyroErrorY / 200;
          GyroErrorZ = GyroErrorZ / 200;
        }
      }
    }
  }
}
and here is the error message

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Development Board: "Arduino Leonardo"

C: \ Users \ PC-RTX2070 \ Desktop \ Ready_Hand_v4 \ Ready_Hand_v4.ino: 23: 0: warning: "MOUSE_RIGHT" redefined

 #define MOUSE_RIGHT 3

 

In file included from C: \ Users \ PC-RTX2070 \ Desktop \ Ready_Hand_v4 \ Ready_Hand_v4.ino: 2: 0:

C: \ Program Files \ WindowsApps \ ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt \ libraries \ Mouse \ src / Mouse.h: 38: 0: note: this is the location of the previous definition

 #define MOUSE_RIGHT 2

 

C: \ Users \ PC-RTX ~ 1 \ AppData \ Local \ Temp \ ccGcwr5J.ltrans0.ltrans.o: In function `setup ':

C: \ Users \ PC-RTX2070 \ Desktop \ Ready_Hand_v4 / Ready_Hand_v4.ino: 69: undefined reference to `calculate_IMU_error () '

collect2.exe: error: ld returned 1 exit status

Multiple libraries "MPU6050.h" found

 Used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ MPU6050

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Arduino-MPU6050-master

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ mpu6050-master

Multiple libraries found "I2Cdev.h"

 Used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ I2Cdev

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VRController-master

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ ArduinoMotionSensorExample-master

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ mpu6050-master

Multiple "Wire.h" libraries found

 Used: C: \ Users \ PC-RTX2070 \ Documents \ ArduinoData \ packages \ arduino \ hardware \ avr \ 1.8.3 \ libraries \ Wire

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Wire-master

Multiple "Mouse.h" libraries found

 Used: C: \ Program Files \ WindowsApps \ ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt \ libraries \ Mouse

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Mouse-1.0.1

 Not used: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Mouse-master

exit status 1

An error occurred while compiling on the Arduino Leonardo board.



This report should have more information in
"Display detailed output at compile time"
according to the enabled option in File -> Settings.

"Doesn't work" conveys very little information.

Back to you.

Edit: the code doesn't compile. Why didn't you say that?

 void calculate_IMU_error(); {

Lose the semicolon.
It looks like you've tried to embed that function in another function.
That won't compile.

I would like the controller on the VR to do that when I move to the left so that the VR also moves to the left, I want it to work backlit when I press the right button so it turns green, for example when the left button is so red. i want to use gyroscope and accelerometer mpu6050 without magnetometer to read the position in real time and then put it in VR but i don't know how i used 2 codes and i made one with them then i would like to put on / off button there.

69: undefined reference to `calculate_IMU_error ()

Your function definition for calculate IMU error is messed up. It is now buried in the loop(), surrounded by extra brackets, has a incorrect ';' and not properly broken out.

This code should be outside of setup() and loop() along with your other function definitions like handleMouse() and readMouse()

void calculate_IMU_error() {
  while (c < 200) {
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 6, true);
    AccX = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
    AccY = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
    AccZ = (Wire.read() << 8 | Wire.read()) / 8192.0 ;

    AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
    AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
    c++;
    AccErrorX = AccErrorX / 200;
    AccErrorY = AccErrorY / 200;
    c = 0;
    while (c < 200) {
      Wire.beginTransmission(MPU);
      Wire.write(0x43);
      Wire.endTransmission(false);
      Wire.requestFrom(MPU, 6, true);
      GyroX = Wire.read() << 8 | Wire.read();
      GyroY = Wire.read() << 8 | Wire.read();
      GyroZ = Wire.read() << 8 | Wire.read();
      GyroErrorX = GyroErrorX + (GyroX / 131.0);
      GyroErrorY = GyroErrorY + (GyroY / 131.0);
      GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
      c++;
    }
    GyroErrorX = GyroErrorX / 200;
    GyroErrorY = GyroErrorY / 200;
    GyroErrorZ = GyroErrorZ / 200;
  }
}

The second error is very clear

#define MOUSE_RIGHT 3

In file included from C: \ Users \ PC - RTX2070 \ Desktop \ Ready_Hand_v4 \ Ready_Hand_v4.ino: 2: 0:

C: \ Program Files \ WindowsApps \ ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt \ libraries \ Mouse \ src / Mouse.h: 38: 0: note: this is the location of the previous definition

#define MOUSE_RIGHT 2

The Mouse.h() library and the written code both define MOUSE_RIGHT with different values. You probably don't need the redefinition in your code. Let the library define it as 2.

so should i set it up? if so, will you also get an error message or what did you mean outside? 
#include <MPU6050.h>
#include <Mouse.h>
#include <RGBLed.h>
#include <Wire.h>
int calculate_IMU_error();
void calculate_IMU_error() {
  while (c < 200) {
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 6, true);
    AccX = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
    AccY = (Wire.read() << 8 | Wire.read()) / 8192.0 ;
    AccZ = (Wire.read() << 8 | Wire.read()) / 8192.0 ;

    AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
    AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
    c++;
    AccErrorX = AccErrorX / 200;
    AccErrorY = AccErrorY / 200;
    c = 0;
    while (c < 200) {
      Wire.beginTransmission(MPU);
      Wire.write(0x43);
      Wire.endTransmission(false);
      Wire.requestFrom(MPU, 6, true);
      GyroX = Wire.read() << 8 | Wire.read();
      GyroY = Wire.read() << 8 | Wire.read();
      GyroZ = Wire.read() << 8 | Wire.read();
      GyroErrorX = GyroErrorX + (GyroX / 131.0);
      GyroErrorY = GyroErrorY + (GyroY / 131.0);
      GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
      c++;
    }
    GyroErrorX = GyroErrorX / 200;
    GyroErrorY = GyroErrorY / 200;
    GyroErrorZ = GyroErrorZ / 200;
  }
}


const int MPU = 0x68;
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float a;
float G;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
int c = 0;

float elapsedTime, currentTime, previousTime;

RGBLed led(9, 10, 11,  RGBLed::COMMON_CATHODE);

// Mouse buttons
#define MOUSE_SWITCH 2
#define MOUSE_LEFT_BTN 5
#define MOUSE_RIGHT 2

#define MOUSE_X_AXIS_INPT A0
#define MOUSE_Y_AXIS_INPT A1

#define MOUSE_SENSITIVITY 3
#define JOYSTICK_DEADZONE 1



#define INPUT_LOW_MODE 1
#define INPUT_HIGH_MODE 2
#define INPUT_CHANGED_MODE 3


bool mouseActive = false;
bool lastSwitchState = HIGH;
bool lastMouseRightState = HIGH;
bool lastMouseLeftState = HIGH;




void setup() {
  Serial.begin(9600);
  Wire.begin ();
  Wire.beginTransmission (MPU);
  Wire.write (0x6B);
  Wire.write (0x00);
  Wire.endTransmission (true);
  pinMode(MOUSE_SWITCH, INPUT);
  pinMode(MOUSE_LEFT_BTN, INPUT);
  pinMode(MOUSE_RIGHT, INPUT);

  Wire.beginTransmission (MPU);
  Wire.write (0x1C);
  Wire.endTransmission (true);


  Wire.beginTransmission (MPU);
  Wire.write (0x1B);

  Wire.endTransmission (true);
  delay (20);


  calculate_IMU_error();
  delay(20);
}
void handleMouse() {
  int xReading = analogRead(MOUSE_X_AXIS_INPT);
  int yReading = analogRead(MOUSE_Y_AXIS_INPT);

  int mouseX = map(xReading, 0, 1024, -MOUSE_SENSITIVITY, MOUSE_SENSITIVITY);
  int mouseY = map(yReading, 0, 1024, MOUSE_SENSITIVITY, -MOUSE_SENSITIVITY);


  if (mouseX > JOYSTICK_DEADZONE || mouseX < -JOYSTICK_DEADZONE || mouseY < -JOYSTICK_DEADZONE || mouseY > JOYSTICK_DEADZONE)
  {
    Mouse.move(mouseX, mouseY, 0);

  }
}


bool readMouseButton(int button, bool &lastState, unsigned char mode = INPUT_LOW_MODE) {
  bool ret = false;
  bool state = digitalRead(button);
  if (state != lastState) {
    if ((mode == INPUT_LOW_MODE && state == LOW) || (mode == INPUT_HIGH_MODE && state == HIGH) || mode == INPUT_CHANGED_MODE) {
      ret = true;
    }
  }
  lastState = state;
  return ret;
}

void loop() {
  Wire.beginTransmission (MPU);
  Wire.write (0x3B);
  Wire.endTransmission (false);
  Wire.requestFrom (MPU, 6, true);


  AccX = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  AccY = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  AccZ = (Wire.read () << 8 | Wire.read ()) / 8192.0;
  accAngleX = (atan (AccY / sqrt (pow (AccX, 2) + pow (AccZ, 2))) * 180 / PI) - 0.58;
  accAngleY = (atan (-1 * AccX / sqrt (pow (AccY, 2) + pow (AccZ, 2))) * 180 / PI) + 1.58;

  Wire.beginTransmission (MPU);
  Wire.write (0x43);
  Wire.endTransmission (false);
  Wire.requestFrom (MPU, 6, true);

  GyroX = (Wire.read () << 8 | Wire.read ()) / 131.0;
  GyroY = (Wire.read () << 8 | Wire.read ()) / 131.0;
  GyroZ = (Wire.read () << 8 | Wire.read ()) / 131.0;

  GyroX = GyroX + 0.16;
  GyroY = GyroY - 4.22;
  GyroZ = GyroZ - 0.26;

  gyroAngleX = gyroAngleX + GyroX * elapsedTime;
  gyroAngleY = gyroAngleY + GyroY * elapsedTime;
  yaw = yaw + GyroZ * elapsedTime;

  roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
  pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;

  G = sqrt (AccX * AccX + AccY * AccY + AccZ * AccZ) - 1.03;

  a = 9.81 * G;




  if (readMouseButton(MOUSE_SWITCH, lastSwitchState)) {
    if (mouseActive) {
      Mouse.end();
      mouseActive = false;
    } else {
      Mouse.begin();
      mouseActive = true;
    }
    if (mouseActive) {
      handleMouse();


      if (readMouseButton(MOUSE_LEFT_BTN, lastMouseLeftState, INPUT_CHANGED_MODE)) {
        if (lastMouseLeftState == HIGH && !Mouse.isPressed()) {
          Mouse.press();
          led.flash(255, 0, 0, 200), Mouse.press();

        } else if (lastMouseLeftState == LOW && Mouse.isPressed())
          Mouse.release();
      }

      if (readMouseButton(MOUSE_RIGHT, lastMouseRightState, INPUT_HIGH_MODE))
        Mouse.press(MOUSE_RIGHT);
      led.flash(0, 0, 255, 200), Mouse.press();
      delay(2000);
      Mouse.release(MOUSE_RIGHT);
    }
  }



}
int calculate_IMU_error();
void calculate_IMU_error() {

What do you think you are doing here? It would be worth your time to review the basics on functions.

This revision does not look correct. You don't want the same pin as input for two different things.

// Mouse buttons
#define MOUSE_SWITCH 2
#define MOUSE_LEFT_BTN 5
#define MOUSE_RIGHT 2

Thank you for your help

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