Background:
I am using a Teensy 3.6 to data log over a dozen sensors. I am printing to Serial1 for local Serial access, Serial3 for remote access (via xbee, same data is on Serial1 as on Serail3, just different physical connection) as well as datalog to SDCard (CSV format). My code is expanding and is incredibly repetitive, and I am looking for a more efficient way of writing the following. I am trying to move a lot of the rest of my code to classes using this and this tutorial, but I'm not sure this is the best option for the datalogging as there are many more strings in this portion.
The variables we are dealing with:
- Live sensor value (eOilTempF)
- A visual flag for if the sensor is out of bounds (checkEngine)
- A auditory alarm if it's further out of bounds (eOilTempAlarm)
- The max value we've recorded (engineOilStats.maximum())
- The minimum value (engineOilStats.minimum())
As well as all the little bits to make it attractive and human readable for the Serial stuff.
Code:
Local serial:
Serial.print("Engine Oil Temp:\t");
Serial.print(eOilTempF);
Serial.print("F\t ");
Serial.print("Flag: ");
Serial.print(checkEngine);
Serial.print("\t Alarm:");
Serial.print(eOilTempAlarm);
Serial.print("\tMAX: ");
Serial.print(engineOilStats.maximum());
Serial.print(" F");
Serial.print("\t\t\t\tAVG: ");
Serial.print(engineOilStats.average());
Serial.println(" F");
Xbee serial:
Serial3.print("Engine Oil Temp:\t");
Serial3.print(eOilTempF);
Serial3.print("F\t ");
Serial3.print("Flag: ");
Serial3.print(checkEngine);
Serial3.print("\t Alarm:");
Serial3.print(eOilTempAlarm);
Serial3.print("\tMAX: ");
Serial3.print(engineOilStats.maximum());
Serial3.print(" F");
Serial3.print("\t\t\t\tAVG: ");
Serial3.print(engineOilStats.average());
Serial3.println(" F");
SD Card Logging:
logfile.print(eOilTempF);
logfile.print(",");
logfile.print(checkEngine);
logfile.print(",");
logfile.print(engineOilStats.maximum());
logfile.print(",");
logfile.print(engineOilStats.average());
logfile.print(",");
tl;dr
My datalogging section of my sketch is 567 lines of mostly repeated code, and I'm new at programming and need some tutorials/terms to google to make it more effecient.
log(1, eOilTempF);
...1
points toEngine Oil Temp:\t
– jsotola Aug 9 '20 at 18:23<<
with Serial. I can't recall what it's called though and my google-fu isn't strong enough. – Majenko♦ Aug 9 '20 at 18:25e[ OilTempF ]
instead ofeOilTempF
... that way you can loop through values – jsotola Aug 9 '20 at 18:31