why doesn't this code work

hi i don't know why, but this code doesn't work for me, it's a code with two buttons and a gyroscope and an accelerometer (lately I'm trying to create a VR driver that emulates a mouse driver from a VR driver that I plan to add a joystick and RGB LED. this code is very complicated

#include <I2Cdev.h>
#include <Mouse.h>
#include<Wire.h>

#define MOUSE_SWITCH 4
#define MOUSE_LEFT 1
#define MOUSE_RIGHT 2

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

const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {
  pinMode(MOUSE_SWITCH, INPUT);
  pinMode(MOUSE_LEFT, INPUT);
  pinMode(MOUSE_RIGHT, INPUT);

  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {


  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6, true); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();
  // formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied


}
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;
  if (readMouseButton(MOUSE_SWITCH, lastSwitchState)) {
    if (mouseActive) {
      Mouse.end();
      mouseActive = false;
    } else {
      Mouse.begin();
      mouseActive = true;
    }
  }

  if (mouseActive) {


    // check left mouse btn
    if (readMouseButton(MOUSE_LEFT, lastMouseLeftState, INPUT_CHANGED_MODE)) {
      if (lastMouseLeftState == HIGH && !Mouse.isPressed()) {
        Mouse.press();


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

    }

    // check right mouse btn
    if (readMouseButton(MOUSE_RIGHT, lastMouseRightState, INPUT_HIGH_MODE)) {
      Mouse.press(MOUSE_RIGHT);

      delay(2000);
      Mouse.release(MOUSE_RIGHT);

    }
  }



}

It might help the forum if you explained what "doesn't work" means.

You presuambly know what your expecting the code to do, but the forum does not.

The code is expected to actually send from the real worldthe position to virtual reality from the gyroscope and accelerometer (x, y), I want it to work as a VR driver I don't expect more from the code (I didn't get any error message but when I tested it, the right or left button doesn't work and gyroscope and accelerometer does not work)

thank you in advance for the reply

  • you calculate roll and pitch but never use them
  • the readMouseButton() function is totally weird, you have a   return ret;line 63 so all the code after that is useless - and weird because it would call the readMouseButton() recursively.

you probably messed up when copying the code.

aha (the code is actually two together in one because I don't know how to create such code that will work) thank you

don't you know where i could find such code for work?

(Ne, já nevím ...that's the English forum here.)

There are tutorials for merging codes

I also want to ask one thing why the right button does not work properly?
Thank you in advance for your reply

#include <Mouse.h>
#define left 3
#define right 2
void setup() {
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  //initiate the Mouse library
  Mouse.begin();
}

void loop() {
  if (digitalRead(left) == HIGH) {
    Mouse.press();
    delay(20);

    if (digitalRead(left) == LOW) {
      Mouse.release();
      delay(100);

      if (digitalRead(right) == HIGH) {
        Mouse.press();


        if (digitalRead(right) == LOW) {
          Mouse.release();





        }
      }
    }
  }
}

because you messed up the brackets. You only test for the right button if the left button has been pressed

if you press ctrl-T in the IDE, it will indent the code and you'll see the blocks for what belongs where

  if (digitalRead(left) == HIGH) {
    Mouse.press();
    delay(20);

    if (digitalRead(left) == LOW) {
      Mouse.release();
      delay(100);

      if (digitalRead(right) == HIGH) {
        Mouse.press();


        if (digitalRead(right) == LOW) {
          Mouse.release();
        }
      }
    }
  }

see how you nested the tests?

now if you were to close the brackets after the if() {...} it would ident like this and you would do all the tests in a row

void loop() {
  if (digitalRead(left) == HIGH) {
    Mouse.press();
    delay(20);
  }

  if (digitalRead(left) == LOW) {
    Mouse.release();
    delay(20);
  }

  if (digitalRead(right) == HIGH) {
    Mouse.press();
    delay(20);
  }

  if (digitalRead(right) == LOW) {
    Mouse.release();
    delay(20);
  }
}

I'm unsure about your delays

you're really good, I didn't notice, thank you

how come that for me doesn't work for me right button how in real mouse?

in both cases you send the command Mouse.press();

when you don't give a parameter to the call, by default it's the left button press.

==> see the documentation

if you want to send a right click, it's Mouse.press(MOUSE_RIGHT);
(obviously same for release)

thank you, it works

Why does it not work? :frowning:

#include <MPU6050.h>


#include <I2Cdev.h>
#include <Mouse.h>
#include<Wire.h>


#define left 3
#define right 5

MPU6050 sensor;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;

const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {
  Serial.begin(9600);
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  Mouse.begin();
  sensor.initialize();
  if (!sensor.testConnection()) {
    while (1);
  }

}

void loop() {
  sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  vx = (gx + 15) / 150;
  vy = -(gz - 100) / 150;

  Mouse.move(vx, vy);

  if (digitalRead(left) == HIGH) {
    Mouse.press();
  }


  if (digitalRead(left) == LOW) {
    Mouse.release();
  }


  if (digitalRead(right) == HIGH) {
    Mouse.press(MOUSE_RIGHT);

  }



  if (digitalRead(right) == LOW) {
    Mouse.release(MOUSE_RIGHT);
  }






}

Add some Serial.print() statements and see where stops working at.

I improved the code, but I can't move the driver in vr why? the code works but otherwise I work only the right and left button but the gyroscope and the accelerometer not

#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

#define left 6
#define right 7
#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {
pinMode(left,INPUT);
pinMode(right,INPUT);
  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6, true); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();

  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied

  Serial.print("roll = ");
  Serial.print(roll,1);
  Serial.print(", pitch = ");
  Serial.println(pitch,1);
  delay(400);
   if (digitalRead(left) == HIGH) {
    Mouse.press();

  }
  if (digitalRead(left) == LOW) {
    Mouse.release();

  }
  if (digitalRead(right) == HIGH) {
    Mouse.press(MOUSE_RIGHT);

  }
  if (digitalRead(right) == LOW) {
    Mouse.release(MOUSE_RIGHT);






  }
}

hi why doesn't this code work properly with gyroscope and accelerometer? (lately I've been trying to make code to send data from gyroscope and accelerometer to vr but it doesn't work, buttons work right and left. I'm using mouse emulation program on vr driver it works but no position from gyroscope and accelerometer why?)thank you in advance for the reply

#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

#define left 6
#define right 7
#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6, true); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();

  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied

  Serial.print("roll = ");
  Serial.print(roll, 1);
  Serial.print(", pitch = ");
  Serial.println(pitch, 1);
  delay(400);
  if (digitalRead(left) == HIGH) {
    Mouse.press();

  }
  if (digitalRead(left) == LOW) {
    Mouse.release();

  }
  if (digitalRead(right) == HIGH) {
    Mouse.press(MOUSE_RIGHT);

  }
  if (digitalRead(right) == LOW) {
    Mouse.release(MOUSE_RIGHT);






  }
}

Where exactly in the code are you sending the data to the vr device ?

nowhere, I don't know how to do it I need help :frowning:

What sort of interface does this mysterious vr device have ?

Please post a link to it

Like this one?