Adding high and low temperatures for current day to TFT screen

Hi all, basically what I'd like to do is in the title. I'd like to display the high and low temperatures for the current day and when it is midnight have that part on the screen to reset.

Also, when logging to an SD card, I notice when I take out my SD card with the program running to get the data, that after I plug it back in if another reading has been taken before then, it won't log anymore. I have to unplug and replug or hit the reset button. This isn't a big deal to do of course, I was hoping you all had any tips that would allow the data to be logged to an SD card after putting it back in the module without having to reset Arduino. Below is my current code. It's making me split it up because of character limits. Thanks!

#include "Wire.h"
#define P0 1013.25
#include "DHT.h"
#include <SPI.h>
#include <SD.h>
#include <stdlib.h>
#include "Sodaq_DS3231.h"
#include <BME280_MOD-1022.h>
#include "Ucglib.h"
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/ A12, /*data=*/ A11, /*cd=*/ A10 , /*cs=*/ A9, /*reset=*/ A8);

void printFormattedFloat(float x, uint8_t precision) {
char buffer[10];

  dtostrf(x, 7, precision, buffer);
  Serial.print(buffer);

}


// print out the measurements

void printCompensatedMeasurements(void) {

float temp, humidity,  pressure, pressureMoreAccurate;
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate;
char buffer[80];

  temp      = BME280.getTemperature();
  humidity  = BME280.getHumidity();
  pressure  = BME280.getPressure();
  
  pressureMoreAccurate = BME280.getPressureMoreAccurate();  // t_fine already calculated from getTemperaure() above
  
  tempMostAccurate     = BME280.getTemperatureMostAccurate();
  humidityMostAccurate = BME280.getHumidityMostAccurate();
  pressureMostAccurate = BME280.getPressureMostAccurate();
  Serial.println("                Good  Better    Best");
  Serial.print("Temperature  ");
  printFormattedFloat(temp, 2);
  Serial.print("         ");
  printFormattedFloat(tempMostAccurate, 2);
  Serial.println();
  
  Serial.print("Humidity     ");
  printFormattedFloat(humidity, 2);
  Serial.print("         ");
  printFormattedFloat(humidityMostAccurate, 2);
  Serial.println();

  Serial.print("Pressure     ");
  printFormattedFloat(pressure, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMoreAccurate, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMostAccurate, 2);
  Serial.println();
}
const byte pink14 = A0;
const byte blue2 = A1;  // wired output - R - Gnd  High = illuminated all 3
const byte yellowgreen3 = A2;
const byte green4 = A3;
const byte yellow5 = A4;
const byte orange6 = A5;
const byte red7 = A6;
const byte white8 = A7;
#define DHTPIN 9     // what digital pin we're connected to
const int chipSelect = 53;
float time;

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);

void setup()
{ 
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  Serial.println("BMP DHT22 Measurement");
  dht.begin();
      ucg.begin(UCG_FONT_MODE_TRANSPARENT);
  //ucg.begin(UCG_FONT_MODE_SOLID);
  ucg.clearScreen();
  pinMode(pink14, OUTPUT);
  pinMode(blue2, OUTPUT);
  pinMode(yellowgreen3, OUTPUT);
  pinMode(green4, OUTPUT);
  pinMode(yellow5, OUTPUT);
  pinMode(orange6, OUTPUT);
  pinMode(red7, OUTPUT);
  pinMode(white8, OUTPUT);
}
void loop()
{
  delay(300000);
  time = millis();
        //ucg.setRotate270();
    ucg.clearScreen();
  ucg.setFont(ucg_font_7x14B_tf);
    ucg.setColor(1, 0, 0, 255);
  ucg.setColor(255, 255, 255);
  //ucg.setColor(0, 255, 0);  
 ucg.setPrintPos(0, 15);
  uint8_t chipID;
Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!");
  Serial.println("Embedded Adventures (www.embeddedadventures.com)");
  chipID = BME280.readChipId();
  
  // find the chip ID out just for fun
  Serial.print("ChipID = 0x");
  Serial.print(chipID, HEX);
  
 
  // need to read the NVM compensation parameters
  BME280.readCompensationParams();
  
  // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything
  BME280.writeOversamplingPressure(os1x);  // 1x over sampling (ie, just one sample)
  BME280.writeOversamplingTemperature(os1x);
  BME280.writeOversamplingHumidity(os1x);
  
  // example of a forced sample.  After taking the measurement the chip goes back to sleep
  BME280.writeMode(smForced);
  while (BME280.isMeasuring()) {
    Serial.println("Measuring...");
    delay(50);
  }
  Serial.println("Done!");
  
  DateTime now = rtc.now();

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  if (f >= 100)
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, HIGH);
}

 if (f >= 90 && f < 100)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, HIGH);
  digitalWrite(white8, LOW);
}

 if (f >= 80 && f < 90)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, HIGH);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}

 if (f >= 70 && f < 80)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, HIGH);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}

 if (f >= 60 && f < 70)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, HIGH);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}

 if (f >= 50 && f < 60)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, HIGH);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}

 if (f >= 40 && f < 50)     
{
  digitalWrite(pink14, LOW);
  digitalWrite(blue2, HIGH);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}

