Need help with sequencing millis(). Code attached

I'm back. I thought I had it worked out, but I was wrong. I understand the need for more than one "pre". My problem is getting my sequence right. I'm not too lazy to figure it out for myself. But after three days, I've run out of ideas. Here's my goal: I want a wine cooler to run for 8 hours continuously for an initial cool down. Then, I want it to cycle off and on for 15 minutes each cycle to conserve energy and reduce noise from fans. And so, I need the cool down with the giant 8 hour interval to fire first. The compiler always starts the short intervals first. Any help will be appreciated.

byte pin8 = 8;
byte pin9 = 9;
byte pin86 = 86;//fake

bool pin8_state = LOW;
bool pin9_state = LOW;

unsigned long pre = 0;
unsigned long pre2 = 0;

void setup()
{
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
}

void loop()
{
  unsigned long cur = millis();

  if ((cur - pre >= 0) && (pin9_state == LOW)) //begin cool down
  {
    pin9_state = HIGH;
    pre = cur;
    digitalWrite(pin9, pin9_state);
  }

  if (cur - pre >= 3000 && pin9_state == HIGH) //end cool down: 28,800,000ms (using 3000 for testing)
  {
    pin9_state = LOW;
    pre = cur;
    digitalWrite(pin9, pin9_state);
    pin9 = pin86;//fake pin
  }

  if (cur - pre2 >= 250 && pin8_state == LOW) //rest cooler for 15 min: 900000 ms (using 250 for testing)
  {
    pin8_state = HIGH;
    pre2 = cur;
    digitalWrite(pin8, pin8_state);
  }

  else if (cur - pre2 >= 250 && pin8_state == HIGH) //run cooler for 15 min: 900000 ms (using 250 for testing)
  {
    pin8_state = LOW;
    pre2 = cur;
    digitalWrite(pin8, pin8_state);
  }

}//end loop

I want a wine cooler to run for 8 hours continuously for an initial cool down. Then, I want it to cycle off and on for 15 minutes each cycle to conserve energy and reduce noise from fans.

What is connected to pin 8 and pin 9?

When I need long time timers I use these Icstation Adjustable Timer Module Time Delay On Off Control Switch Board 10s to 24h (Pack of 5) . I find them the be quite accurate , reliable, and re-tiggerable. I use them to generate interrupts.

larryd:
What is connected to pin 8 and pin 9?

Peltier thermo-electric coolers and fans. In the final wire-up, everything would probably be controlled by one relay (pin8). I should have mentioned that I used two pins in writing the sketch hoping that it might somehow make it easier to put one action (the long cool down) before the other (the 15 minute looping). But no matter what I do, the compiler executes the short interval blocks first. I figger there's got to be some kind of work-around, but maybe not.

Here is a start:

//Version 2

#define controllerON HIGH
#define controllerOFF !controllerON

const byte heartBeatLED = 13;
const byte controller   = 8;  //Peltier thermo-electric coolers and fans.

const byte startSwitch  = 2;  //Switch going to GND, push gives a LOW

unsigned long heartBeatMillis;
unsigned long previousMillis;

const unsigned long coolDownMillis    = 10000;  //testing
//const unsigned long coolDownMillis  = 8 * 60 * 60 * 1000UL;  //8 hours

const unsigned long normalMillis      = 2000;   //testing
//const unsigned long normalMillis    = 15 * 60 * 1000UL;      //15 minutes

enum States {PowerUp, CoolDown, NormalRunning};
States mState = PowerUp;

void setup()
{
  pinMode(heartBeatLED, OUTPUT);  //flashing indicates machine is working

  digitalWrite(controller, controllerOFF);
  pinMode(controller, OUTPUT);

  pinMode(startSwitch, INPUT_PULLUP);

} //END of setup()

void loop()
{
  unsigned long currentMillis = millis();

  //********************************************
  if (currentMillis - heartBeatMillis >= 1000)
  {
    heartBeatMillis = currentMillis;
    //toggle heart beat LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //********************************************
  //if the start switch is pressed, the machine is reset
  if (digitalRead(startSwitch) == LOW)
  {
    //initialize timer
    previousMillis = currentMillis;

    //put machine in cool down mode
    mState = CoolDown;

    digitalWrite(controller, controllerON);
  }

  //********************************************
  if (mState == CoolDown && currentMillis - previousMillis >= coolDownMillis)
  {
    digitalWrite(controller, controllerOFF);

    //initialize timer
    previousMillis = currentMillis;

    //put machine in normal mode
    mState = NormalRunning;

  }

  //********************************************
  if (mState == NormalRunning && currentMillis - previousMillis >= normalMillis)
  {
    //re-initialize timer
    previousMillis = currentMillis;
    //toggle cooling
    digitalWrite(controller, !digitalRead(controller));
  }


} //END of loop()

Rather than the fake pin, you can use a boolean control variable. I called it initialCoolComplete.

byte pin8 = 8;
byte pin9 = 9;
//byte pin86 = 86;//fake

bool pin8_state = LOW;
bool pin9_state = LOW;

unsigned long pre = 0;
unsigned long pre2 = 0;

boolean initialCoolComplete = false;

void setup()
{
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
}

void loop()
{
  unsigned long cur = millis();

  //if ((cur - pre >= 0) && (pin9_state == LOW)) //begin cool down
  if (pin9_state == LOW && !initialCoolComplete)
  {
    pin9_state = HIGH;
    pre = cur;
    digitalWrite(pin9, pin9_state);
  }

  if (cur - pre >= 3000 && pin9_state == HIGH) //end cool down: 28,800,000ms (using 3000 for testing)
  {
    pin9_state = LOW;
    pre = cur;
    digitalWrite(pin9, pin9_state);
    //pin9 = pin86;//fake pin
    initialCoolComplete = true;
  }

  if (initialCoolComplete == true)
  {
    if (cur - pre2 >= 250 && pin8_state == LOW) //rest cooler for 15 min: 900000 ms (using 250 for testing)
    {
      pin8_state = HIGH;
      pre2 = cur;
      digitalWrite(pin8, pin8_state);
    }

    else if (cur - pre2 >= 250 && pin8_state == HIGH) //run cooler for 15 min: 900000 ms (using 250 for testing)
    {
      pin8_state = LOW;
      pre2 = cur;
      digitalWrite(pin8, pin8_state);
    }
  }
}//end loop

Brattain Member: I really apprecicate the code. The syntax is quite advanced compared to what I'm used to. I will keep it and learn what all of it means. Thank you very much.

To cattledog: THANK YOU! Love it. Precisely what I needed and familiar looking stuff to this noob. Especially like the boolean. I owe you a beer!

If you have questions, ask for an explanation.