#include <Wire.h>
const int MPU = 0x68; // MPU6050 I2C address
float AccX;
int sensor[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
float force = 0;
float mass =200;
float total_acc_x = 0;
int sensor_count = 0;
void setup() {
Serial.begin(9600);
for (int x = 0; x < 9 ; x++)
{
pinMode(sensor[x], OUTPUT);
digitalWrite(sensor[x], HIGH);
}
for (int x = 0; x < 9 ; x++)
{
digitalWrite(sensor[x], LOW);
Wire.begin(); // Initialize comunication
Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); //end the transmission
delay(100);
digitalWrite(sensor[x], HIGH);
}
}
void loop() {
sensor_count = 1;
for (int x = 0; x < 9 ; x++)
{
if (x==0)
{
digitalWrite(sensor[x], LOW);
// === Read acceleromter data === //
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
//For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis value
// Print the values on the serial monitor
//Serial.print(AccX);
//Serial.println("");
delay(20);
if (AccX<0)
{
AccX=AccX*-1;
}
if (AccX > 0.25)
{
total_acc_x = AccX + total_acc_x ;
sensor_count++;
sensor_count--;
Serial.println(total_acc_x);
}
digitalWrite(sensor[x], HIGH);
}
}
if (sensor_count>0)
{
total_acc_x = total_acc_x / sensor_count;
total_acc_x = total_acc_x*9.8;
force=(mass*total_acc_x)/2;
if (force!=0)
{
Serial.print(force);
Serial.println("");
}
}
total_acc_x = 0;
sensor_count=0;
}