I am using an Arduino Uno, a Datalogger Shield and an IMU LSM9DS1. I am able to print things to the SD card, but whenever I run the code, the data is just repeated over and over again without updating with new values. Attached is my code and serial output. Any ideas how to fix this? Thank you!
#include <Arduino_LSM9DS1.h>
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
float x, y, z;
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
if (IMU.accelerationAvailable() && IMU.gyroscopeSampleRate()) {
// Read acceleration data
IMU.readAcceleration(x, y, z);
dataString += String(x) + ",";
dataString += String(y) + ",";
dataString += String(z) + ",";
// Read gyroscope data
IMU.readGyroscope(x, y, z);
dataString += String(x) + ",";
dataString += String(y) + ",";
dataString += String(z);
// Open the file
File plzWork = SD.open("data.txt", FILE_WRITE);
// If the file is available, write to it
if (plzWork) {
plzWork.println(dataString);
plzWork.close();
// print to the serial port too:
Serial.println(dataString);
} else {
Serial.println("error opening datalog.txt");
}
}
// Add a delay if needed to control the sampling rate
delay(1000); // Adjust the delay based on your desired sampling rate
}
