I just run mpu6050 at 16g configuration. but what i dont understand, is 16g the maximum value the accelerometer sensor can reach? i try it and with oled monitor. the sensor is attached to a semi automatic toy gun's slide and it reached 26.8g. i thought top acceleration reading is +16g?
lemme know if i have done something wrong in this code.
#include <Wire.h>
#include <MPU6050.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
MPU6050 mpu;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int a = 50;
float maxG_Total = 0.0;
float maxG_Total_decrement = 0.0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_16);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
}
void loop() {
updateVariables();
// Display variables on OLED
displayVariables();}
void updateVariables() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
maxG_Total = 0.0; // Reset maxG_Total to 0 at the beginning of each update
unsigned long startMillis = millis(); // Waktu mulai delay
while (millis() - startMillis < 230) {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Calculate G_Total
float accelScale = (32768.0/16); // Accelerometer sensitivity scale factor (LSB/g)
float ax_g = ax / accelScale;
float ay_g = ay / accelScale;
float az_g = az / accelScale;
float G_Total = sqrt(ax_g * ax_g + ay_g * ay_g + az_g * az_g);
if (G_Total > maxG_Total) { // Update maxG_Total
maxG_Total = G_Total;
}
}
// Update variables a based on maxG_Total and the appropriate interval
if (maxG_Total > 17.5 ) {
a--;
maxG_Total_decrement = 0.0; // Reset maxG_Total_decrement dari iterasi sebelumnya
maxG_Total_decrement = maxG_Total;
}
}
void displayVariables() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("a= ");
display.setTextSize(2);
display.println(a);
display.setTextSize(1);
display.print("MaxG= ");
display.setTextSize(2);
display.println(maxG_Total);
display.setTextSize(1);
display.print("RECORD= ");
display.setTextSize(2);
display.println(maxG_Total_decrement);
display.display();
}