Hi, I'm interfacing a MPU-6050 with an arduino through i2c and constantly gathering the accelerometer data. My code will turn on an LED when the Gforce on the Z axis reaches above 0.7 and turn off when it is below 0.7. However, the LED will only turn on when the Gforce has been above 0.7 for lets say 20 samples and turn off when below 0.7 for again 20 samples. Any idea on how this might be approached?
So it should work by gathering 20 samples of accelerometer data and if all 20 samples are above 0.7 in a row then it should turn the LED on and vice versa for turning the LED off.
This is the initial code that works but doesn't collect the 20 samples. It changes the LED on every sample.
if (gForceZ > 0.7 ) {
digitalWrite(LED, HIGH);
}
if (gForceZ < 0.7 ){
digitalWrite(LED, LOW);
}