HeliBob,
I assumed that since both parts work independently, the problem lies not with the code but with my misunderstanding of how the two processes might interact.
However, here is the code. To avoid confusing everyone with code that I haven't yet cleaned up, I have omitted the function declarations. They definitely work OK.
void setup()
{
pinMode(CardDetect, INPUT); // Card Detect on pin 6.
pinMode(greenLED, OUTPUT); // To indicate ready or error status via green LED on pin 5.
pinMode(10, OUTPUT); // CS (chip select) pin 10.
Wire.begin(); //initializes the Wire library and joins the I2C bus as master (because parameter not specified)
}
// --------------------------------------------------------------------------------------------------------------------------------
void loop()
{
Serial.begin(9600); // Initializes the Serial library.
Serial.println("Ready to check Ardulog RTC: (a) set-up and SD card, (b) clock time.");
Serial.println("Enter a or b in the box above and press Return.");
while(!Serial.available())
{
error();
}
char aorb = Serial.read();
if(aorb=='a')
{
//Checking set-up and SD card
Serial.println("Checking set-up and SD card.");
delay(50);
// Check SD card present. LOW on pin A2 (card detect pin) means a card is inserted.
if(digitalRead(CardDetect) == HIGH)
{
Serial.println("ERROR: no SD card");
while(digitalRead(CardDetect) == HIGH)
{
error();
}
Serial.println("SD card detected. Checking card and contents.");
}
// OK, so SD card is present. Check whether card can be written to. Returns true on success, false on failure.
while(!SD.begin(10)) // NB the optional parameter "10" indicates which is the CS pin. On the Ardulog, pin 10 is hard-wired to select the SD card reader.
{
error(); // ERROR: can't write to card. Flash green LED in warning mode until problem rectified.
}
// SD card is present and useable if we reach this line. Now need to read config.txt file.
// First check whether config.txt file exists on the SD card.
if(SD.exists("config.txt"))
{
Serial.println("OK, config.txt file found on SD card");
}
else
{
Serial.println("No config.txt file found on SD card");
}
while(!SD.exists("config.txt"))
{
error(); // ERROR: No config.txt file. Flash green LED in warning mode until problem rectified.
}
configFile = SD.open("config.txt", FILE_READ);
for (int c=0; c<6; c++) // Start reading file.
{
char k = configFile.read(); // Read number of bytes prescribed by loop.
if(k == '\n' || k == EOF)
{
error(); // If line end or EOF is reached unexpectedly, go into ERROR mode until problem rectified.
}
else
{
siteID[c] = k; // If not yet line end or EOF, write the next character to string.
}
}
siteID[6] = '\0'; // Write string terminator.
Serial.print("Logger ID: ");
Serial.println(loggerID);
char x = configFile.read(); // Read and ignore next character (can be any white space character such as new line '\n').
char y = configFile.read(); // Read and ignore next character (can be any white space character such as new line '\n').
for (int j=0; j<3; j++) // Now continue reading more bytes as prescribed by fresh loop.
{
char o = configFile.read(); // Read number of bytes prescribed by loop.
if(o == '\n' || o == EOF)
{
error(); // If line end or EOF is reached unexpectedly, go into ERROR mode until resolved.
}
else
{
operatorID[j] = o; // Write the character to string.
}
}
operatorID[3] = '\0'; // Write string terminator.
Serial.print("Operator : ");
Serial.println(operatorID);
configFile.close(); // Close config file again immediately.
Serial.println("Set-up and SD card OK.");
}
if(aorb=='b')
digitalWrite(10,HIGH);
{
// Check clock time
do
{
getRTCdata(seconds,minutes,hours,dow,days,months,years);
getdateString(dow,days,months,years);
gettimeString(hours,minutes,seconds);
Serial.println(dateString + "," + timeString);
Serial.println("Is date and time OK? Type y or n in command box above, followed by Return");
while(!Serial.available())
{
signal();
}
char option = Serial.read();
if(option=='n')
{
Serial.println("Remove SD card from Ardulog RTC to enable date/time reset");
while(digitalRead(CardDetect) == LOW)
{
error();
}
Serial.println("Now enter date and time in this format: DD/MM/YY HH:MM:SS d");
Serial.println("where d=day of the week: Sun=1 Mon=2 Tue=3 Wed=4 Thur=5 Fri=6 Sat=7");
Serial.println(" Don't forget to press Return!");
do
{
signal();
} while(!Serial.available());
for (int z = 0; z<19; z++)
{
char inByte = Serial.read();
datetime[z] = inByte;
}
setDateTime(datetime); //Calls function to reset clock
}
} while(option == 'n');
reps=0;
while(reps<500)
{
getRTCdata(seconds,minutes,hours,dow,days,months,years);
getdateString(dow,days,months,years);
gettimeString(hours,minutes,seconds);
Serial.println(dateString + "," + timeString);
reps++;
}
}
} // repeat void loop()