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
DHT dht(DHTPIN, DHTTYPE);
int maxtemp=0;
int mintemp=999;
void setup() {
Serial.begin(9600);
Serial.println("green HAUS Version 1 by Kent Scipione 9/3/2017");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.setTextColor(WHITE);
sensors.begin();
dht.begin();
}
void loop() {
timenow = millis();
sensors.requestTemperatures();
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.clearDisplay();
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
display.display();
}
Thank you so much for your help this is driving me mad haha
1 Answer 1
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){
maxtemp=0;
mintemp=999;
lastDay = timenow;
}
-
unsigned long. long will fail after about 25 days.Squats– Squats2017年09月11日 05:38:01 +00:00Commented Sep 11, 2017 at 5:38
-
But when it rolls over it will still work correctly so it doesn't matter that muchratchet freak– ratchet freak2017年09月11日 06:57:20 +00:00Commented Sep 11, 2017 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– Squats2017年09月11日 06:59:19 +00:00Commented Sep 11, 2017 at 6:59
if(millis()>day) ...