Okay so I am working on a project with an LM34dz that will control a digital pin on an off that is attached
to a relay. I have three different temperatures where I want the relay to be ON for a certain amount and OFF for
a different amount of time. I have done this successfully with delay's but I am aware that this causes the arduino
to not process anything during these times. I know one solution is to use millis() and I have see code where you can
have independent on and off times from another post (listed below my conditions).
The temps and ON/OFF times are listed here
Temp>80°F relay on for .5mins off for 2.5mins
Temp>90°F relay on for 1min off for 2mins
Temp>95°F relay on for 2.5mins off for .5mins
Code that does unequal intervals from Using millis to blink - unequal intervals? - Programming Questions - Arduino Forum
unsigned long currentTime = millis();
if (currentTime > nextTime) {
if (digitalRead(PIN)) {
digitalWrite(PIN, LOW);
nextTime = currentTime + OFF_INTERVAL;
} else {
digitalWrite(PIN, HIGH);
nextTime = currentTime + ON_INTERVAL;
}
}
}
My problem is I am using a series of if else if for the temperature conditional statements and have a variable that will store
the previous millis and I cant seems to either reset that variable or make independent previous millis varibles and have the code work. The code I currently have works some what but for whatever reason the current time gets too large to ever be
caught up to if the temp changes quickly
I would greatly appreciate any help as I feel this could be beneficial to others.
Here is my code so far sorry its quite a bit but repetitive in nature
//code to have relay turn ON and OFF at uneven intervals- in multiple conditional statements
const int threshold1 = 80;
const int threshold2 = 90;
const int threshold3 = 95;
const unsigned long ON_INTERVAL1=1.5*60*1000;
const unsigned long OFF_INTERVAL1=.5*60*1000;
const unsigned long ON_INTERVAL2=1*60*1000;
const unsigned long OFF_INTERVAL2=2*60*1000;
const unsigned long ON_INTERVAL3=.5*1000*60;
const unsigned long OFF_INTERVAL3=2.5*1000*60;
unsigned long nextTime=0;
int LEDpin_Relay = 13;
void setup() {
Serial.begin(9600);
pinMode(LEDpin_Relay, OUTPUT);
}
void loop() {
int rawvalue = analogRead(A0);
//next we need to convert the reading to a voltage value
//using the ReadAnalogVoltage example code we can do this
float voltage = rawvalue * (5.0/1023.0); //Here we scale the raw A/D value to a voltage
//because we are using the 5v onboard power supply
//Our sensor reads in millivolts so we will need to convert so we can see the change. The
//LM34dz does 10mV per °F (deg symbol done with Alt+0176)
float millivolts = voltage * 1000; //here we convert volts to millivolts by multiplying by 1000
float TempF = millivolts/10; //because our sensor reads 1 degree farenheit/10mV we will
//divide by 10 to scale our value
Serial.print (TempF);
if (TempF > threshold3){
Serial.println (" >95"); //simply print the temperature with a new line
unsigned long currentTime = millis();
if (currentTime > nextTime) {
if (digitalRead(LEDpin_Relay)) {
digitalWrite(LEDpin_Relay, LOW);
nextTime = currentTime + OFF_INTERVAL1;
}
else {
digitalWrite(LEDpin_Relay, HIGH);
nextTime = currentTime + ON_INTERVAL1;
}
}
}
else if ( TempF >= threshold2 && TempF <= threshold3) {
Serial.println (">90");
unsigned long currentTime = millis();
if (currentTime > nextTime) {
if (digitalRead(LEDpin_Relay)) {
digitalWrite(LEDpin_Relay, LOW);
nextTime = currentTime + OFF_INTERVAL2;
}
else {
digitalWrite(LEDpin_Relay, HIGH);
nextTime = currentTime + ON_INTERVAL2;
}
}
}
else if (TempF >= threshold1 && TempF <= threshold2){
Serial.println(">80");
unsigned long currentTime = millis();
if (currentTime > nextTime) {
if (digitalRead(LEDpin_Relay)) {
digitalWrite(LEDpin_Relay, LOW);
nextTime = currentTime + OFF_INTERVAL3;
}
else {
digitalWrite(LEDpin_Relay, HIGH);
nextTime = currentTime + ON_INTERVAL3;
}
}
}
else if (TempF < threshold1) {
Serial.println("<80");
digitalWrite(LEDpin_Relay ,LOW);
}
delay(1000);
}