我在这里不知所措,如果可以提供,我真的想要一些帮助。我在网站上搜索过类似的问题,但我发现任何类似的问题都不足以完成我的查询。我一直在尝试利用sdfatlib库来提高文件的写入速度。我已经尝试修改模拟记录示例以满足我的需要,但在将它们放入文件之前我不得不加倍修改值,因为有一个更紧迫的问题。 SD卡无法从Arduino接收值。我有什么简单的遗失吗?可能是,我已经做了太长时间了。
进一步缩小范围,
我的输出看起来像这样:
FreeRam: 7056
Type any character to start
Logging to: LOGGER00.BIN
Type any character to stop
millis,sens0,sens1,sens2
1930,387,318,314
Done!
仅将行(millis,sens0,sens1,sens2
)保存到文件中。
如果这有助于破译问题。这是我的代码:
#include <SdFat.h>
#include <SdFatUtil.h> // define FreeRam()
#define SD_CHIP_SELECT SS // SD chip select pin
#define USE_DS1307 0 // set nonzero to use DS1307 RTC
#define LOG_INTERVAL 10 // mills between entries
#define SENSOR_COUNT 3 // number of analog pins to log
#define ECHO_TO_SERIAL 1 // echo data to serial port if nonzero
#define WAIT_TO_START 1 // Wait for serial input in setup()
#define ADC_DELAY 10 // ADC delay for high impedence sensors
// file system object
SdFat sd;
// text file for logging
ofstream logfile;
// Serial print stream
ArduinoOutStream cout(Serial);
// buffer to format data - makes it eaiser to echo to Serial
char buf[80];
//------------------------------------------------------------------------------
#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif // SENSOR_COUNT
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
#if USE_DS1307
// use RTClib from Adafruit
// https://github.com/adafruit/RTClib
// The Arduino IDE has a bug that causes Wire and RTClib to be loaded even
// if USE_DS1307 is false.
#error remove this line and uncomment the next two lines.
//#include <Wire.h>
//#include <RTClib.h>
RTC_DS1307 RTC; // define the Real Time Clock object
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
os << ':' << setw(2) << int(dt.second()) << setfill(' ');
return os;
}
#endif // USE_DS1307
//------------------------------------------------------------------------------
const int powerpin =19;
const int groundpin =18;
const int X = A0;
const int Y = A1;
const int Z = A2;
float xMin = 512;
float xMax = 512;
float yMin = 512;
float yMax = 512;
float zMin = 512;
float zMax = 512;
float sensorVals[]={0,0,0};
void setup() {
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
analogWrite(groundpin, LOW);
analogWrite(powerpin, HIGH);
while (!Serial){} // wait for Leonardo
// pstr stores strings in flash to save RAM
cout << endl << pstr("FreeRam: ") << FreeRam() << endl;
#if WAIT_TO_START
cout << pstr("Type any character to start\n");
while (Serial.read() <= 0) {}
delay(400); // catch Due reset problem
#endif // WAIT_TO_START
#if USE_DS1307
// connect to RTC
Wire.begin();
if (!RTC.begin()) error("RTC failed");
// set date time callback function
SdFile::dateTimeCallback(dateTime);
DateTime now = RTC.now();
cout << now << endl;
#endif // USE_DS1307
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();
// create a new file in root, the current working directory
char name[] = "LOGGER00.BIN";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) continue;
logfile.open(name);
break;
}
if (!logfile.is_open()) error("file.open");
cout << pstr("Logging to: ") << name << endl;
cout << pstr("Type any character to stop\n\n");
// format header in buffer
obufstream bout(buf, sizeof(buf));
bout << pstr("millis");
#if USE_DS1307
bout << pstr(",date,time");
#endif // USE_DS1307
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
bout << pstr(",sens") << int(i);
}
logfile << buf << endl;
#if ECHO_TO_SERIAL
cout << buf << endl;
#endif // ECHO_TO_SERIAL
}
//------------------------------------------------------------------------------
void loop() {
uint32_t m;
// wait for time to be a multiple of interval
do {
m = millis();
} while (m % LOG_INTERVAL);
// use buffer stream to format line
obufstream bout(buf, sizeof(buf));
// start with time in millis
bout << m;
#if USE_DS1307
DateTime now = RTC.now();
bout << ',' << now;
#endif // USE_DS1307
// read analog pins and format data
for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
#if ADC_DELAY
/*int sensorx = analogRead(A0);
if (sensorx < xMin)
xMin = sensorx;
if (sensorx > xMax)
xMax = sensorx;
long xscaled = map(sensorx, xMin, xMax, -1000, 1000);
float xfinal=((xscaled*3.0)/1000.0);
int sensory = analogRead(A1);
if (sensory < yMin)
yMin = sensory;
if (sensorx > yMax)
yMax = sensory;
long yscaled = map(sensory, yMin, yMax, -1000, 1000);
float yfinal=((yscaled*3.0)/1000.0);
int sensorz = analogRead(A2);
if (sensorz < zMin)
zMin = sensorz;
if (sensorz > zMax)
zMax = sensorz;
long zscaled = map(sensorz, zMin, zMax, -1000, 1000);
float zfinal=((zscaled*3.0)/1000.0);*/
analogRead(ia);
delay(ADC_DELAY);
#endif // ADC_DELAY
bout << ',' << analogRead(ia);
}
bout << endl;
// check for error
if (!logfile) error("write data failed");
#if ECHO_TO_SERIAL
cout << buf;
#endif // ECHO_TO_SERIAL
// don't log two points in the same millis
if (m == millis()) delay(1);
if (!Serial.available()) return;
logfile.close();
cout << pstr("Done!");
while (1);
}