J-M-L
Close but no cigar. I moved the definitions of the variables to be global. I then ran it but there was no difference. I also noticed that the variables in the function were a highlighted color, like Serial.print is, so I thought they must be reserved words. For example "year" in the statement year = now.year(); was highlighted. I changed that to SDyear, added SD to the others as well, made no difference other than SDyear was not highlighted.
The statement that logs the date in the SD file is "logfile.print(now.year(), DEC);" if I used that format, adding the DEC, I get different results. I added print statements to monitor what the values were.
without the DEC
month, day, year, hour, minute,seconds = 10. The date on the file is 10/10/2058 11:10, the date/time in the file is correct
with DEC
month, day = 1, year = 2000, hour, minute, second = 0, The date on the file is 1/1/200 12:00 AM, the date/time in the file is correct.
I'm thinking it has something to do with either how the variables are defined, how the RTC value is set to the SDyear variable as opposed to how the RTC variable is used in the logfile.print statement.
I have the test function on a separate tab in the sketch, it is called when the test is started by pressing a button on the display screen. Here is how I've declared the variables
// for date stamp on SD file
unsigned int SDyear = 2019;
byte SDmonth = 11;
byte SDday = 15;
byte SDhour = 6;
byte SDminute = 11;
byte SDsecond = 00;
and here is the function
// for adding current date and time to SD file
void dateTime(uint16_t* date, uint16_t* time)
{
DateTime now; // read RTC
SDyear = now.year();
SDmonth = now.month();
SDday = now.day();
SDhour = now.hour();
SDminute = now.minute();
SDsecond = now.second();
*date = FAT_DATE(SDyear, SDmonth, SDday);
*time = FAT_TIME(SDhour, SDminute, SDsecond);
Serial.print("month = ");
Serial.print(SDmonth);
Serial.print(" ");
Serial.print("day = ");
Serial.print(SDday);
Serial.print(" ");
Serial.print("year = ");
Serial.print(SDyear);
Serial.print(" ");
Serial.print("hour = ");
Serial.print(SDhour);
Serial.print(" ");
Serial.print("minute = ");
Serial.print(SDminute);
Serial.print(" ");
Serial.print("second = ");
Serial.println(SDsecond);
}
// Pump Test logic and data logging
void Pump_Test()
{
// read limit switches
fwdLS_status = digitalRead(fwdLS);
revLS_status = digitalRead(revLS);
if(cycleStep <= 0){
cycleStep = 1;
cycleCount = 0;
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
errorNumb = 1;
error("Card failed, or not present");
}
Serial.println("card initialized.");
SdFile::dateTimeCallback(dateTime); // sets file date
// create a new file
char filename[] = "LogFile00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[7] = i/10 + '0';
filename[8] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
strcpy(logFileName, filename);
break; // leave the loop!
} // end if (! SD.exist)
} // end for (uint8_t i = 0; i < 100; i++)
if (! logfile) { // checks that logfile created
errorNumb = 2;
error("couldnt create file");
} // end (! logfile)
DateTime now; // read RTC
// fetch the time
now = RTC.now();
logfile.print("Test # = ");
logfile.print(", ");
logfile.print(testNumb);
logfile.print(", ");
logfile.print("Serial # = ");
logfile.print(", ");
logfile.print(serialNumb);
logfile.print(", ");
logfile.print("Date ");
logfile.print(", ");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(" ");
logfile.print(", ");
logfile.print("Time ");
logfile.print(", ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(" ");
logfile.println();
logfile.print("Cycle Count,Fwd Time,Rev Time,Voltage");
logfile.println();
// Close file
logfile.close();
} // end if(cycleStep <= 0)
switch(cycleStep){
case 1:
pulseFwdIn = map(TestBattIn, 0, 1025, 0, 255); // turn fwd on
analogWrite(pulseFwd,(pulseFwdIn - 25));
if(fwdPlsStart == 0){
fwdStart = millis(); // capture forward pulse start time
fwdPlsStart = 1; // set forward pulse has started bit
}
pulseRevIn = 0; // turn rev off
analogWrite(pulseRev,pulseRevIn);
if(fwdLS_status == HIGH){
pulseFwdIn = 0;
analogWrite(pulseFwd,pulseFwdIn);
pulseStartTime = millis();
fwdTime = (millis() - fwdStart); // time forward pulse was on
Serial.print("fwdTime = ");
Serial.println(fwdTime);
cycleStep = 2;
}
revPlsStart = 0; //clear reverse pulse has started bit
break;
case 2:
if(millis() > (pulseStartTime + dwellPreset)){
pulseStartTime = millis();
fwdPlsStart = 0; //clear forward pulse has started bit
cycleStep = 3;
}
break;
case 3:
if(millis() > (pulseStartTime + 500)){
cycleStep = 4;
}
break;
case 4:
pulseRevIn = map(TestBattIn, 0, 1025, 0, 255); // turn rev on
analogWrite(pulseRev,(pulseRevIn - 25));
if(revPlsStart == 0){
revStart = millis(); // capture reverse pulse start time
revPlsStart = 1; // set reverse pulse has started bit
}
if(revLS_status == HIGH){
pulseRevIn = 0;
analogWrite(pulseRev,pulseRevIn);
pulseStartTime = millis();
revTime = (millis() - revStart); // time forward pulse was on
cycleStep = 5;
cycleCount ++;
// log data
logfile = SD.open(logFileName, FILE_WRITE);
logfile.print(cycleCount);
logfile.print(", ");
logfile.print(fwdTime);
logfile.print(", ");
logfile.print(revTime);
logfile.print(", ");
logfile.print(BattVolts);
logfile.println();
// Close file
logfile.close();
}
break;
case 5:
if(millis() > (pulseStartTime + dwellPreset)){
cycleStep = 6;
}
break;
case 6:
if(millis() > (pulseStartTime + 500)){
pulseStartTime = millis();
cycleStep = 1;
}
break;
} // end switch
} // end void Pump_Test
Thanks for your help and time
John