if (f < 40)
{
  digitalWrite(pink14, HIGH);
  digitalWrite(blue2, LOW);
  digitalWrite(yellowgreen3, LOW);
  digitalWrite(green4, LOW);
  digitalWrite(yellow5, LOW);
  digitalWrite(orange6, LOW);
  digitalWrite(red7, LOW);
  digitalWrite(white8, LOW);
}
  
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

      BME280.readMeasurements();
      Serial.print(BME280.getTemperature());  // must get temp first
      Serial.print(",");
      Serial.print(BME280.getHumidity());
      Serial.print(",");
      Serial.print(BME280.getPressure());
      Serial.print(",");
      Serial.print(BME280.getPressureMoreAccurate());  // use int64 calculcations
      Serial.print(",");
      Serial.print(BME280.getTemperatureMostAccurate() * 9 / 5 + 32);
      Serial.print(",");
      Serial.print(BME280.getHumidityMostAccurate()); // use double calculations
      Serial.print(",");
      Serial.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (BME280.getTemperatureMostAccurate() + 273.15))) * 0.0295301); // use double calculations
      Serial.print(",");
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.date(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(",");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
         if(now.minute() < 10){
    Serial.print('0');}
      Serial.print(now.minute(), DEC);
      Serial.print(':');
          if(now.second() < 10){
    Serial.print('0');}      
      Serial.print(now.second(), DEC);
      Serial.print(",");
      Serial.print("Temperature: ");
      Serial.print(f);
      Serial.print(" *F, ");
      Serial.print("Humidity: ");
      Serial.print(BME280.getHumidityMostAccurate());
      Serial.print(" %, ");
      Serial.print("Temp C: ");
      Serial.print(t);
      Serial.print(" Dew Point:  ");
      Serial.print(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);
      Serial.print("   Heat Index:  ");
      Serial.println(hif);
      
  if (f >= 100)
{
  ucg.setColor(234, 52, 4);
}

 if (f >= 90 && f < 100)     
{
  ucg.setColor(255, 199, 2);
}

 if (f >= 80 && f < 90)     
{
  ucg.setColor(228, 229, 3);
}

 if (f >= 70 && f < 80)     
{
  ucg.setColor(9, 192, 16);
}

 if (f >= 60 && f < 70)     
{
  ucg.setColor(1, 212, 219);
}

 if (f >= 50 && f < 60)     
{
  ucg.setColor(25, 93, 194);
}

 if (f >= 40 && f < 50)     
{
  ucg.setColor(95, 28, 169);
}

if (f < 40)
{
  ucg.setColor(189, 90, 196);
}
 ucg.print(now.month(), DEC);
  ucg.print('/');
  ucg.print(now.date(), DEC);
  ucg.print('/');
  ucg.print(now.year(), DEC);
  ucg.print(" ");
  ucg.print(now.hour(), DEC);
  ucg.print(':');
           if(now.minute() < 10){
    ucg.print('0');}
  ucg.print(now.minute(), DEC);
  ucg.print(':');
           if(now.second() < 10){
    ucg.print('0');}      
  ucg.print(now.second(), DEC);
    ucg.setPrintPos(0, 62);
  ucg.setFont(ucg_font_inb38_tf);
  ucg.print(f, 0);
  ucg.print("\xB0");
  ucg.setFont(ucg_font_7x14B_tf);
     ucg.setPrintPos(0, 84);
  ucg.print("Humidity: ");
  ucg.print(BME280.getHumidityMostAccurate(), 0);
  ucg.print("%");
    ucg.setPrintPos(0, 99);
  ucg.print("Pressure: ");
  ucg.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (BME280.getTemperatureMostAccurate() + 273.15))) * 0.0295301);
  ucg.print("\"");
    ucg.setPrintPos(0, 116);
  ucg.print("Dew Point: ");
  ucg.print((243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32), 0);
  ucg.print("\xB0");
  ucg.print("F");
    ucg.setPrintPos(0, 132);
  ucg.print("Heat Index: ");
  ucg.print(hif, 0);
  ucg.print("\xB0");
  ucg.print("F");
    ucg.setPrintPos(0, 148);
  ucg.print("Days: ");
  ucg.print((time/60000)/60/24, 0);
   if (f < 50)     
{
  ucg.drawDisc(108, 130, 20, UCG_DRAW_ALL);
}

      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      File dataFile = SD.open("dhtbmptc.txt", FILE_WRITE);

      // if the file is available, write to it:
      if (dataFile)
      {
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.date(), DEC);
        dataFile.print('/');
        dataFile.print(now.year(), DEC);
        dataFile.print(",");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
                   if(now.minute() < 10){
    dataFile.print('0');}
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
                 if(now.second() < 10){
    dataFile.print('0');}      
        dataFile.print(now.second(), DEC);
        dataFile.print(",");
        dataFile.print(f);
        dataFile.print(",");
        dataFile.print(BME280.getHumidityMostAccurate());
        dataFile.print(",");
        dataFile.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (t + 273.15))) * 0.0295301);
        dataFile.print(",");
        dataFile.print(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);
        dataFile.print(",");
        dataFile.println(hif);
      
      
      dataFile.close();

      }   else
    {
      Serial.println("Error.");
    }
}

