I'm uploading to an Arduino Mega 2560 using v0022 on windows 7 x64
This is my test code:
/* SD card read/write
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - DI
** MISO - DO
** SCK - CLK
** SS - should always be OUTPUT
*/
#include <SD.h>
File myFile;
int CS = 30;
int SS = 53;
#include <MomentaryButton.h>
#define PIN_BUTTON1 2
MomentaryButton button1(PIN_BUTTON1);
#define PIN_BUTTON2 3
MomentaryButton button2(PIN_BUTTON2);
void setup()
{
SetupSDcard();
button1.setup(); // set as INPUT, set HIGH
button2.setup(); // set as INPUT, set HIGH
}
void loop()
{
CheckButtonClicks();
}
void SetupSDcard() {
pinMode(SS, OUTPUT);
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Note that even if it's not used as the CS pin, the hardware SS pin should be set to OUTPUT
pinMode(CS, OUTPUT);
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void CheckButtonClicks() {
button1.check();
if (button1.wasClicked()) {
// Respond to the button being briefly closed, then released.
Serial.println("Button1 was clicked");
WriteFile("piet.txt","my line of text");
}
button2.check();
if (button2.wasClicked()) {
// Respond to the button being briefly closed, then released.
Serial.println("Button2 was clicked");
ReadFile("piet.txt");
}
}
void WriteFile(char Filename[ ], char TextLine[ ])
{
// open the file. note that only one file can be open at a time,
//so you have to close this one before opening another.
myFile = SD.open(Filename, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to ");Serial.print(Filename);Serial.println("...");
myFile.println(TextLine);
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.print("error opening ");Serial.println(Filename);
}
}
void ReadFile(char Filename[ ])
{
// re-open the file for reading:
myFile = SD.open(Filename);
if (myFile) {
Serial.print(Filename);Serial.println(":");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.print("error opening ");Serial.println(Filename);
}
}