Created
April 23, 2022 22:04
-
-
Save rmkanda/f53ad3eed96c5509136c9cf5fdde3abe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_MPU6050.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <Wire.h> | |
#define SCREEN_WIDTH 128 | |
#define SCREEN_HEIGHT 64 | |
#define OLED_RESET 4 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
Adafruit_MPU6050 mpu; | |
String lastDirection = "Unknown"; | |
float magnitude(sensors_vec_t &vec) | |
{ | |
return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); | |
} | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
while (!Serial) | |
delay(10); // will pause Zero, Leonardo, etc until serial console opens | |
Serial.println("Adafruit MPU6050 test!"); | |
// Try to initialize! | |
if (!mpu.begin()) | |
{ | |
Serial.println("Failed to find MPU6050 chip"); | |
while (1) | |
{ | |
delay(10); | |
} | |
} | |
Serial.println("MPU6050 Found!"); | |
mpu.setAccelerometerRange(MPU6050_RANGE_8_G); | |
Serial.print("Accelerometer range set to: "); | |
switch (mpu.getAccelerometerRange()) | |
{ | |
case MPU6050_RANGE_2_G: | |
Serial.println("+-2G"); | |
break; | |
case MPU6050_RANGE_4_G: | |
Serial.println("+-4G"); | |
break; | |
case MPU6050_RANGE_8_G: | |
Serial.println("+-8G"); | |
break; | |
case MPU6050_RANGE_16_G: | |
Serial.println("+-16G"); | |
break; | |
} | |
mpu.setGyroRange(MPU6050_RANGE_500_DEG); | |
Serial.print("Gyro range set to: "); | |
switch (mpu.getGyroRange()) | |
{ | |
case MPU6050_RANGE_250_DEG: | |
Serial.println("+- 250 deg/s"); | |
break; | |
case MPU6050_RANGE_500_DEG: | |
Serial.println("+- 500 deg/s"); | |
break; | |
case MPU6050_RANGE_1000_DEG: | |
Serial.println("+- 1000 deg/s"); | |
break; | |
case MPU6050_RANGE_2000_DEG: | |
Serial.println("+- 2000 deg/s"); | |
break; | |
} | |
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); | |
Serial.print("Filter bandwidth set to: "); | |
switch (mpu.getFilterBandwidth()) | |
{ | |
case MPU6050_BAND_260_HZ: | |
Serial.println("260 Hz"); | |
break; | |
case MPU6050_BAND_184_HZ: | |
Serial.println("184 Hz"); | |
break; | |
case MPU6050_BAND_94_HZ: | |
Serial.println("94 Hz"); | |
break; | |
case MPU6050_BAND_44_HZ: | |
Serial.println("44 Hz"); | |
break; | |
case MPU6050_BAND_21_HZ: | |
Serial.println("21 Hz"); | |
break; | |
case MPU6050_BAND_10_HZ: | |
Serial.println("10 Hz"); | |
break; | |
case MPU6050_BAND_5_HZ: | |
Serial.println("5 Hz"); | |
break; | |
} | |
Serial.println(""); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) | |
{ // Ive changed the address //already chill | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;) | |
; // Don't proceed, loop forever | |
} | |
delay(100); | |
} | |
void loop() | |
{ | |
sensors_event_t a, g, temp; | |
mpu.getEvent(&a, &g, &temp); | |
// /* Print out the values */ | |
// Serial.print("Acceleration X: "); | |
// Serial.print(a.acceleration.x); | |
// Serial.print(", Y: "); | |
// Serial.print(a.acceleration.y); | |
// Serial.print(", Z: "); | |
// Serial.print(a.acceleration.z); | |
// Serial.println(" m/s^2"); | |
float m = sqrt((a.acceleration.x * a.acceleration.x) + (a.acceleration.y * a.acceleration.y) + (a.acceleration.z * a.acceleration.z)); | |
// Serial.println("M"); | |
// Serial.println(m); | |
float x = a.acceleration.x / m; | |
// Serial.println("X"); | |
// Serial.println(x); | |
float y = a.acceleration.y / m; | |
// Serial.println("Y"); | |
// Serial.println(y); | |
float z = a.acceleration.z / m; | |
// Serial.println("Z"); | |
// Serial.println(z); | |
// Serial.print("Rotation X: "); | |
// Serial.print(g.gyro.x); | |
// Serial.print(", Y: "); | |
// Serial.print(g.gyro.y); | |
// Serial.print(", Z: "); | |
// Serial.print(g.gyro.z); | |
// Serial.println(" rad/s"); | |
// Serial.print("Temperature: "); | |
// Serial.print(temp.temperature); | |
// Serial.println(" degC"); | |
String direction = "Unknown"; | |
if (z >= 0.9) | |
{ | |
direction = "Up"; | |
} | |
if (z <= -0.9) | |
{ | |
direction = "Down"; | |
} | |
if (y <= -0.9) | |
{ | |
direction = "Right"; | |
} | |
if (y >= 0.9) | |
{ | |
direction = "Left"; | |
} | |
if (x <= -0.9) | |
{ | |
direction = "Back"; | |
} | |
if (x >= 0.9) | |
{ | |
direction = "Front"; | |
} | |
float rotationRateThreshold = .2; | |
bool isRotating = magnitude(g.gyro) > rotationRateThreshold; | |
if (isRotating != true && lastDirection != direction && direction != "Unknown") | |
{ | |
display.clearDisplay(); | |
display.setTextSize(3); | |
display.setTextColor(WHITE); | |
display.setCursor(0, 0); | |
Serial.println(direction); | |
display.println(direction); | |
lastDirection = direction; | |
display.display(); | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needed plugins
pio lib install "Adafruit SSD1306"
pio lib install 6782