Some VERY!!! basic questions on SD card storage

Hi
Trying to add sd card storage to a project. Cant find answers to some very basic questions(probably cos they are too simple to mention), PLEASE HELP!!! But be warned there may be plenty more stupidly simple questions to follow.
Thanks

i have a sd shield(full 32pin not 4 pin) plugged into my arduino. I have been running the sample projects but getting nowhere. If they are plugged together is this enough or do i need to do anything else with other pins?

Do i need to create the datalog.txt file or does the programme do this(neither have worked for me.

Sorry for the very basic questions but im a complete newb

/*
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 = 4;

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(10, 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 < 2) {
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”);
}
}

i have a sd shield

Which one?

I have been running the sample projects but getting nowhere.

No serial output at all?

Do i need to create the datalog.txt file or does the programme do this(neither have worked for me.

The program should create the file, if it attempts to open it correctly.

  // make a string for assembling the data to log:
  String dataString = "";

That is NOT what that code does, and is completely unnecessary. File writes are buffered. It doesn't matter a bit if there is one write with 100 characters in a String, or 100 write()s with one character each. The data will be written to the buffer, until the buffer is full. When it is, the buffer will be flushed to the file, and emptied.

Looking casually at the code, it seems you are trying to run before you can walk. For starters, if you are only going to use one file, checking that it is there every time round the loop is wasting space. Nominating the file is usually done at the start

char filename[] = "Datalog.CSV";
File myFile;

void setup() {
  Serial.begin(9600);
what you've done looks OK
  }

void loop() {
get one variable;
get the other variable;

        myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<< OPEN
  myFile.print(One variable);
  myFile.print(",");
  myFile.print(Other variable);
  myFile.println();
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE  
}

It should not be any more complicated than that.

It would be better if you enclose your code between the hash icon. Instructions are in the sticky at the start of the forum -where it is easy for a newbie to miss. A lot of comment, particularly all that stuff at the start, can be safely omitted.

Thanks for the replies lads.

The sd card shield i have is a stackable large SD card shield. Looks exactly the same as the one on the Arduino site but it is from eBay and is Chinese. The brand on it is derek-robot data logging shield v1.0 Sorry for the basic errors with the last code. This was one of the Arduino examples so i presumed this would be a good place to start.

Instead of general question it may be better to state exactly what i am looking to do. in the code below i am using an ultrasonic sensor to produce a "% of liquid remaining" reading from a tank. I am producing a variable called "percent" which i need to send to an excel file to produce a graph of usage. The code for the variable is below.

Any help you guys could give with writing the "percent" variable to a CSV file would be great as i have been pulling my hair out for couple weeks now trying to implement it. By the way i really need to write the current reading to row 1 so most current is at the top and the preceding reading are consecutively below.

Thanks is advance for any help you can give

//* VCC connection of the sensor attached to +5V
//* GND connection of the sensor attached to ground
//* TRIG connection of the sensor attached to digital pin 4
//* ECHO connection of the sensor attached to digital pin 5


const int trigPin = 4;
const int echoPin = 5;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm, percent;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);


digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
percent = microsecondsTopercent(duration);
Serial.print("Distance from sensor to oil = ");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm, ");
Serial.println();
Serial.print(percent);
Serial.print("% oil capacity remaining");
Serial.println();
Serial.println();
delay(3000);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

long microsecondsTopercent(long microseconds)
{
return  (420-(microseconds / 29 / 2))/4 ; 
// Tank has been defined a 4m deep. This will now give a % reading
// based on the distance from bottom of tank to oil height
//20cm has been allowed between sensor and full oil level
}

Instead of general question it may be better to state exactly what i am looking to do.

Definitely. It's also better to post a link to the hardware, even if it's t a next-to-useless ebay listing.

Any help you guys could give with writing the "percent" variable to a CSV file would be great as i have been pulling my hair out for couple weeks now trying to implement it.

I don't see anything in that code that even acknowledges that there is a SD card/shield attached to the Arduino. I don't see anything that makes percent different from duration, inches, or cm. I also don't see any reason to define inches or cm, since only one system or measurement is useful. Either you measure things in inches, like I do, or you measure things in centimeters, like weird people do. :) You don't need both.

Daradol: The sd card shield i have is a stackable large SD card shield. Looks exactly the same as the one on the Arduino site but it is from eBay and is Chinese.

I don't think that is relevant. The only thing that counts is the select pin and you can assume it is the same as all the others until it is evident it isn't.

Sorry for the basic errors with the last code. This was one of the Arduino examples so i presumed this would be a good place to start.

It is. It is just that you have a swag of junk that is best removed as a matter of courtesy. The matter is made worse by not posting it in the recommended manner - which I see you now know about!.

Any help you guys could give with writing the "percent" variable to a CSV file would be great as i have been pulling my hair out for couple weeks now trying to implement it.

I guess this is a rangefinder device that reads the liquid surface in a tank of known depth so the percent is (depth-dist)*100/depth. Since you are sending the data to Excel, you could work it out there if you can't do it in Arduino. The maths is the same in either.

By the way i really need to write the current reading to row 1 so most current is at the top and the preceding reading are consecutively below.

I bet you don't.