hi. im trying to make code (on an arduino leonardo) to detect swing acceleration and then put in a keyboard input along with turn on a vibration motor.
I honestly don't know what the specific problem is, its fairly inconsistent. Sometimes the arduino doesnt get anything from the accelerometer, sometimes it activates all the stuff once and then just starts sending nothing, sometimes it starts activating the stuff but then doesnt get to the deactivation of the key or the vibration. its had moments where its somewhat worked. it originally kind of worked but was inconsistent and the input didnt work the way i wanted.
this is the culmination of my many attempts to make this thing work
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Keyboard.h>
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long hit = 50;
const unsigned long brrr = 500;
const int numReadings = 10;
int readings[numReadings];
int index = 0;
int total = 0;
int average = 0;
int hitcheck = 0;
int a_pos = 0;
int motorPin = 8;
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
startMillis = millis();
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
// set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// set gyro range to +- 500 deg/s
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
total -= readings[index];
readings[index] = g.gyro.z;
total += readings[index];
index ++;
if (index >= numReadings)
index = 0;
average = total / numReadings;
//bbbbbSerial.println(average);
if((average >= 7) || (average <= -7)){
a_pos = 1;
}
if(a_pos == 1) {
if(hitcheck == 0) {
startMillis = millis();
Keyboard.press('b');
Keyboard.release('b');
digitalWrite(motorPin, HIGH);
hitcheck = 1;
}
currentMillis = millis();
if (currentMillis - startMillis >= brrr) {
digitalWrite(motorPin, LOW);
a_pos = 0;
hitcheck = 0;
}
}
}
ive reached the dark night of the soul in my programming journey, idk what to do to make this work. ive tried a few template accelerometer readers and meddled with them to my wits end. i have delved into coding insanity. please save me.
hi mr jremington. that code was really cool and worked, then i put the thing on its side and it didnt like gravity i guess because it starting wigging out, which is fine coz the system doesnt need to be on its side for extended amounts of time. however i reset the code and uploaded it back onto the arduino and now its just printing 0s.
The accelerometer measures the acceleration due to gravity as well as that due to other forces. While the sensor is held still, any axis that is vertical will read either +1 g if it is pointing up, or -1 g if pointing down (g is measured in raw sensor units).
With the swing in motion, you can expect the acceleration to vary between + or -2 g.