CCCPaste

Sensor Fusion rev_01

/********* 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: Sensor Fusion
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-08-04 21:12:17

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Implement a system to read and process data from */
	/* the MPU6050 sensor using I2C communication on pins */
	/* SDA (D21) and SCL (D22), with interrupt handling */
	/* on pin D4 for motion detection. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  // https://github.com/electroniccats/mpu6050

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void motionDetectionISR(); // Function prototype for the interrupt service routine

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t MPU6050_MPU6050_Interrupt_PIN_D4 = 4; // Interrupt pin for motion detection

/***** DEFINITION OF I2C PINS *****/
const uint8_t MPU6050_MPU6050_I2C_PIN_SDA_D21 = 21; // SDA pin
const uint8_t MPU6050_MPU6050_I2C_PIN_SCL_D22 = 22; // SCL pin
const uint8_t MPU6050_MPU6050_I2C_SLAVE_ADDRESS = 104; // I2C address of MPU6050

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Create an instance of the MPU6050 class with the default I2C address
MPU6050 mpu(MPU6050_MPU6050_I2C_SLAVE_ADDRESS);

// Variables to hold sensor data
int16_t ax, ay, az; // Accelerometer data
int16_t gx, gy, gz; // Gyroscope data

void setup(void)
{
    // Initialize the interrupt pin for the MPU6050
    pinMode(MPU6050_MPU6050_Interrupt_PIN_D4, INPUT);
    attachInterrupt(digitalPinToInterrupt(MPU6050_MPU6050_Interrupt_PIN_D4), motionDetectionISR, RISING); // Attach interrupt

    // Initialize I2C communication
    Wire.begin(MPU6050_MPU6050_I2C_PIN_SDA_D21, MPU6050_MPU6050_I2C_PIN_SCL_D22); // Set SDA and SCL pins
    Wire.setClock(400000); // Set I2C frequency to 400kHz

    // Initialize serial communication
    Serial.begin(115200);
    while (!Serial); // Wait for serial port to connect

    // Initialize the MPU6050
    Serial.println(F("Initializing I2C devices..."));
    mpu.initialize();

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

void loop(void)
{
    // Main code to run repeatedly
    // Here you can add your logic to read data from the MPU6050
    // For example, read accelerometer and gyroscope data
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Read motion data

    // Print the data to the Serial Monitor
    Serial.print("Accelerometer: ");
    Serial.print("X: "); Serial.print(ax);
    Serial.print(" Y: "); Serial.print(ay);
    Serial.print(" Z: "); Serial.println(az);
    
    Serial.print("Gyroscope: ");
    Serial.print("X: "); Serial.print(gx);
    Serial.print(" Y: "); Serial.print(gy);
    Serial.print(" Z: "); Serial.println(gz);
    
    delay(500); // Delay for readability
}

// Interrupt Service Routine for motion detection
void motionDetectionISR() {
    // This function will be called when motion is detected
    Serial.println("Motion detected!");
    // You can add additional logic here to handle the motion event
}

/* END CODE */