I have a logging sketch that works. It logs as well as sending to serial. I am also sending serial to my phone via bluetooth. It all works but, I would like to send to serial every 5 seconds so that I can see what the values are on my phone. In order to conserve space on memory card, I don't need to log anywhere near that often, every 15 minutes would be plenty. The problem I'm having is if I extend the logging times, it takes that long for the first reading to show on serial/phone. How do I change it to output to serial at different time intervals than log intervals?
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#include <SDL_Arduino_INA3221.h>
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 5000// 600000=10 minutes mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 5000//600000=10 minutes - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ONE_WIRE_BUS 7
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
SDL_Arduino_INA3221 ina3221;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// the three channels of the INA3221
#define HOUSE_BATTERY 1
#define CHARGER_CHANNEL 2
#define START_BATTERY_CHANNEL 3
RTC_DS1307 RTC; // define the Real Time Clock object
//RTC_DS3231 rtc;
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
// digitalWrite(redLEDpin, HIGH);
while (1);
}
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now(); //this was changed to RTC
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
void setup(void)
{
Serial.begin(9600);
sensors.begin();
Serial.println();
ina3221.begin();
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
// 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)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
//put this next line *Right Before* any file open line:
// create a new file
char filename[] = "LOG00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[3] = i / 10 + '0';
filename[4] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
SdFile::dateTimeCallback(dateTime);
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
Serial.println (" ");
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println(" BMS SAFETY ");
logfile.println(" ");
logfile.println(" Date, Time, Temperature, Temperature, Temperature, Temperature, Voltage , Voltage, Voltage");
logfile.println(" , , Batt 1, Batt 2, Cabin, Heat, House, Charger, Start");
logfile.println(" ");
}
void loop(void)
{
DateTime now;
sensors.requestTemperatures();
// delay for the amount of time we want between readings
delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));
//+++++++++++++++++++++++++++++++ Date and Time
now = RTC.now();
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(" , ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
Serial.println (" ");
#endif //ECHO_TO_SERIAL
//++++++++++++++++++++++++++++Temperature
float temperature1 = sensors.getTempFByIndex(0);
//logfile.print ("Batt 1 ");
logfile.print( sensors.getTempFByIndex(0));
logfile.print (" F");
logfile.print(",");
float temperature2 = sensors.getTempFByIndex(1);
//logfile.print ("Batt 2 ");
logfile.print(sensors.getTempFByIndex(1));
logfile.print (" F ");
logfile.print(",");
float temperature3 = sensors.getTempFByIndex(2);
//logfile.print ("Cabin ");
logfile.print(sensors.getTempFByIndex(2));
logfile.print (" F ");
logfile.print(",");
float temperature4 = sensors.getTempFByIndex(3);
//logfile.print ("Heat ");
logfile.print(sensors.getTempFByIndex(3));
logfile.print (" F ");
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print ("Batt temp 1 ");
Serial.println(sensors.getTempFByIndex(0));
Serial.print ("Batt temp 2 ");
Serial.println(sensors.getTempFByIndex(1));
Serial.print ("Cabin temp 3 ");
Serial.println(sensors.getTempFByIndex(2));
Serial.print ("Heat temp 4 ");
Serial.println(sensors.getTempFByIndex(3));
Serial.println (" ");
#endif //ECHO_TO_SERIAL
//++++++++++++++++++++++++++Volts
float busvoltage1 = 0;
busvoltage1 = ina3221.getBusVoltage_V(HOUSE_BATTERY);
//logfile.print ("House ");
logfile.print(busvoltage1);
logfile.print(",");
float busvoltage2 = 0;
busvoltage2 = ina3221.getBusVoltage_V(CHARGER_CHANNEL);
//logfile.print ("Charger ");
logfile.print(busvoltage2);
logfile.print(",");
float busvoltage3 = 0;
busvoltage3 = ina3221.getBusVoltage_V(START_BATTERY_CHANNEL);
//logfile.print ("Start ");
logfile.print(busvoltage3);
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print("House Battery Voltage: ");
Serial.print(busvoltage1);
Serial.println(" V");
Serial.print("Charger Voltage : ");
Serial.print(busvoltage2);
Serial.println(" V");
Serial.print("Start Battery Voltage : ");
Serial.print(busvoltage3);
Serial.println(" V");
Serial.println("");
#endif //ECHO_TO_SERIAL
logfile.println();
//#if ECHO_TO_SERIAL
// Serial.println();
//#endif // ECHO_TO_SERIAL
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
logfile.flush();
}