#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
#include <SD.h>
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
bool enableSerialLogging = true;
bool sdCardPresent = false; // Keeps track of if SD card is plugged in 跟踪SD卡是否已插入
String logFileName; // Active logging file 活动日志文件
String logFileBuffer; // Buffer for logged data. Max is set in config 记录数据的缓冲区。
static int x,y,z;
static unsigned long RetryMillis = 0;
static File logFile;
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
const int chipSelect = 4;
void setup(){
if ( initSD() )
{
sdCardPresent = true;
// Get the next, available log file name 获取下一个可用的日志文件名
logFileName = nextLogFile();
}
Serial.begin(115200); // Start the serial terminal
adxl.powerOn();
adxl.setRangeSetting(2); // Give the range settings ccepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range Lower Values = Greater Sensitivity
adxl.setSpiBit(0); // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
adxl.setRate(3200);
adxl.set_bw(1600);
pinMode(4,OUTPUT);
SD.begin();
}
/****************** MAIN CODE ******************/
/* Accelerometer Readings and Interrupt */
void loop()
{
adxl.readAccel(&x, &y, &z);
logIMUData(); // Log new data
}
void logIMUData(void)
{
String imuLog = ""; // Create a fresh line to log 创建一条新行来记录
/////////////////////////////////////////////////////////////////////////////////
// imuLog += String(imu.time) + ", "; // Add time to log string //添加时间到日志字符串
{
// imuLog += String(RetryMillis) +" ";
imuLog += String(x) + " ";
imuLog += String(y) + " ";
// imuLog += String(z) + " ";
}
imuLog += "\r\n"; // Add a new line //添加新行
Serial.print(imuLog);
// If adding this log line will put us over the buffer length:
if (imuLog.length() + logFileBuffer.length() >=460)
{
sdLogString(logFileBuffer); // Log SD buffer 记录SD缓冲区
logFileBuffer=" "; // Clear SD log buffer 清除SD日志缓冲区
}
// Add new line to SD log buffer //将新行添加到SD日志缓冲区
logFileBuffer += imuLog;
}
bool initSD(void)
{
// SD.begin should return true if a valid SD card is present //如果存在有效的SD卡,则SD.begin应返回true
if ( !SD.begin() )
{
return false;
}
return true;
}
// Log a string to the SD card //将一个字符串记录到SD卡
bool sdLogString(String toLog)
{// File logFile = SD.open(logFileName, FILE_WRITE);
if (logFile)
{
// Open the current file name: //打开当前文件名:
// If the file will get too big with this new string, create a new one, and open it.
//如果该文件将变得太大,使用此新字符串,请创建一个新的字符串,并将其打开。
if (logFile.size() > (500000 - toLog.length()))
{
//logFile.close();
logFileName = nextLogFile();
logFile = SD.open(logFileName, FILE_WRITE);
}
}
else
{
// Open the current file name: //打开当前文件名:
logFile = SD.open(logFileName, FILE_WRITE);
}
// If the log file opened properly, add the string to it. //如果日志文件打开正确,请添加字符串。
if (logFile)
{
RetryMillis = millis();
logFile.print( RetryMillis);
logFile.print( toLog);
logFile.close();
return true; // Return success
}
return false; // Return fail
}
// Find the next available log file. Or return a null string
// if we've reached the maximum file limit.
//找到下一个可用的日志文件。 或返回一个空字符串
//如果我们达到了最大文件限制。
String nextLogFile(void)
{
String filename;
int logIndex = 0;
for (int i = 0; i < 999999; i++)
{
// Construct a file with PREFIX[Index].SUFFIX //使用PREFIX [Index] .SUFFIX构造一个文件
filename = String("DATA");
filename += String(logIndex);
filename += ".";
filename += String("TXT");
// If the file name doesn't exist, return it 如果文件名不存在,请返回
if (!SD.exists(filename))
{
return filename;
}
// Otherwise increment the index, and try again //否则增加索引,然后重试
logIndex++;
}
return "";
}
#include <SD.h>
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
bool enableSerialLogging = true;
bool sdCardPresent = false; // Keeps track of if SD card is plugged in 跟踪SD卡是否已插入
String logFileName; // Active logging file 活动日志文件
String logFileBuffer; // Buffer for logged data. Max is set in config 记录数据的缓冲区。
static int x,y,z;
static unsigned long RetryMillis = 0;
static File logFile;
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
const int chipSelect = 4;
void setup(){
if ( initSD() )
{
sdCardPresent = true;
// Get the next, available log file name 获取下一个可用的日志文件名
logFileName = nextLogFile();
}
Serial.begin(115200); // Start the serial terminal
adxl.powerOn();
adxl.setRangeSetting(2); // Give the range settings ccepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range Lower Values = Greater Sensitivity
adxl.setSpiBit(0); // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
adxl.setRate(3200);
adxl.set_bw(1600);
pinMode(4,OUTPUT);
SD.begin();
}
/****************** MAIN CODE ******************/
/* Accelerometer Readings and Interrupt */
void loop()
{
adxl.readAccel(&x, &y, &z);
logIMUData(); // Log new data
}
void logIMUData(void)
{
String imuLog = ""; // Create a fresh line to log 创建一条新行来记录
/////////////////////////////////////////////////////////////////////////////////
// imuLog += String(imu.time) + ", "; // Add time to log string //添加时间到日志字符串
{
// imuLog += String(RetryMillis) +" ";
imuLog += String(x) + " ";
imuLog += String(y) + " ";
// imuLog += String(z) + " ";
}
imuLog += "\r\n"; // Add a new line //添加新行
Serial.print(imuLog);
// If adding this log line will put us over the buffer length:
if (imuLog.length() + logFileBuffer.length() >=460)
{
sdLogString(logFileBuffer); // Log SD buffer 记录SD缓冲区
logFileBuffer=" "; // Clear SD log buffer 清除SD日志缓冲区
}
// Add new line to SD log buffer //将新行添加到SD日志缓冲区
logFileBuffer += imuLog;
}
bool initSD(void)
{
// SD.begin should return true if a valid SD card is present //如果存在有效的SD卡,则SD.begin应返回true
if ( !SD.begin() )
{
return false;
}
return true;
}
// Log a string to the SD card //将一个字符串记录到SD卡
bool sdLogString(String toLog)
{// File logFile = SD.open(logFileName, FILE_WRITE);
if (logFile)
{
// Open the current file name: //打开当前文件名:
// If the file will get too big with this new string, create a new one, and open it.
//如果该文件将变得太大,使用此新字符串,请创建一个新的字符串,并将其打开。
if (logFile.size() > (500000 - toLog.length()))
{
//logFile.close();
logFileName = nextLogFile();
logFile = SD.open(logFileName, FILE_WRITE);
}
}
else
{
// Open the current file name: //打开当前文件名:
logFile = SD.open(logFileName, FILE_WRITE);
}
// If the log file opened properly, add the string to it. //如果日志文件打开正确,请添加字符串。
if (logFile)
{
RetryMillis = millis();
logFile.print( RetryMillis);
logFile.print( toLog);
logFile.close();
return true; // Return success
}
return false; // Return fail
}
// Find the next available log file. Or return a null string
// if we've reached the maximum file limit.
//找到下一个可用的日志文件。 或返回一个空字符串
//如果我们达到了最大文件限制。
String nextLogFile(void)
{
String filename;
int logIndex = 0;
for (int i = 0; i < 999999; i++)
{
// Construct a file with PREFIX[Index].SUFFIX //使用PREFIX [Index] .SUFFIX构造一个文件
filename = String("DATA");
filename += String(logIndex);
filename += ".";
filename += String("TXT");
// If the file name doesn't exist, return it 如果文件名不存在,请返回
if (!SD.exists(filename))
{
return filename;
}
// Otherwise increment the index, and try again //否则增加索引,然后重试
logIndex++;
}
return "";
}