Using millis() for unequal ON/OFF times in nested statements
okay working on project lm34dz control digital pin on off attached
to relay. have 3 different temperatures want relay on amount , off
a different amount of time. have done delay's aware causes arduino
to not process during these times. know 1 solution use millis() , have see code can
have independent on , off times post (listed below conditions).
the temps , on/off times listed here
temp>80°f relay on .5mins off 2.5mins
temp>90°f relay on 1min off 2mins
temp>95°f relay on 2.5mins off .5mins
code unequal intervals http://forum.arduino.cc/index.php?topic=82777.0;wap2
code: [select]
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 using series of if else if temperature conditional statements , have variable store
the previous millis , cant seems either reset variable or make independent previous millis varibles , have code work. code have works whatever reason current time gets large ever be
caught if temp changes quickly
i appreciate feel beneficial others.
here code far sorry quite bit repetitive in nature
code: [select]
//code have relay turn on , off @ 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 need convert reading voltage value
//using readanalogvoltage example code can this
float voltage = rawvalue * (5.0/1023.0); //here scale raw a/d value voltage
//because using 5v onboard power supply
//our sensor reads in millivolts need convert can see change. the
//lm34dz 10mv per °f (deg symbol done alt+0176)
float millivolts = voltage * 1000; //here convert volts millivolts multiplying 1000
float tempf = millivolts/10; //because our sensor reads 1 degree farenheit/10mv will
//divide 10 scale our value
serial.print (tempf);
if (tempf > threshold3){
serial.println (" >95"); //simply print temperature 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);
}
suggest define variables hold current on duration , off duration, record whether output on or off , use compare elapsed time against either on interval or off interval, this:
i'll leave update current on , off durations depending on temperature.
code: [select]
if(digitalread(pin) == high)
{
if(millis() - starttime >= onduration)
{
starttime += onduration;
digitalwrite(pin, low);
}
}
else
{
if(millis() - starttime >= offduration)
{
starttime += offduration;
digitalwrite(pin, high);
}
}
i'll leave update current on , off durations depending on temperature.
Arduino Forum > Using Arduino > Programming Questions > Using millis() for unequal ON/OFF times in nested statements
arduino
Comments
Post a Comment