Hi,
I’m working on a temperature control project.
I have trouble to implement three time settings using the millis() function.
In the sketch, I put it in “delay() “ as a place holder for the millis() function I want to use.
Can somebody help me to adjust the sketch with the millis () function to replace all three delay functions.
Thank you!
Stefan
#include <Wire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int Motor1 = 8;
const int Motor2 = 2;
unsigned long currentmillis = 0;
unsigned long previousmillis = 0;
long Interval_l = 5000; //stands for delay 5000ms
long Interval_2 = 10000; //stands for delay 10000ms
long Interval_3 = 50000; //stands for delay 50000ms
void setup() {
pinMode(Motor1, OUTPUT);
pinMode(Motor2, OUTPUT);
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if(temperature < 23)
{
digitalWrite(Motor1,HIGH); //If temperature < 23 turn on Motor1 on for 5000ms
delay (5000);
digitalWrite(Motor1,LOW); //After 5000ms turn Motor1 off, wait 10000ms (Time starts after Motor 1 was turnded off)
delay (10000); // Go through loops till line 52 becomes true
}
//Put this the right way :)
//currentmillis = millis();
//(currentmillis - previousmillis >= Interval_1);
// previousmillis = currentmillis;
//Interval_2;
//Interval_3;
else if(temperature > 26)
{
digitalWrite(Motor2,HIGH); //If temperature > 26 turn Motor2 on for 5000ms
delay (5000);
digitalWrite(Motor2,LOW); //After 5000ms turn off Motor 2, wait 10000ms (Time starts after Motor 2 was turnded off)
delay (10000); // Go through loop till line 52 becomes true
if ((temperature > 23)&& (temperature < 26)){ // If the temperature is between 23C und 26C don't do anythimng just wait 50000ms
delay (50000); // then check temperatures again.
}
}
}