using millis like a timer

im trying to build a controller for a fan. i dont want to use delay so i can still give input to other parts of the code. basically i found this example code to get use to millis. i have the code running and ive managed to add it to my master script. can someone help me write some code for my arduino. what i want to do is wait a certain amount of time like 10 hours. after 10 hours i want the relay to turn on but only for about 1 minute. ive managed to make this example script stay on and off for 1000ms. but i cant have it stay on that long. im really new at this please help me lol.

const int relay =  A0;      // the number of the LED pin

// Variables will change:
int relayState = HIGH;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(relay, OUTPUT);      
}

void loop()
{
 
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (relayState == HIGH)
      relayState = LOW;
    else
      relayState = HIGH;
    digitalWrite(relay, relayState);
  }
}

notsolowki:
but i cant have it stay on that long.

How long is it staying on for?

Your code looks OK to me except that previousMillis should be unsigned long and I would also make interval unsigned long.

What you don't have in your code is the ability to have different ON and OFF times. You can do that by changing the value of interval at the same time as you change the state of the relay.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

Yes im trying to figure out how to have a different on off time. i was looking through your script and the problem is im too new at this to understand exactly how that script functions. could you maybe demonstrate the a very basic example to have a different on time than off time. basically every 10 hours i want it to turn the relay on for about 1 minute. alot has me scared about such a wait too. im not sure how it will perform if i set the long interval to 10 hours worth of ms. thanks again

im trying to get this script to work. i keep getting an error, expected unqualified-id before '=' token. can someone please go over this code and make it proper. thankyou

const unsigned long offTime = 60000UL;
const unsigned long onTime = 36000000UL;
unsigned long previousMillis=0;
int long = onTime;
boolean relaystate = true;
void setup() {
  pinMode(13, OUTPUT);
}
 
void loop() {
  digitalWrite(13, relaystate);
  
  unsigned long currentMillis = millis();
 if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    if (relaystate) {
      interval = offTime;
    } else {
      interval = onTime;
    }
    relaystate = !(relaystate);
    previousMillis = currentMillis;
  }
}

This might work

const int relay =  A0;  


int relayState = HIGH;
unsigned long previousMillis = 0; // changed

unsigned long interval = 0; // changed
unsigned long onInterval = 1000; // NEW
unsigned long offInterval = 10000; // NEW

void setup() {

  pinMode(relay, OUTPUT);
  interval = offInterval; // NEW not sure which should start
}

void loop()
{
 
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (relayState == HIGH) { // changed
      relayState = LOW;
      interval = offInterval; // NEW assumes LOW  = off
    }  // new
    else {  // changed
      relayState = HIGH;
      interval = onInterval; // NEW assumes high  = on
    }  // new
    
    digitalWrite(relay, relayState);

  }
}

I have deleted all of your comments just so that mine are more obvious.

...R
PS I wrote this based on Reply #2 and your Original Post

THANKYOU that did the trick. im not sure what you meant by"// NEW not sure which should start" but the off and on is backwards. i just copied the original script and changed it to be more compatible with what im doing. now when i want to have it off for example 10 hours can i just replace 1000 with 36000000UL? im not sure what the UNSIGNED LONG vs INTERVAL is. however i have fix the on off being backwards. im just worried about how to properly tell millis 10 hours. iseen in an example to us 36000000UL instead of 1000x60xetc. i just dont want to show up and the fan had not shutoff or the board crashed and ruined what i was doing lol. thanks again

thankyou @Robin2, here is my completed prototype code. if you have time id like you to take a look at it and maybe show me the clean way to do it. i mean it “works” but maybe you would do things a bit better. thanks again

#include "DHT.h"
#define TANK1 2
#define TANK2 3
#define TANK3 4
#define TANK4 5
#define TANK5 6
#define TANK6 7
#define TANK7 8
#define TANK8 9
#define TANK9 10
#define TANK10 11
#define DHTTYPE DHT22


int forcespray = A3;
int forcefan = A2;
const int relay = A0;
int relay2 = A1;
int relayState = HIGH;  
unsigned long previousMillis = 0; // changed
unsigned long interval = 0; // changed
unsigned long onInterval = 1000; // NEW
unsigned long offInterval = 10000; // NEW


DHT tank1(TANK1, DHTTYPE);
DHT tank2(TANK2, DHTTYPE);
DHT tank3(TANK3, DHTTYPE);
DHT tank4(TANK4, DHTTYPE);
DHT tank5(TANK5, DHTTYPE);
DHT tank6(TANK6, DHTTYPE);
DHT tank7(TANK7, DHTTYPE);
DHT tank8(TANK8, DHTTYPE);
DHT tank9(TANK9, DHTTYPE);
DHT tank10(TANK10, DHTTYPE);

void setup() {
  
  interval = offInterval; // NEW not sure which should start
  pinMode (relay, OUTPUT);
  pinMode (relay2, OUTPUT);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay, HIGH);
  Serial.begin(9600);
  tank1.begin();
  tank2.begin();
  tank3.begin();
  tank4.begin();
  tank5.begin();
  tank6.begin();
  tank7.begin();
  tank8.begin();
  tank9.begin();
  tank10.begin();
  Serial.print("Tanks Ready, Start Program! ");

}