You can record min and max at midnight, then reset them with absurd dummy values. Then poll them in the loop and compare with current readings. If the current is lower than min, then min = current, etc etc The initial dummies are declared in Setup().

Thanks for the reply. I'm not very good at coding, so I'm not exactly sure at how to go about this. The trickiest part seems to be setting it up to reset at midnight, but even the simplest part of this I'm not sure of either. Can you help with this? Thanks.

I don't know exactly how the SD code is written, but I would guess that the hardware senses when the SD reader is "empty" and probably releases the file handle. I think your code is probably going to have to assume that it needs to be reopened.

Well here's my SD code for opening and writing to a file and closing it. I don't know if that helps.

      File dataFile = SD.open("dhtbmptc.txt", FILE_WRITE);

      // if the file is available, write to it:
      if (dataFile)
      {
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.date(), DEC);
        dataFile.print('/');
        dataFile.print(now.year(), DEC);
        dataFile.print(",");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
                   if(now.minute() < 10){
    dataFile.print('0');}
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
                 if(now.second() < 10){
    dataFile.print('0');}      
        dataFile.print(now.second(), DEC);
        dataFile.print(",");
        dataFile.print(f);
        dataFile.print(",");
        dataFile.print(BME280.getHumidityMostAccurate());
        dataFile.print(",");
        dataFile.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (t + 273.15))) * 0.0295301);
        dataFile.print(",");
        dataFile.print(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);
        dataFile.print(",");
        dataFile.println(hif);
      
      
      dataFile.close();

      }   else
    {
      Serial.println("Error.");
    }

I think all that if and else stuff is redundant waffle. It is normal to test the SD in setup but that is all, and in the loop it need only be like

    myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(supply);
  myFile.print(",");
  myFile.print(maxOut);
  myFile.print(",");
  myFile.println(maxkW); 
    myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE

File myFile; goes in Setup

For midnight I would check hour==0 but I am changing to

   GetClock();
  if (today != day)
  {
   today = day;
   minmax();
   getFileName(); 
  }

You might try something similar.

Nick_Pyner:
File myFile; goes in Setup

However, if he is removing the SD card while the code is running, the setup() code is long gone. The question is what does the SD and fat lib code do when it tries to access the SD card and the slot is empty? If it releases the file handle and then tries to reopen it, that code probably needs to be accessible from within loop().

econjack:
However, if he is removing the SD card while the code is running,

Really? Who the hell would do something like that? And how good an idea is it?

I was going to say before that that waffle is only there as comfort to the paranoid who constantly worries that somebody might sneak in and steal the SD, but I refrained.

There is as yet no apparent need, but I would suggest that, if OP does want to get data off SD with the code running, he would be better advised to introduce some code to download it. It's probably easier on the fingers too.

Ok, well I think I've got the gist of the high and low temperatures. Thanks for the help. I haven't got to try out the code yet. I think the big thing now is with this DS3231 Sodaq library is determining when the day changes.

now.date() is the function for the current day with this library. I haven't found how to recognize when it changes.

if(now.date() != now.date())
{float minTemp = -30
float maxTemp = 130}

I know that the code there as written won't do what I'd like most likely. I'm not sure what I need to do to differentiate between the days. I'm pretty sure that's the last part I'll need in order to get this going for daily high and low temps. Here is the link to the library:

Thanks again!

Nick_Pyner:
Really? Who the hell would do something like that?

Well, if you read the original post:

Also, when logging to an SD card, I notice when I take out my SD card with the program running to get the data...

evidently the OP does.