Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 Logging
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-08-17 00:22:59
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Strat and log data intonsd card */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <SPI.h>
- #include <MPU6050.h> // Include the MPU6050 library
- #include <SdFat.h> // Include the SdFat library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t Accmano_MPU6050_I2C_PIN_SDA_A4 = A4; // SDA pin for I2C
- const uint8_t Accmano_MPU6050_I2C_PIN_SCL_A5 = A5; // SCL pin for I2C
- const uint8_t Accmano_MPU6050_I2C_SLAVE_ADDRESS = 104; // I2C address for MPU6050
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t Sd_SDCardModule_SPI_PIN_MOSI_D11 = 11; // MOSI pin for SD card
- const uint8_t Sd_SDCardModule_SPI_PIN_MISO_D12 = 12; // MISO pin for SD card
- const uint8_t Sd_SDCardModule_SPI_PIN_SCLK_D13 = 13; // SCLK pin for SD card
- const uint8_t Sd_SDCardModule_SPI_PIN_CS_D10 = 10; // CS pin for SD card
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- MPU6050 mpu; // Create an instance of the MPU6050 class
- SdFs sd; // Create an instance of the SdFs class (corrected from SdFat to SdFs)
- FsFile file; // Create an instance of the FsFile class to handle file operations
- void setup(void)
- {
- // Initialize Serial communication for debugging
- Serial.begin(115200);
- while (!Serial) {
- ; // Wait for serial port to connect
- }
- // Initialize I2C communication for MPU6050
- Wire.begin();
- mpu.initialize(); // Initialize the MPU6050
- // Check if the MPU6050 is connected
- if (mpu.testConnection()) {
- Serial.println("MPU6050 connection successful");
- } else {
- Serial.println("MPU6050 connection failed");
- }
- // Initialize SPI for SD card
- pinMode(Sd_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
- SPI.begin();
- // Initialize SD card
- if (!sd.begin(Sd_SDCardModule_SPI_PIN_CS_D10)) { // Use sd object for initialization
- Serial.println("SD card initialization failed!");
- return; // Stop if SD card initialization fails
- }
- Serial.println("SD card initialized successfully");
- // Open a file for writing
- if (!file.open("data.txt", FILE_WRITE)) {
- Serial.println("Failed to open file for writing");
- return; // Stop if file opening fails
- }
- Serial.println("File opened successfully");
- }
- void loop(void)
- {
- // Read data from the MPU6050
- Vector rawAccel = mpu.getAcceleration();
- Vector rawGyro = mpu.getRotation();
- // Log data to the serial monitor
- Serial.print("Accel: ");
- Serial.print(rawAccel.XAxis);
- Serial.print(", ");
- Serial.print(rawAccel.YAxis);
- Serial.print(", ");
- Serial.print(rawAccel.ZAxis);
- Serial.print(" | Gyro: ");
- Serial.print(rawGyro.XAxis);
- Serial.print(", ");
- Serial.print(rawGyro.YAxis);
- Serial.print(", ");
- Serial.println(rawGyro.ZAxis);
- // Write data to the SD card
- file.print("Accel: ");
- file.print(rawAccel.XAxis);
- file.print(", ");
- file.print(rawAccel.YAxis);
- file.print(", ");
- file.print(rawAccel.ZAxis);
- file.print(" | Gyro: ");
- file.print(rawGyro.XAxis);
- file.print(", ");
- file.print(rawGyro.YAxis);
- file.print(", ");
- file.println(rawGyro.ZAxis);
- // Flush the file buffer to ensure data is written
- file.flush();
- // Delay for a while before the next reading
- delay(1000); // Adjust the delay as needed
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement