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);
}
}
}
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)
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.
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);
}
}