#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
const char TAGNAME[8] = "Alpha";
const int STATIC_DELAY = 1000; // normally 3000
const int MOVEMENT_DELAY = 300;
bool moving = false;
// int delayTime;
int button = 3;
unsigned long previousMillis = 0;
const int MPU_ADDR = 0x68;
Adafruit_MPU6050 mpu;
void setup() {
pinMode(button, INPUT);
Serial.begin(115200);
while (!mpu.begin(MPU_ADDR)) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
}
sensors_event_t event;
void loop() {
unsigned long currentMillis = millis();
mpu.getAccelerometerSensor()->getEvent(&event);
Serial.print("[");
Serial.print(millis());
Serial.print("] X: ");
Serial.print(event.acceleration.x);
Serial.print(", Y: ");
Serial.print(event.acceleration.y);
Serial.print(", Z: ");
Serial.print(event.acceleration.z);
Serial.println(" m/s^2");
delay(100);
// put your main code here, to run repeatedly:
if(digitalRead(button) == HIGH && currentMillis - previousMillis >= MOVEMENT_DELAY){
previousMillis = currentMillis;
printOut(true);
// sensorPrintOut();
} else if (currentMillis - previousMillis >= STATIC_DELAY) {
previousMillis = currentMillis;
printOut(false);
}
delay(50);
}
void sensorPrintOut() {
mpu.getAccelerometerSensor()->getEvent(&event);
Serial.print("[");
Serial.print(millis());
Serial.print("] X: ");
Serial.print(event.acceleration.x);
Serial.print(", Y: ");
Serial.print(event.acceleration.y);
Serial.print(", Z: ");
Serial.print(event.acceleration.z);
Serial.println(" m/s^2");
delay(100);
}
void printOut(bool moving) {
Serial.print(millis());
Serial.print(": ");
Serial.print(TAGNAME);
Serial.print(";");
Serial.print(moving);
Serial.println();
}