Refer this code below. It works as expected ...well atleast most of the time that is.
When I press a push button, the code has to Erase a file on SD card, then go ahead and create a fresh version of the same file on the SD card and finally read the file and display in Serial monitor.
I have a delay(scanMs); so as not to run the loop at break neck speeds (!). But what happens is when I set this scanMs to say 100ms then at times the erasePendingFile() function alone is skipped. I don't see any serial output from that function. And the contents are also not erased..
Rest of the two functions called execute as expected.
But when I set scanMs to 10ms then every single time it happens without fail.
I am curious about this behaviour. Any suggestions regarding the recommended delay for the loop() ??
/*
Hardware is EtherTen Board / UNO board. SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13 ( So this pin cannot be used for the LED connected to it.)
** CS - pin 4
*/
#include <SPI.h>
#include <SD.h>
#include <phi_interfaces.h>
const int chipSelect = 4; // SD card select on shield
// Define the Machine Digital Inputs and Outputs
#define StartPB 12 // EtherTen mapping
#define IncrPB 14
#define DecrPB 6
#define EnterPB 7
#define total_buttons 4
char mapDIN[] = {'S', 'I', 'D', 'E'}; // This is a list of names for each button.
byte pinDIN[] = { StartPB, IncrPB, DecrPB, EnterPB}; // The digital pins connected to the 4 buttons.
phi_button_groups MyDIN(mapDIN, pinDIN, total_buttons);
char ValidDIN = '0';
unsigned long scanMs = 10;
boolean rdAllData = false;
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// SETUP
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void setup()
{
Serial.begin(9600);
Serial.println(F("Initializing SD card...")); // Check if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(F("Card failed, or not present"));
return; // No card .. just return..
}
Serial.println(F("SD Card initialized."));
Serial.println(F("SerialToSD program ready "));
pinMode(chipSelect, OUTPUT);
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// MAIN LOOP
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void loop()
{
ValidDIN = MyDIN.getKey();
if ( ValidDIN == 'S' ) {
ValidDIN = '0';
Serial.println( "Got the command !");
erasePendingFile();
createPendingSummary();
readPendFile();
}
delay(scanMs);
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// FUNCTIONS
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//FUNCTION TO ERASE PENDING.CSV FILE ..
void erasePendingFile() {
if (SD.exists("PENDING.CSV")) {
if (!SD.remove("PENDING.CSV")) {
Serial.println( " Error Erasing PENDING.CSV");
}
else {
Serial.println( "PENDING.CSV Erased !!");
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// FUNCTION TO CREATE THE PENDING FILE FROM ACTUAL FILE.CSV
void createPendingSummary() {
char statusToken = '0';
int data;
File dataFile = SD.open("ACTUAL.CSV", FILE_READ); // Open the existing Actual File
File pendFile = SD.open("PENDING.CSV", FILE_WRITE); // Open the file to write the selected records..
while ((data = dataFile.read()) >= 0) {
if ( data == '<') {
statusToken = dataFile.read();
}
if (statusToken == '&') {
pendFile.write('<');
do {
data = dataFile.read();
pendFile.write(data); // Criteria token matches. Write the record to file..
} while ( data != '>');
pendFile.write("\n");
statusToken = '0';
}
if ( statusToken == '#') {
do {
data = dataFile.read(); // Ignore record. But move the read pointer for next
} while ( data != '>');
statusToken = '0';
}
}
pendFile.close(); // Write the data and close file.
dataFile.close();
} // Function end
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void readPendFile(){
File pendFile = SD.open("PENDING.CSV", FILE_READ);
if ( pendFile ){
while (pendFile.available()) { // Data file exists ..read it.
Serial.write(pendFile.read());
}
Serial.println ( pendFile.position());
pendFile.close();
}
else {
Serial.print( "No PENDING.CSV");
}
}
//*****************************************************