I am keeping a simple weather station to monitor humidity and temperature in my greenhouse. I have a ds18b20 and dht probes. everything works great and I even used a little trick to hold my maximum and minimum values, but I would like them to reset every 24 hours. Is there anyway i can code the arduino to reset itself. i tried several examples of connecting digital pins to the reset pin and i couldn't get any of those to work. i tried to reset the values of my max and min to 0 and 99 respectively after my millis() reached the equivalent of a day, but they just stuck at 0 and 999. I understand fully why it does this i just don't know how to fix it. all i want to do is reset my minimum and maximum temperatures after 24 hours.

//       Green HAUS Version 1 by Kent Scipione 9/3/2017
//       Monitors temperature and humidity
long timenow;
long day = 86400000; // 86400000 milliseconds in a day
long hour = 3600000; // 3600000 milliseconds in an hour
long minute = 60000; // 60000 milliseconds in a minute
long second =  1000; // 1000 milliseconds in a second

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define ONE_WIRE_BUS 10
double temp;  // sensor is precise to .1 degree. 
//Double gives smoother serial graph, byte doesn't overflow oled screen
Adafruit_SSD1306 display(OLED_RESET);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#include <Adafruit_Sensor.h>
#include "DHT.h"
#define DHTPIN 2    
#define DHTTYPE DHT22   

int maxtemp=0;
int mintemp=999;

void setup() {

  Serial.println("green HAUS Version 1 by Kent Scipione 9/3/2017");
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);


void loop() {
  timenow = millis();
  temp = sensors.getTempFByIndex(0); 
  int humid = dht.readHumidity();
  float dtemp = dht.readTemperature(true);
  if (temp > maxtemp) {
    maxtemp=temp; }
  if (temp < mintemp) {
    mintemp=temp; }

  if (timenow >= day){
      digitalWrite(4, HIGH);
      pinMode(4, OUTPUT);       //*************************this is the part where I'm trying to reset the mintemp and maxtemp**********


  Serial.print("Probe Temperature: ");
  Serial.println(temp); Serial.println("*F");
  display.setCursor(0,0);  //refreshes display
  display.setTextSize(1); display.print("Probe "); display.println(temp);  //line 1 of text
  display.print("DHT  "); display.print(dtemp); display.print("  "); display.print(humid); display.println(" %");  //line 2 of text
  display.print("HIGH "); display.println(maxtemp);           //line 3 of text
  display.print("LOW  "); display.println(mintemp);   //line 4 of text


Thank you so much for your help this is driving me mad haha

  • At first glance, a small typo in your reset section -- you have set the pinMode to OUTPUT after you try to write. You should call pinMode() before you try to call digitalWrite(). – jose can u c Sep 7 '17 at 1:06
  • if(millis()>day) ... – dandavis Sep 7 '17 at 6:14
  • Consider using #define for your constants day,hour, minutes, seconds rather than variables. While on the subject.... if you're not going to keep decimals for your temperatures then you can use byte (or unsigned byte depending on if you'd ever have negative degrees F) rather than int for your min,max and current temperatures saving a few more bytes. Finally dtemp isn't used so you can delete that line. – Squats Sep 11 '17 at 5:45

You should also keep a timestamp of the last time you reset the min and max otherwise the arduino will keep thinking it's a new day:

//add to globals
unsigned long lastDay = 0;

and inside loop()

  if (timenow-lastday >= day){

      lastDay = timenow;
  • unsigned long. long will fail after about 25 days. – Squats Sep 11 '17 at 5:38
  • But when it rolls over it will still work correctly so it doesn't matter that much – ratchet freak Sep 11 '17 at 6:57
  • You'll find unsigned long rolls over ok when compared to another unsigned long that's rolled over. A signed long won't when compared to an unsigned long in the >= comparison in the if loop. – Squats Sep 11 '17 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.