#include
"FS.h"
#include "SD.h"
#include "SPI.h"
#include<Wire.h>
const int MPU=0x68;//Device address
int16_t AccX, AccY, AccZ;// X, Y, and Z acceleration values
void setup(){
Wire.begin(); //To redefine the I2C pins:
Wire.begin(SDA,SCL) or Wire.begin(SDA, SCL, Bus_Speed).
// Wake up the sensor (reset)
Wire.beginTransmission(MPU);
Wire.write(0x6B);//Talk to the ACCEL_CONFIG register
(1C hex)
Wire.write(0x0);
Wire.endTransmission(true);
Serial.begin(115200);
if(!SD.begin()){
Serial.println("Card Mount
Failed");
return;
}
}
void loop(){
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card
attached");
return;
}
accRead();
appendFile(SD, "/AccX.txt", AccX);
appendFile_char(SD, "/AccX.txt", ",");
appendFile(SD, "/AccY.txt", AccY);
appendFile_char(SD, "/AccY.txt", ",");
appendFile(SD, "/AccZ.txt", AccZ);
appendFile_char(SD, "/AccZ.txt", ",");
}
void accRead(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);//Start with register 0x3B
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true);// Read 6 registers total
AccX=Wire.read()<<8 | Wire.read();
AccY=Wire.read()<<8 | Wire.read();
AccZ=Wire.read()<<8 | Wire.read();
Wire.endTransmission(true);
// Serial.print(AccX);
// Serial.print(" ");
// Serial.print(AccY);
// Serial.print(" ");
// Serial.println(AccZ);
}
void appendFile(fs::FS &fs, const char * path, int16_t message){ //
changed the argument type to int16_t
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to
open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message
appended");
} else {
Serial.println("Append
failed");
}
file.close();
}
void appendFile_char(fs::FS &fs, const char * path, const char *
message){ // added an 'appendFile_char' function to append ',' as data
separators
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to
open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message
appended");
} else {
Serial.println("Append
failed");
}
file.close();
}