I made a gaming gun using arduino leonardo and mpu6050 sensor with mouse library . I used mouse left click as the trigger and right click as the reload.
Problem is when i press the trigger button or the reload button repeatedly my arduino get stuck and i have to disconnect the usb and reconnect it to pc for fix the arduino.If you could point the the correct direction much appreciated.
#include "I2Cdev.h"
#include "MPU6050.h"
#include <Mouse.h>
#include <Bounce2.h>
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro; // default is 0x68; with AD0 high it's 0x69
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t sensitivityDivider = 250;
signed char mouseVX, mouseVY;
const unsigned long BTN_THROTTLE_DELAY = 40;
const int btnPin[] = {8, 9, 10};
const char mouseBtn[] = {MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT};
Bounce * btn = new Bounce[3];
void setup() {
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif;
delay(10);
accelgyro.initialize();
mpu.setXAccelOffset(-4963);
mpu.setYAccelOffset(508);
mpu.setZAccelOffset(1735);
mpu.setXGyroOffset(42);
mpu.setYGyroOffset(-13);
mpu.setZGyroOffset(-7);;
for (int i = 0; i < 3; i++) {
btn[i].attach(btnPin[i], INPUT_PULLUP);
btn[i].interval(BTN_THROTTLE_DELAY);
}
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
processMouse(-gy, gz);
processBtns();
delay(15);
}
void processMouse(int dX, int dY) {
mouseVX = -dY / sensitivityDivider;
mouseVY = -dX / sensitivityDivider;
if (mouseVX != 0 || mouseVY != 0) {
}
}
void processBtns() {
for (int i = 0; i < 3; i++) {
btn[i].update();
if (btn[i].fell()) {
Mouse.press(mouseBtn[i]);
} else if (btn[i].rose()) {
Mouse.release(mouseBtn[i]);
}
}
}
I don't see anything special, but without the source code for that two local header files "I2Cdev.h" and "MPU6050.h" (why local and not global?) we can't have a deeeper look inside. Can you post them?
Anyway, this code seems to be incomplete (e.g. an empy if() statement inside processMouse), but the best thing to do when something is not working is to use the serial to have some debug information about what the code "sees" and "does". Something like (please get rid of that useless consecutive empty rows):
#include "I2Cdev.h"
#include "MPU6050.h"
#include <Mouse.h>
#include <Bounce2.h>
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro; // default is 0x68; with AD0 high it's 0x69
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t sensitivityDivider = 250;
signed char mouseVX, mouseVY;
const unsigned long BTN_THROTTLE_DELAY = 40;
const int btnPin[] = {8, 9, 10};
const char mouseBtn[] = {MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT};
Bounce * btn = new Bounce[3];
void setup() {
Serial.begin(115200);
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Serial.println("Wire");
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Serial.println("Fastwire");
Fastwire::setup(400, true);
#endif;
delay(10);
Serial.println("Initializing gyro...");
accelgyro.initialize();
mpu.setXAccelOffset(-4963);
mpu.setYAccelOffset(508);
mpu.setZAccelOffset(1735);
mpu.setXGyroOffset(42);
mpu.setYGyroOffset(-13);
mpu.setZGyroOffset(-7);;
for (int i = 0; i < 3; i++) {
btn[i].attach(btnPin[i], INPUT_PULLUP);
btn[i].interval(BTN_THROTTLE_DELAY);
}
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
processMouse(-gy, gz);
processBtns();
delay(15);
}
void processMouse(int dX, int dY) {
mouseVX = -dY / sensitivityDivider;
mouseVY = -dX / sensitivityDivider;
if (mouseVX != 0 || mouseVY != 0) {
}
}
void processBtns() {
for (int i = 0; i < 3; i++) {
btn[i].update();
if (btn[i].fell()) {
Serial.print("btn[");Serial.print(i);Serial.println(" fell!");
Mouse.press(mouseBtn[i]);
} else if (btn[i].rose()) {
Serial.print("btn[");Serial.print(i);Serial.println(" rose!");
Mouse.release(mouseBtn[i]);
}
}
}
See what you get over Serial monitor (remember to set it to the same speed), and if needed please post here the result and what you did and saw during the test.
If even this can't help, make some "aseptic" tests. I mean, get rid of mouse and gyro and keep just bounce: if it works, it's something related to the excluded libraries and/or settings.
PS: why use #if with two symbols to compare instead of just an #ifdef ?
Thanks for answering. It seems like the problem is in the wiring of the sensor and buttons.And also i will care about the useless things that I did in the code.