void loop() {
 unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (relayState == HIGH) { // changed
      relayState = LOW;
      interval = onInterval; // NEW assumes LOW  = off
    }  // new
    else {  // changed
      relayState = HIGH;
      interval = offInterval; // NEW assumes high  = on
    }  // new
    digitalWrite(relay, relayState);
 }
  float f1 = tank1.readTemperature(true);
  float f2 = tank2.readTemperature(true);
  float f3 = tank3.readTemperature(true);
  float f4 = tank4.readTemperature(true);
  float f5 = tank5.readTemperature(true);
  float f6 = tank6.readTemperature(true);
  float f7 = tank7.readTemperature(true);
  float f8 = tank8.readTemperature(true);
  float f9 = tank9.readTemperature(true);
  float f10 = tank10.readTemperature(true);
   if (isnan(f1)) 
   if (isnan(f2)) 
   if (isnan(f3)) 
   if (isnan(f4)) 
   if (isnan(f5)) 
   if (isnan(f6)) 
   if (isnan(f7)) 
   if (isnan(f8)) 
   if (isnan(f9)) 
   if (isnan(f10)) {
    return;
  }
 if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
      if (digitalRead(forcespray) == HIGH)
 while (digitalRead(forcespray) == HIGH) {
        digitalWrite(relay, HIGH);
}

else if (digitalRead(forcespray) == LOW)
      digitalWrite(relay, LOW);
  Serial.print("Tank1 Temp");
  Serial.print(f1+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
            if (digitalRead(forcespray) == HIGH)
 while (digitalRead(forcespray) == HIGH) {
        digitalWrite(relay, HIGH);
}

else if (digitalRead(forcespray) == LOW)
      digitalWrite(relay, LOW);
  Serial.print("Tank2 Temp");
  Serial.print(f2+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
            if (digitalRead(forcespray) == HIGH)
 while (digitalRead(forcespray) == HIGH) {
        digitalWrite(relay, HIGH);
}

else if (digitalRead(forcespray) == LOW)
      digitalWrite(relay, LOW);
  Serial.print("Tank3 Temp");
  Serial.print(f3+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
  Serial.print("Tank4 Temp");
  Serial.print(f4+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
  Serial.print("Tank5 Temp");
  Serial.print(f5+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
  Serial.print("Tank6 Temp");
  Serial.print(f6+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
  Serial.print("Tank7 Temp");
  Serial.print(f7+9);
  delay(300);
  if (digitalRead(forcefan) == HIGH)
 while (digitalRead(forcefan) == HIGH) {
        digitalWrite(relay2, LOW);
}

else if (digitalRead(forcefan) == LOW)
      digitalWrite(relay2, HIGH);
  Serial.print("Tank8 Temp");
  Serial.print(f8+9);
  delay(300);
  Serial.print("Tank9 Temp");
  Serial.print(f9+9);
  delay(300);
  Serial.print("Tank10 Temp");
  Serial.print(f10+9);
  delay(300);


  //if ( f9 < 76.00 )
  //digitalWrite( relay, LOW);
  //if ( f9 > 82.00 )
  //digitalWrite( relay, HIGH);
  //if ( h9 > 80.00 )
 // digitalWrite( relay2, HIGH);
  //if ( h9 < 88.00 )
  //digitalWrite( relay2, LOW);
   }

notsolowki:
im not sure what you meant by"// NEW not sure which should start"

Exactly what I said - I did not know which state you want to start in.

In your program you have lots of variables called f1, f2 etc. Using an array would greatly simplify that. And more meaningful variable names would also be wise.

Now that you know how to use millis() why are you using delay()

WHILE is also a blocking concept and should be avoided unless it always completes in a millisec or so. Likewise FOR. Use iF and allow loop() to do the iteration.

Use the AutoFormat tool to indent your code consistently - it will make it much easier to read.

I don't think this will work - you need 'or' s to join them together. And would be much simpler with an array.

   if (isnan(f1))
   if (isnan(f2))
   if (isnan(f3))
   if (isnan(f4))
   if (isnan(f5))
   if (isnan(f6))
   if (isnan(f7))
   if (isnan(f8))
   if (isnan(f9))
   if (isnan(f10)) {
    return;
  }

Avoid float variables if you can because floating point maths is very slow

...R

yea i notice something is getting in the wait of delay times. f1-10 stands for fahrenheit. of tanks 1-10. according to the dht library i have to have some sort of delay between readings or it will be inaccurate. lol like i said i really new to this and i really wish i understood it like you do.

i think this is also where your tutorial is going to come into play. maybe i should find a way to replace all of the delays with millis

Here is part of your code reformatted to show more clearly which lines will be executed if the if tests are true

  if (isnan(f1))
    if (isnan(f2))
      if (isnan(f3))
        if (isnan(f4))
          if (isnan(f5))
            if (isnan(f6))
              if (isnan(f7))
                if (isnan(f8))
                  if (isnan(f9))
                    if (isnan(f10))
                    {
                      return;
                    }

Can you please explain under what condition(s) you want the return; to be executed ?

As Robin has suggested, using arrays will make the code shorter and neater.