Advertisement
pleasedontcode

Sensor Display rev_01

Nov 2nd, 2024
82
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 Display
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-11-02 12:32:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 读取MPU6050信息,并做好信息好滤波,并打印到串口上。 */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <MPU6050.h>    // https://github.com/electroniccats/mpu6050
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF I2C PINS *****/
  32. const uint8_t wang_MPU6050_I2C_PIN_SDA_A4       = A4;
  33. const uint8_t wang_MPU6050_I2C_PIN_SCL_A5       = A5;
  34. const uint8_t wang_MPU6050_I2C_SLAVE_ADDRESS        = 104;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. // Create an instance of the MPU6050 class
  38. MPU6050 mpu; // Initialize the MPU6050 object
  39.  
  40. // Variables to hold accelerometer and gyroscope data
  41. int16_t ax, ay, az; // Accelerometer data
  42. int16_t gx, gy, gz; // Gyroscope data
  43.  
  44. // Variables for filtering
  45. const int filterSize = 5; // Size of the filter
  46. int16_t axFilter[filterSize] = {0}; // Filter array for accelerometer x
  47. int16_t ayFilter[filterSize] = {0}; // Filter array for accelerometer y
  48. int16_t azFilter[filterSize] = {0}; // Filter array for accelerometer z
  49. int16_t gxFilter[filterSize] = {0}; // Filter array for gyroscope x
  50. int16_t gyFilter[filterSize] = {0}; // Filter array for gyroscope y
  51. int16_t gzFilter[filterSize] = {0}; // Filter array for gyroscope z
  52. int filterIndex = 0; // Current index for the filter arrays
  53.  
  54. void setup(void)
  55. {
  56.     // Initialize I2C communication
  57.     Wire.begin();
  58.     Serial.begin(115200); // Start serial communication at 115200 baud rate
  59.  
  60.     Serial.println("Initializing I2C devices...");
  61.     mpu.initialize(); // Initialize the MPU6050
  62.  
  63.     // Check if the MPU6050 is connected
  64.     if (mpu.testConnection()) {
  65.         Serial.println("MPU6050 connection successful");
  66.     } else {
  67.         Serial.println("MPU6050 connection failed");
  68.     }
  69.  
  70.     pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as output
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Read accelerometer and gyroscope data
  76.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  77.  
  78.     // Update the filter arrays
  79.     axFilter[filterIndex] = ax;
  80.     ayFilter[filterIndex] = ay;
  81.     azFilter[filterIndex] = az;
  82.     gxFilter[filterIndex] = gx;
  83.     gyFilter[filterIndex] = gy;
  84.     gzFilter[filterIndex] = gz;
  85.  
  86.     // Calculate the average for each axis
  87.     int16_t axFiltered = 0, ayFiltered = 0, azFiltered = 0;
  88.     int16_t gxFiltered = 0, gyFiltered = 0, gzFiltered = 0;
  89.  
  90.     for (int i = 0; i < filterSize; i++) {
  91.         axFiltered += axFilter[i]; // Sum accelerometer x values
  92.         ayFiltered += ayFilter[i]; // Sum accelerometer y values
  93.         azFiltered += azFilter[i]; // Sum accelerometer z values
  94.         gxFiltered += gxFilter[i]; // Sum gyroscope x values
  95.         gyFiltered += gyFilter[i]; // Sum gyroscope y values
  96.         gzFiltered += gzFilter[i]; // Sum gyroscope z values
  97.     }
  98.  
  99.     axFiltered /= filterSize; // Average for accelerometer x
  100.     ayFiltered /= filterSize; // Average for accelerometer y
  101.     azFiltered /= filterSize; // Average for accelerometer z
  102.     gxFiltered /= filterSize; // Average for gyroscope x
  103.     gyFiltered /= filterSize; // Average for gyroscope y
  104.     gzFiltered /= filterSize; // Average for gyroscope z
  105.  
  106.     // Print the filtered accelerometer and gyroscope data to the serial monitor
  107.     Serial.print("Filtered Accel: ");
  108.     Serial.print(axFiltered); Serial.print(", ");
  109.     Serial.print(ayFiltered); Serial.print(", ");
  110.     Serial.print(azFiltered); Serial.print(" | ");
  111.     Serial.print("Filtered Gyro: ");
  112.     Serial.print(gxFiltered); Serial.print(", ");
  113.     Serial.print(gyFiltered); Serial.print(", ");
  114.     Serial.println(gzFiltered);
  115.  
  116.     // Blink the built-in LED to indicate activity
  117.     digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
  118.     delay(100); // Wait for 100 milliseconds
  119.     digitalWrite(LED_BUILTIN, LOW); // Turn the LED off
  120.     delay(100); // Wait for 100 milliseconds
  121.  
  122.     // Increment the filter index and wrap around if necessary
  123.     filterIndex = (filterIndex + 1) % filterSize; // Update filter index
  124. }
  125.  
  126. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement