millis() after state change

int Enable_output = 0;
int Vi_output = 0;
int Vn_output = 0;
int Up_output = 0;
int Dwn_output = 0;
int Rot_Switch_pos1 = 0;                         //standby mode
int Rot_Switch_pos2 = 0;                         //TIC mode
int Rot_Switch_pos3 = 0;                         //CAN mode
int Rot_Switch_pos4 = 0;                         //Auto mode

long StartTime = 0;
long interval = 120000;



void setup()
{



  /********TIC Switch output pin allocation ***********/
  pinMode(48, OUTPUT);                                      //TIC Enable switch pin 48 = output
  pinMode(47, OUTPUT);                                      //TIC Vi switch pin 47 = output
  pinMode(43, OUTPUT);                                      //TIC Vn switch pin 43 = output
  pinMode(39, OUTPUT);                                      //TIC Dwn switch pin 39 = output
  pinMode(35, OUTPUT);                                      //TIC Up switch pin 35 = output

  /********Rotary Switch input pin allocation ***********/
  pinMode(28, INPUT);                                      //Rotary switch Position 1 pin 28 = input
  pinMode(26, INPUT);                                      //Rotary switch Position 2 pin 26 = input
  pinMode(24, INPUT);                                      //Rotary switch Position 3 pin 24 = input
  pinMode(22, INPUT);                                      //Rotary switch Position 4 pin 22 = input

  pinMode(A10, OUTPUT);                                     //UP LED pin A10 = UP auto mode indicator
  pinMode(A11, OUTPUT);                                     //DWN LED pin A11 = UP auto mode indicator
  pinMode(A12, OUTPUT);                                     //Vi LED pin A12 = UP auto mode indicator
  pinMode(A13, OUTPUT);                                     //Vn LED pin A13 = UP auto mode indicator
  pinMode(A14, OUTPUT);                                     //QSP LED pin A14 = UP auto mode indicator

}
void loop() {
  AUTOmode();
}

void AUTOmode() {

  unsigned long currentTime = millis();
  unsigned long previousTime = currentTime - StartTime;
  int Rot_Switch_pos4 = digitalRead(22);
  if (Rot_Switch_pos4 == HIGH && previousTime > interval)
  {
    StartTime = currentTime;
  }
  if (Rot_Switch_pos4 == HIGH && previousTime < 5000)                           // 0 to 5 second point in the 120 second loop
    // all switch output off, stationary
  {
    digitalWrite(47, LOW);              //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(35, LOW);              //UP switch
    digitalWrite(48, LOW);              //ENABLE switch
    digitalWrite(A12, LOW);             //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, LOW);             //UP LED
    digitalWrite(A11, LOW);             //DWN LED
    digitalWrite(A14, LOW);             //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 20000)  // if between 5 to 20 second point in the 120 second loop
    // Travel up at low speed
  {
    digitalWrite(47, HIGH);             //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(35, HIGH);             //UP switch
    digitalWrite(48, HIGH);             //ENABLE switch
    digitalWrite(A12, HIGH);            //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, HIGH);            //UP LED
    digitalWrite(A11, LOW);             //DWN LED
    digitalWrite(A14, HIGH);            //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 40000) // if between 20 to 40 second point in the 120 second loop
    // Travel up at high speed
  {
    digitalWrite(47, LOW);             //Vi switch
    digitalWrite(43, HIGH);            //Vn switch
    digitalWrite(35, HIGH);            //UP switch
    digitalWrite(48, HIGH);            //ENABLE switch
    digitalWrite(A12, LOW);            //Vi LED
    digitalWrite(A13, HIGH);           //Vn LED
    digitalWrite(A10, HIGH);           //UP LED
    digitalWrite(A11, LOW);            //DWN LED
    digitalWrite(A14, HIGH);           //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 55000)  // if between 40 to 55 second point in the 120 second loop
    // Travel up at low speed
  {
    digitalWrite(47, HIGH);             //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(35, HIGH);             //UP switch
    digitalWrite(48, HIGH);             //ENABLE switch
    digitalWrite(A12, HIGH);            //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, HIGH);            //UP LED
    digitalWrite(A11, LOW);             //DWN LED
    digitalWrite(A14, HIGH);            //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 65000)// if between 55 to 65 second point in the 120 second loop
    // all switch outputs off, stationary
  {
    digitalWrite(47, LOW);              //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(35, LOW);              //UP switch
    digitalWrite(48, LOW);              //ENABLE switch
    digitalWrite(A12, LOW);             //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, LOW);             //UP LED
    digitalWrite(A11, LOW);             //DWN LED
    digitalWrite(A14, LOW);             //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 80000)  // if between 65 to 80 second point in the 120 second loop
    // Travel down at low speed
  {
    digitalWrite(47, HIGH);             //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(39, HIGH);             //DWN switch
    digitalWrite(48, HIGH);             //ENABLE switch
    digitalWrite(A12, HIGH);            //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, LOW);             //UP LED
    digitalWrite(A11, HIGH);            //DWN LED
    digitalWrite(A14, HIGH);            //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 100000) // if between 80 to 100 second point in the 120 second loop
    // Travel down at high speed
  {
    digitalWrite(47, LOW);             //Vi switch
    digitalWrite(43, HIGH);            //Vn switch
    digitalWrite(39, HIGH);            //DWN switch
    digitalWrite(48, HIGH);            //ENABLE switch
    digitalWrite(A12, LOW);            //Vi LED
    digitalWrite(A13, HIGH);           //Vn LED
    digitalWrite(A10, LOW);            //UP LED
    digitalWrite(A11, HIGH);           //DWN LED
    digitalWrite(A14, HIGH);           //ENABLE LED
  }
  else if (Rot_Switch_pos4 == HIGH && previousTime < 115000)  // if between 100 to 115 second point in the 120 second loop
    // Travel down at low speed
  {
    digitalWrite(47, HIGH);             //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(39, HIGH);             //DWN switch
    digitalWrite(48, HIGH);             //ENABLE switch
    digitalWrite(A12, HIGH);            //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, LOW);             //UP LED
    digitalWrite(A11, HIGH);            //DWN LED
    digitalWrite(A14, HIGH);            //ENABLE LED
  }
  else // if between 115 to 120 second point in the 120 second loop
    // all switch outputs off, sationary
  {
    digitalWrite(47, LOW);              //Vi switch
    digitalWrite(43, LOW);              //Vn switch
    digitalWrite(39, LOW);              //DWN switch
    digitalWrite(48, LOW);              //ENABLE switch
    digitalWrite(A12, LOW);             //Vi LED
    digitalWrite(A13, LOW);             //Vn LED
    digitalWrite(A10, LOW);             //UP LED
    digitalWrite(A11, LOW);             //DWN LED
    digitalWrite(A14, LOW);             //ENABLE LED
  }


  if  (Rot_Switch_pos4 == HIGH) {
    digitalWrite(36, HIGH);                        //If Rotary switch is on position 3 = AUTO MODE, Turn on AUTO mode LED indicator
  } else {
    digitalWrite(36, LOW);                       //If Rotary switch is NOT position 3,Turn off AUTO mode LED indicator
  }
}

Obviously the millis() counter starts whenever the arduino powers up, and runs to the interval limit of 120000 millis (120seconds). I would like the millis() to restart at 0, whenever the rotary switch position 4 goes high from a low state, effectively starting the sequence from 0 again.

I would like the 120second loop to restart at 0second point everytime the rotary switch is turned to position 4.

However, as it stands when the rotary switch starts in position 1-3 then goes to 4 the millis() counter starts midway through the 120second loop depending on how long from powerup.

I would like the millis() to restart at 0, whenever the rotary switch position 4 goes high from a low state,

Don't even think about doing that.

Save the value of millis() when the rotary switch position 4 goes high from a low state and do the timing relative to that start time by subtracting the start time from the current time to ascertain whether the required period has elapsed

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.