Hardware:
Mega 2560 with MegaShield
Windows 7 ultimate (64 bit)
What I am trying to accomplish:
I am using the Mega for a general data logger. So far I am getting the analog sensors (not important, one is a infrared position sensor, the other is an ambient light sensor, last is anolog(2) grounded). I will be adding many more sensors to this setup once this proof of concept is completed.
Structure:
I am trying to use the millis() function to timestamp my data in the SD card. At the moment, I am happy with reading the sensors every 250mS and writing the data to the SD card. I tried to concatenate the millis() data to the left of the data, but could only get it to work by doing it on the right. Not sure on that but I am sure it is because of something I am doing wrong.
Main problems:
My issue is when I am running this while watching the serial monitor, the data will randomly stop. Once the data stops on serial, it stops on the SD card also. The data is different between them also. Here is an instance:
{This is from the Serial monitor}
Initializing SD card...card initialized.
62,881,0,414414
59,927,0,689689
60,886,27,951951
60,885,0,12141214
64,890,0,16861686
59,882,3,19491949
59,895,1,22122212
59,885,0,24752475
58,878,0,27382738
63,877,0,30013001
60,886,0,32643264
59,888,0,35273527
59,886,3,37893789
59,895,0,40524052
59,912,0,43164316
48,875,0,45784578
87,874,0,48414841
61,890,0,51045104
60,887,0,53675367
59,895,15,56305630
59,887,1,58935893
60,902,0,61566156
60,872,0,64196419
67,875,0,66826682
62,881,0,69446944
59,885,0,72077207
60,888,0,74717471
59,883,25,77347734
{This is from the SD card}
62,881,0,414
59,927,0,689
60,886,27,951
60,885,0,1214
64,890,0,1686
59,882,3,1949
59,895,1,2212
59,885,0,2475
58,878,0,2738
63,877,0,3001
60,886,0,3264
59,888,0,3527
59,886,3,3789
59,895,0,4052
59,912,0,4316
48,875,0,4578
87,874,0,4841
61,890,0,5104
60,887,0,5367
59,895,15,5630
59,887,1,5893
60,902,0,6156
60,872,0,6419
67,875,0,6682
62,881,0,6944
59,885,0,7207
60,888,0,7471
59,883,25,7734
As you can tell the timestamp data (on the far right) is correctly delimited with the coma but the data still freezes. Any help with this is appreciated!
Here is the code (Very green at this, please have mercy)
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 51
** MISO - pin 50
** CLK - pin 52
** CS - pin 53
created 24 Nov 2010
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
long currenttime = 0; // will store last time updated
//long interval = 250; // interval of data sampling output
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(53, OUTPUT);
// 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:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 4) {
dataString += ",";
}
}
unsigned long currenttime = millis();
delay(250);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
//if(currenttime - previoustime > interval) {
// save the last time you blinked the LED
// previoustime = currenttime;
//Serial.println(currenttime);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString += currenttime);
dataFile.close();
// print to the serial port too:
Serial.println(dataString += currenttime);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
//}
}