Close

2024-03-21 - Success! Programming the gyroscope 2.0

A project log for M8B 2.0

Magic 8 Ball + User Guide 2.0

nananisNananis 03/21/2024 at 09:270 Comments

Today we finally managed to correct the code and ensure that when we shake the gyroscope, text appears on the screen:

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal.h>

Adafruit_MPU6050 mpu;

const int rs = 15;
const int en = 13;
const int d4 = 32;
const int d5 = 33;
const int d6 = 25;
const int d7 = 26;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  Serial.begin(115200);
  while (!Serial)
    delay(10); 

  Serial.println("Adafruit MPU6050 test!");

  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;
  }

  lcd.begin(8, 2);
  Serial.begin(115200);
  lcd.clear();
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  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");

  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");

  if (abs(g.gyro.x) > 1 || abs(g.gyro.y) > 1 || abs(g.gyro.z) > 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(getRandomMessage());
    lcd.setCursor(0, 1);
    lcd.print("UwU");
    delay(3000);
    lcd.clear();
  }

 The next step is reducing the sensitivity of the gyroscope. We don't want to text to appear as soon as the person picks up the ball.

Here is the new code:

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

const int rs = 15;
const int en = 13;
const int d4 = 32;
const int d5 = 33;
const int d6 = 25;
const int d7 = 26;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Adafruit_MPU6050 mpu;

void setup() {
  lcd.begin(8, 2);
  Serial.begin(115200);
  while (!Serial) {
    delay(10); // Wait for Serial Monitor to open
  }

  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  float accelerationMagnitude = sqrt(pow(a.acceleration.x, 2) + pow(a.acceleration.y, 2) + pow(a.acceleration.z, 2));
  
  // Adjust this threshold value to change the sensitivity
  float threshold = 60.0;

  if (accelerationMagnitude > threshold) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(getRandomMessage());
    lcd.setCursor(0, 1);
    lcd.print("UwU");
    delay(3000);
    lcd.clear();
  }
}

String getRandomMessage() {
  String messages[] = {
    "Go ahead",
    "Fuck it.",
    "WTF?!",
    "OOPS",
    "Possibly",
    "Fate.",
    "Nauwr",
    "Shut up",
    "RIP",
    "Nope"
  };

  int index = random(10);
  return messages[index];
}

Discussions