Advertisement
pleasedontcode

Sensor Logging rev_01

Aug 16th, 2024
876
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 Logging
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-17 00:22:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Strat and log data intonsd card */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <SPI.h>
  26. #include <MPU6050.h>    // Include the MPU6050 library
  27. #include <SdFat.h>      // Include the SdFat library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF I2C PINS *****/
  34. const uint8_t Accmano_MPU6050_I2C_PIN_SDA_A4        = A4; // SDA pin for I2C
  35. const uint8_t Accmano_MPU6050_I2C_PIN_SCL_A5        = A5; // SCL pin for I2C
  36. const uint8_t Accmano_MPU6050_I2C_SLAVE_ADDRESS      = 104; // I2C address for MPU6050
  37.  
  38. /***** DEFINITION OF SPI PINS *****/
  39. const uint8_t Sd_SDCardModule_SPI_PIN_MOSI_D11       = 11; // MOSI pin for SD card
  40. const uint8_t Sd_SDCardModule_SPI_PIN_MISO_D12       = 12; // MISO pin for SD card
  41. const uint8_t Sd_SDCardModule_SPI_PIN_SCLK_D13       = 13; // SCLK pin for SD card
  42. const uint8_t Sd_SDCardModule_SPI_PIN_CS_D10         = 10; // CS pin for SD card
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  45. MPU6050 mpu; // Create an instance of the MPU6050 class
  46. SdFs sd;     // Create an instance of the SdFs class (corrected from SdFat to SdFs)
  47. FsFile file; // Create an instance of the FsFile class to handle file operations
  48.  
  49. void setup(void)
  50. {
  51.     // Initialize Serial communication for debugging
  52.     Serial.begin(115200);
  53.     while (!Serial) {
  54.         ; // Wait for serial port to connect
  55.     }
  56.  
  57.     // Initialize I2C communication for MPU6050
  58.     Wire.begin();
  59.     mpu.initialize(); // Initialize the MPU6050
  60.  
  61.     // Check if the MPU6050 is connected
  62.     if (mpu.testConnection()) {
  63.         Serial.println("MPU6050 connection successful");
  64.     } else {
  65.         Serial.println("MPU6050 connection failed");
  66.     }
  67.  
  68.     // Initialize SPI for SD card
  69.     pinMode(Sd_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
  70.     SPI.begin();
  71.  
  72.     // Initialize SD card
  73.     if (!sd.begin(Sd_SDCardModule_SPI_PIN_CS_D10)) { // Use sd object for initialization
  74.         Serial.println("SD card initialization failed!");
  75.         return; // Stop if SD card initialization fails
  76.     }
  77.     Serial.println("SD card initialized successfully");
  78.  
  79.     // Open a file for writing
  80.     if (!file.open("data.txt", FILE_WRITE)) {
  81.         Serial.println("Failed to open file for writing");
  82.         return; // Stop if file opening fails
  83.     }
  84.     Serial.println("File opened successfully");
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // Read data from the MPU6050
  90.     Vector rawAccel = mpu.getAcceleration();
  91.     Vector rawGyro = mpu.getRotation();
  92.  
  93.     // Log data to the serial monitor
  94.     Serial.print("Accel: ");
  95.     Serial.print(rawAccel.XAxis);
  96.     Serial.print(", ");
  97.     Serial.print(rawAccel.YAxis);
  98.     Serial.print(", ");
  99.     Serial.print(rawAccel.ZAxis);
  100.     Serial.print(" | Gyro: ");
  101.     Serial.print(rawGyro.XAxis);
  102.     Serial.print(", ");
  103.     Serial.print(rawGyro.YAxis);
  104.     Serial.print(", ");
  105.     Serial.println(rawGyro.ZAxis);
  106.  
  107.     // Write data to the SD card
  108.     file.print("Accel: ");
  109.     file.print(rawAccel.XAxis);
  110.     file.print(", ");
  111.     file.print(rawAccel.YAxis);
  112.     file.print(", ");
  113.     file.print(rawAccel.ZAxis);
  114.     file.print(" | Gyro: ");
  115.     file.print(rawGyro.XAxis);
  116.     file.print(", ");
  117.     file.print(rawGyro.YAxis);
  118.     file.print(", ");
  119.     file.println(rawGyro.ZAxis);
  120.    
  121.     // Flush the file buffer to ensure data is written
  122.     file.flush();
  123.  
  124.     // Delay for a while before the next reading
  125.     delay(1000); // Adjust the delay as needed
  126. }
  127.  
  128. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement