Hello to eveyone
i've got a problem with saving data into a SDcard
i have formatted the SD card to fat16
but the code below could not save the data, what is wrong?
and the data is auto-save or i have to tell to the arduino to save the data?
Thanks a lot.
#define FREQ_PIN 3
#include <LiquidCrystal.h>
#include <Fat16.h>
#include <Fat16util.h>
SdCard card;
Fat16 file;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
pinMode(FREQ_PIN, INPUT);
lcd.begin(16, 2);
char name[] = "WRITE00.TXT";
for (uint8_t i = 0; i < 100; i++) {
name[5] = i/10 + '0';
name[6] = i%10 + '0';
// O_CREAT - create the file if it does not exist
// O_EXCL - fail if the file exists
// O_WRITE - open for write
if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) break;
}
PgmPrint("Now Speed: ");
// write 100 line to file
}
void loop()
{
unsigned long t1 = pulseIn(FREQ_PIN, HIGH);// Time how long it takes to go HIGH again
unsigned long t2 = pulseIn(FREQ_PIN, LOW); // and how long it takes to go low again.
double t = t1 + t2;
double f = 1000000.0/t;
double w = 2 * 3.14 * f;
double v = w * 0.05;
lcd.setCursor(0,0);
lcd.println(v);
delay(2000);
file.println(v); // file.println() would work also
}
could someone help me with the file.write ?
i have a lots of question 
what code must be written for writing data ?
like initialization, is it 100% needed ?
oh my god
i've tried the code from the basic concept, which is from the fat16library's example, fat16write.
here is the code
/*
* Write Example
*
* This sketch creates a new file and writes 100 lines to the file.
* No error checks on write in this example.
*/
#include <Fat16.h>
#include <Fat16util.h> // use functions to print strings from flash memory
SdCard card;
Fat16 file;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode) {
PgmPrint("SD error: ");
Serial.println(card.errorCode, HEX);
}
while(1);
}
/*
* Write an unsigned number to file
*/
void writeNumber(uint32_t n)
{
uint8_t buf[10];
uint8_t i = 0;
do {
i++;
buf[sizeof(buf) - i] = n%10 + '0';
n /= 10;
} while (n);
file.write(&buf[sizeof(buf) - i], i); // write the part of buf with the number
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
PgmPrintln("Type any character to start");
while (!Serial.available());
// initialize the SD card
if (!card.init()) error("card.init");
// initialize a FAT16 volume
if (!Fat16::init(card)) error("Fat16::init");
// create a new file
char name[] = "WRITE00.TXT";
for (uint8_t i = 0; i < 100; i++) {
name[5] = i/10 + '0';
name[6] = i%10 + '0';
// O_CREAT - create the file if it does not exist
// O_EXCL - fail if the file exists
// O_WRITE - open for write
if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error ("file.open");
PgmPrint("Writing to: ");
Serial.println(name);
// write 100 line to file
for (uint8_t i = 0; i < 100; i++) {
file.write("line "); // write string from RAM
writeNumber(i);
file.write_P(PSTR(" millis = ")); // write string from flash
writeNumber(millis());
file.write("\r\n"); // file.println() would work also
}
// close file and force write of all data to the SD card
file.close();
PgmPrintln("Done");
}
void loop(void) {}
However, when i tried to see something, it show me like this in the serial monitor
Type any character to start
error: card.init
SD error: 1
what is wrong actually ?
And also there are some error coming out under the code window.
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:215)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Thanks a lot, i very appreciate it.