I am building a serial communication sketch that uses serial print and serial read to talk to a computer. I have the sketch very close using the new SdFat library. The SdFat library is AWESOME and has helped my speed so much!!! Thanks to the creator!
The only problem I have I am aware of is that it looks like I am only saving one string of data then, erasing it and doing it again. If I am correct could someone help me with the change that I need to make to the sketch?
I would like to log my communication reads until the 2GB card is full or until I turn off the unit. At the moment, I think my string is 50 Bytes long.
// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 8;
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
*/
int startattempt = 0; //counter for start attempts:
int talking = 0; // communication status bit:
String readString; // storage for incoming characters:
int dataadd = 0; // counter to reduce file closes:
char intalk = 0; // incoming serial hex byte marker:
long rxbyte = 0; //used for storing incoming :
int txbyte = 0; //outgoing serial hex byte:
int bytecount = 0; //may use for counting string inputs:
int outputpin = 1; //TX pin control to for HIGH/LOW state:
int failurepin = 9; // led pin to show card failure:
long charAt = 0; // possibly used to find characters in string:
unsigned long previousMillis = 0; // will store last time:
unsigned long currentMillis = 0; // stores current time:
unsigned long interval = 150; // interval at which to time communication events:
int i = 0;
char inChar = 0;
String stringOne = 0; // string for storing incoming bytes
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins:
#include <SdFat.h>
SdFat sd;
SdFile myFile;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(2,1);
lcd.print("Ini. SD card");// Print a message to the LCD.
Serial.print("Ini SD card");
pinMode(10, OUTPUT); // default chip select pin output, even if you don't use it:
// see if the card is present and can be initialized:
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_FULL_SPEED, chipSelect)) sd.initErrorHalt();
pinMode(13, OUTPUT);
Serial.println("serial test"); // so I can keep track of what is loaded:
digitalWrite(outputpin, HIGH); // digital pin 1 as an output to control communication intialization:
delay(300);
while (i < 2 && Serial.available()) {
delay(2000); // to allow me to manually enter a number for testing:
char c = Serial.read();
readString += c;
i = i + 1;
}
if (readString.length() > 0 ) {
Serial.println(readString);
if (readString == "00" ) {
readString = ""; // clear readstring to reuse it
talking = 1;
digitalWrite(13, HIGH);
Serial.println("completed ini");
lcd.print("completed ini");
Serial.print(129,HEX);
}
else {
digitalWrite(13, LOW);
Serial.println(" ini. failed" );
lcd.print(" ini. failed");
Serial.println (readString);
readString = ""; // clear readstring to reuse it
talking = 0;
}
}
currentMillis = millis();
previousMillis = currentMillis; // set the time counter:
}
void loop() {
Serial.println (talking);
while (talking == 1 && currentMillis - previousMillis < interval){
Serial.println(" big dog"); // at way to see progress:
currentMillis = millis();
}
talking = 3; // lock into the next loop of communication
interval = 140; // adjusted interval for delay in the loop;
currentMillis = millis();
previousMillis = currentMillis; // set the time counter:
Serial.println("data request");
lcd.print("data request");
Serial.print(128, HEX);
bytecount = 0;
delay(10);
while (bytecount < 50 && currentMillis - previousMillis < interval ) {
char c = Serial.read();
readString += c;
bytecount = bytecount + 1;
currentMillis = millis(); // refresh the timer:
}
if (readString.length() > 0 && talking == 3) {
Serial.println ("bytecounter full");
Serial.println(bytecount);
}
// open the file for write at end like the Native SD library
if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile.println(readString);
readString = ""; // clear readstring to reuse it
talking = 1; // go back to data request
// close the file:
myFile.close();
Serial.println("done.");
// re-open the file for reading:
if (!myFile.open("test.txt", O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read()) > 0) Serial.write(data);
// close the file:
myFile.close();
}