Advertisement
pleasedontcode

Sensor Fusion rev_01

Aug 4th, 2024
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Sensor Fusion
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-04 21:12:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a system to read and process data from */
  21.     /* the MPU6050 sensor using I2C communication on pins */
  22.     /* SDA (D21) and SCL (D22), with interrupt handling */
  23.     /* on pin D4 for motion detection. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <MPU6050.h> // https://github.com/electroniccats/mpu6050
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void motionDetectionISR(); // Function prototype for the interrupt service routine
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t MPU6050_MPU6050_Interrupt_PIN_D4 = 4; // Interrupt pin for motion detection
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t MPU6050_MPU6050_I2C_PIN_SDA_D21 = 21; // SDA pin
  40. const uint8_t MPU6050_MPU6050_I2C_PIN_SCL_D22 = 22; // SCL pin
  41. const uint8_t MPU6050_MPU6050_I2C_SLAVE_ADDRESS = 104; // I2C address of MPU6050
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  44. // Create an instance of the MPU6050 class with the default I2C address
  45. MPU6050 mpu(MPU6050_MPU6050_I2C_SLAVE_ADDRESS);
  46.  
  47. // Variables to hold sensor data
  48. int16_t ax, ay, az; // Accelerometer data
  49. int16_t gx, gy, gz; // Gyroscope data
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize the interrupt pin for the MPU6050
  54.     pinMode(MPU6050_MPU6050_Interrupt_PIN_D4, INPUT);
  55.     attachInterrupt(digitalPinToInterrupt(MPU6050_MPU6050_Interrupt_PIN_D4), motionDetectionISR, RISING); // Attach interrupt
  56.  
  57.     // Initialize I2C communication
  58.     Wire.begin(MPU6050_MPU6050_I2C_PIN_SDA_D21, MPU6050_MPU6050_I2C_PIN_SCL_D22); // Set SDA and SCL pins
  59.     Wire.setClock(400000); // Set I2C frequency to 400kHz
  60.  
  61.     // Initialize serial communication
  62.     Serial.begin(115200);
  63.     while (!Serial); // Wait for serial port to connect
  64.  
  65.     // Initialize the MPU6050
  66.     Serial.println(F("Initializing I2C devices..."));
  67.     mpu.initialize();
  68.  
  69.     // Verify connection
  70.     Serial.println(F("Testing device connections..."));
  71.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  72. }
  73.  
  74. void loop(void)
  75. {
  76.     // Main code to run repeatedly
  77.     // Here you can add your logic to read data from the MPU6050
  78.     // For example, read accelerometer and gyroscope data
  79.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Read motion data
  80.  
  81.     // Print the data to the Serial Monitor
  82.     Serial.print("Accelerometer: ");
  83.     Serial.print("X: "); Serial.print(ax);
  84.     Serial.print(" Y: "); Serial.print(ay);
  85.     Serial.print(" Z: "); Serial.println(az);
  86.    
  87.     Serial.print("Gyroscope: ");
  88.     Serial.print("X: "); Serial.print(gx);
  89.     Serial.print(" Y: "); Serial.print(gy);
  90.     Serial.print(" Z: "); Serial.println(gz);
  91.    
  92.     delay(500); // Delay for readability
  93. }
  94.  
  95. // Interrupt Service Routine for motion detection
  96. void motionDetectionISR() {
  97.     // This function will be called when motion is detected
  98.     Serial.println("Motion detected!");
  99.     // You can add additional logic here to handle the motion event
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement