kuekuekuedf Posted November 5, 2024 Posted November 5, 2024 So, I see very few people post their code if they are using an arduino based solution with a acceleorometer. I spent a while searching, and then I think I used someone thing I found and atttempted quite a few edits it to try get it right. I still haven't. Please help! What am I missing? Should it auto centre it self and refresh every like 6 seconds or so? I'm using a LIS331HH Adafruit accelerometer and a Arduino Micro. Here's the code. #include <Wire.h> #include <Adafruit_Sensor.h> #include <Joystick.h> #include <SPI.h> #include <Adafruit_LIS331HH.h> // #include <Keyboard.h> #define LIS331HH_SCK 13 #define LIS331HH_MISO 12 #define LIS331HH_MOSI 11 // Used for hardware & software SPI #define LIS331HH_CS 10 // Define the MPU6050 object Adafruit_MPU6050 mpu; Adafruit_LIS331HH lis = Adafruit_LIS331HH(); // Define the joystick object Joystick_ joystick; // Keyboard_ keyboard; // Define the potentiometer pin const int potPin = A0; float xbaseline, ybaseline, zbaseline; sensors_event_t accel, gyro, temp; void setup(void) { Serial.begin(115200); Serial.println("LIS331HH test!"); //if (!lis.begin_SPI(LIS331HH_CS)) { // if (!lis.begin_SPI(LIS331HH_CS, LIS331HH_SCK, LIS331HH_MISO, LIS331HH_MOSI)) { if (! lis.begin_I2C()) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); } Serial.println("LIS331HH found!"); // lis.setRange(H3LIS331_RANGE_100_G); // 100, 200, or 400 G! lis.setRange(LIS331HH_RANGE_6_G); // 6, 12, or 24 G Serial.print("Range set to: "); switch (lis.getRange()) { case LIS331HH_RANGE_6_G: Serial.println "6 g"); break; case LIS331HH_RANGE_12_G: Serial.println("12 g"); break; case LIS331HH_RANGE_24_G: Serial.println("24 g"); break; } // lis.setDataRate(LIS331_DATARATE_50_HZ); Serial.print("Data rate set to: "); switch (lis.getDataRate()) { case LIS331_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; case LIS331_DATARATE_50_HZ: Serial.println("50 Hz"); break; case LIS331_DATARATE_100_HZ: Serial.println("100 Hz"); break; case LIS331_DATARATE_400_HZ: Serial.println("400 Hz"); break; case LIS331_DATARATE_1000_HZ: Serial.println("1000 Hz"); break; case LIS331_DATARATE_LOWPOWER_0_5_HZ: Serial.println("0.5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_1_HZ: Serial.println("1 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_2_HZ: Serial.println("2 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_5_HZ: Serial.println("5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_10_HZ: Serial.println("10 Hz Low Power"); break; } // Set the joystick range joystick.begin(false); joystick.setXAxisRange(-512, 512); joystick.setYAxisRange(-512, 512); joystick.setZAxisRange(0, 1023); // keyboard.begin(); for (int i = 0; i < 10; i++) { mpu.getEvent(&accel, &gyro, &temp); xbaseline += accel.acceleration.x; ybaseline += accel.acceleration.y; zbaseline += accel.acceleration.z; } xbaseline /= 10; ybaseline /= 10; zbaseline /= 10; // lis.setDataRate(LIS331_DATARATE_50_HZ); Serial.print("Data rate set to: "); switch (lis.getDataRate()) { case LIS331_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; case LIS331_DATARATE_50_HZ: Serial.println("50 Hz"); break; case LIS331_DATARATE_100_HZ: Serial.println("100 Hz"); break; case LIS331_DATARATE_400_HZ: Serial.println("400 Hz"); break; case LIS331_DATARATE_1000_HZ: Serial.println("1000 Hz"); break; case LIS331_DATARATE_LOWPOWER_0_5_HZ: Serial.println("0.5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_1_HZ: Serial.println("1 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_2_HZ: Serial.println("2 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_5_HZ: Serial.println("5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_10_HZ: Serial.println("10 Hz Low Power"); break; } } void loop() { /* Get a new sensor event, normalized */ sensors_event_t event; lis.getEvent(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("\t\tX: "); Serial.print(event.acceleration.x); Serial.print(" \tY: "); Serial.print(event.acceleration.y); Serial.print(" \tZ: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2 "); Serial.println(); // Convert the acceleration values to joystick values int x = map(event.acceleration.x - xbaseline, -2, 2, -512, 512); int y = map(event.acceleration.y - ybaseline, -2, 2, -512, 512); // Read the potentiometer value and convert it to a joystick value int z = analogRead(potPin); z = map(z, 0, 1023, 0, 1023); // Send the joystick values to the computer joystick.setXAxis(x); joystick.setYAxis(y); joystick.setZAxis(z); joystick.sendState(); joystick.sendState(); // Wait for a short amount of time to prevent overwhelming the HID driver delay(20); }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now