For my first project with my Arduino, I'm playing around setting up this shield: CAN-BUS Shield - DEV-13262 - SparkFun Electronics with an Uno. My final goal is to make a CAN/GPS/IMU datalogger for my car. To start out I'm just trying to get SD writing working and understand how to use the library.
I loaded the "Datalogger" example from the SD library, and modified the code in 2 places to use the correct SPI CS port for the shield I have. This program writes analog inputs to the SD card. I have a 5 way joystick on A1-A5, so I modified the "for" loop to read those pins. Also I added a line in setup to delete the file that will be written to, in case it already exists. Here's the code after those modifications:
/*
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 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
// 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 = 9;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
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(9, 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.");
SD.remove("datalog.txt");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 1; analogPin < 6; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 5) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
What I expect it to do is to print "Initializing SD card...card initialized.", then every cycle print a line of 5 comma separated values. What it actually does is:
Initializing SD card...card initialized.
507,446,472,425,408
Then it hangs and doesn't print the next line. I inserted some serial prints inside the for to see where it was hanging up, and found that on the second time through the void loop, it is hanging somewhere around the point where it reads the 3rd value and tries to write it to dataString. If I print dataString within the for loop, on the 3rd iteration it prints "447,433, " for example, having blank values for the 3rd column. Then it never makes it to the SD write part of the loop since it has hung in the for loop.
See next post...