#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define BUTTON_PIN 4 //indicate where the button is plugged in
#define LED_PIN 13 //indicate where the LED is plugged in
#define SCREEN_WIDTH 123 //define OLED screen width
#define SCREEN_HEIGHT 64 //define OLED screen height
#define ACCEL_PIN 1
Adafruit_MPU6050 mpu;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT); // define LED as an output
pinMode(BUTTON_PIN, INPUT); // define button as an input
Serial.begin(115200);
// Try to initialize!
mpu.begin();
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x68)) {
Serial.println("SSD1306 allocation failed");
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
int lastState = HIGH;
}
// the loop function runs over and over again forever
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
struct {sensors_vec_t a, g, temp;
//clear display
display.clearDisplay();
//display acceleration
display.setTextSize(1);
display.setCursor(0,0);
display.print("Acceleration: ");
display.print(a);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("m/s^2");
display.display();
/* Print out the values */
Serial.print(a.acceleration.x);
Serial.print(",");
Serial.print(a.acceleration.y);
Serial.print(",");
Serial.print(a.acceleration.z);
Serial.println("");
}