CCCPaste

"MPU6050 Bluetooth" rev_04

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "MPU6050 Bluetooth"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-09 21:24:02

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Design a Bluetooth head tracker for OpenTrack with */
	/* MPU6050 and HC-06 modules. Provide thorough code */
	/* documentation and comments detailing each code */
	/* segment's purpose and the hardware connections */
	/* required. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Develop a Bluetooth head tracker for OpenTrack */
	/* with MPU6050 and HC-06 modules. Include */
	/* comprehensive code documentation, comments on */
	/* hardware connections, and utilize an LED for */
	/* status updates. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include  //https://github.com/jrowberg/i2cdevlib

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void dmpDataReady(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t accelerometer_MPU6050_Interrupt_PIN_D2 = 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t statusLED_LED_PIN_D3 = 3;

/***** DEFINITION OF Software Serial *****/
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2 = A2;
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3 = A3;
SoftwareSerial bluetooth_HC05_mySerial(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);
SoftwareSerial bluetooth_HC05_mySerial_2(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2);

/***** DEFINITION OF I2C PINS *****/
const uint8_t accelerometer_MPU6050_I2C_PIN_SDA_A4 = A4;
const uint8_t accelerometer_MPU6050_I2C_PIN_SCL_A5 = A5;
const uint8_t accelerometer_MPU6050_I2C_SLAVE_ADDRESS = 104;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool statusLED_LED_PIN_D3_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float statusLED_LED_PIN_D3_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MPU6050 mpu;

/****** GLOBAL VARIABLES *****/
bool blinkState = false;
bool dmpReady = false;
uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
uint16_t packetSize, fifoCount;
Quaternion q;
VectorFloat gravity;
float ypr[3];

volatile bool mpuInterrupt = false;
void dmpDataReady() { mpuInterrupt = true; }

void setup(void)
{
    // Initialize digital pins
    pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
    pinMode(statusLED_LED_PIN_D3, OUTPUT);

    // Initialize Bluetooth serial communication
    bluetooth_HC05_mySerial.begin(9600);
    bluetooth_HC05_mySerial_2.begin(9600);

    // Initialize I2C communication
    Wire.begin();
    Wire.setClock(400000);

    // Initialize Serial communication for debugging
    Serial.begin(115200);
    while (!Serial);

    // Initialize MPU6050
    Serial.println(F("Initializing I2C devices..."));
    mpu.initialize();
    pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);

    // Verify connection to MPU6050
    Serial.println(F("Testing device connections..."));
    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

    // Wait for user input to begin DMP programming
    Serial.println(F("\nSend any character to begin DMP programming and demo: "));
    while (!Serial.available());
    while (Serial.available() && Serial.read());

    // Initialize DMP
    Serial.println(F("Initializing DMP..."));
    devStatus = mpu.dmpInitialize();

    // Set offsets for MPU6050
    mpu.setXGyroOffset(220);
    mpu.setYGyroOffset(76);
    mpu.setZGyroOffset(-85);
    mpu.setZAccelOffset(1788);

    // Check if DMP initialization was successful
    if (devStatus == 0) {
        mpu.CalibrateAccel(6);
        mpu.CalibrateGyro(6);
        mpu.PrintActiveOffsets();
        Serial.println(F("Enabling DMP..."));
        mpu.setDMPEnabled(true);

        // Enable interrupt detection
        Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
        Serial.print(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2));
        Serial.println(F(")..."));
        attachInterrupt(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();

        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
    } else {
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus);
        Serial.println(F(")"));
    }
}

void loop(void)
{
    // Main code to run repeatedly
    if (!dmpReady) return;

    // Check if we have a new DMP packet
    if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
        mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
        
        // Send yaw, pitch, and roll data over Serial
        Serial.print("ypr\t");
        Serial.print(ypr[0] * 180/M_PI);
        Serial.print("\t");
        Serial.print(ypr[1] * 180/M_PI);
        Serial.print("\t");
        Serial.println(ypr[2] * 180/M_PI);

        // Send yaw, pitch, and roll data over Bluetooth
        bluetooth_HC05_mySerial.print("ypr\t");
        bluetooth_HC05_mySerial.print(ypr[0] * 180/M_PI);
        bluetooth_HC05_mySerial.print("\t");
        bluetooth_HC05_mySerial.print(ypr[1] * 180/M_PI);
        bluetooth_HC05_mySerial.print("\t");
        bluetooth_HC05_mySerial.println(ypr[2] * 180/M_PI);

        // Toggle LED state
        blinkState = !blinkState;
        digitalWrite(statusLED_LED_PIN_D3, blinkState);
    }

    updateOutputs(); // Refresh output data
}

void updateOutputs()
{
    // Update the status LED based on raw data
    digitalWrite(statusLED_LED_PIN_D3, statusLED_LED_PIN_D3_rawData);
}

/* END CODE */