Stange error

I have a project with 2 buttons UP/DOWN, The up part is working fine every time but when I press the down button it should count for 10 seconds then stop but once I have pressed the Lift button and it goes through it's cycle and stops, I then press the down button and within 1-2 seconds it stops, Then if I press the down button again it counts 10 seconds correctly, I've tired various methods but cannot seem to find where its going wrong, This it the serial Data that I send out the lines with these **** is added by me just to show

 STORED VALUES 
 *****....***** 
5
50
40
10000
lifting
Duty =5
Duty =6
Duty =7
Duty =8
Duty =9
Duty =10
Duty =11
Duty =12
Duty =13
Duty =14
Duty =15
Duty =16
Duty =17
Duty =18
Duty =19
Duty =20
Duty =21
Duty =22
Duty =23
Duty =24
Duty =25
Duty =26
Duty =27
STAGE 1 COMPLETE SLOWING DOWN
LIFTING COMPLETED  ****First stage completed coreetcly
LOWERING           ****First press To lower
LOWERING COMPLETED  *****this should be 10 seconds tmier but within 2 seconds it stops(working in correlty)
LOWERING            ****second press To lower
LOWERING COMPLETED
  *****this counts 10 seconds then it stops(working correctly)

This is the void routine

void  LIFTING_DOWN ()
{
  while (true)
  {
      // Serial.println(currentMillis);
 unsigned long currentMillis = millis();
    //delay(100);
    //'######## CHECKS IF THE GATE IS OPEN ###########
    if ((DWN_FLAG = 1) && (digitalRead(Gate_limitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      DWN_FLAG =  true;
      while (digitalRead(Gate_limitpin) == LOW) {
        digitalWrite(Motor_enable, LOW);
        digitalWrite(LIFT_DWN_COIL, LOW);
        digitalWrite(LIFT_UP_COIL, LOW);
        Serial.println("GATE OPEN, CLOSE GATE");
      }
      return;
    }
    //####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
    
    if (DWN_FLAG == 1 && (currentMillis - previousMillis >= interval)) {
       previousMillis = currentMillis;
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      Serial.println("LOWERING COMPLETED");
     // currentMillis = 0;
      return;
    }
    
  }
}

This is the other part for down

  // #### TURNS ON THE PUMP AND LIFT COIL FOR .3 SECONDS TO PREVENT INTAIL DROP OF PLATFORM #################
  if (digitalRead(DwnbuttonPin) == LOW) {
    delay(50);
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    digitalWrite(LIFT_DWN_COIL, HIGH);
    UP_duty = 50;
    Duty = UP_duty;
    pwmWrite(9, Duty);
    delay(200);
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Duty = 0;
    pwmWrite(9, Duty);
    DWN_FLAG = 1;
   // currentMillis = 0;
    
    Serial.println("LOWERING");
    LIFTING_DOWN ();
  }

I'm guessing that you defined interval as 10000(milliseconds) elsewhere but that's only a guess. Please post the complete program. If it is too large, then strip it down to the smallest possible program which still exhibits the error.

A guess, as you haven't shown all your code: You need to reset previousMillis when you hit the Down button, as it's probably still holding the time when the Up procedure ended.
I like to call previousMillis "startTime" as it makes its purpose clearer.

    if ((DWN_FLAG = 1) && (digitalRead(Gate_limitpin) == LOW)) {

Why are you using = in one place and == in the other? Clearly, only one of them is correct.

Yes interval is defined as unsigned long interval= 10000;
Even if I don't go for lift and press down first it does the same I've also tried currentmills =0 just before it jumps to the down routine
Thanks Paul I will check that I thought I changed that to == but will check that out as that was working when I activated the limit switch, still trying to get use to the syntax code.
I'm away from the computer at the moment but will another look and post code I will also try and break it down just lift down
It's just strange that second third n forth time it works as expected
Thanks for the input guys

I've stripped out lot's of code to bare minimum and still get the same error, Running it debug on simulator I can see what's happening, attached is some screen shots, I set Startmillis to 0 once button pressed but once it jumps startmillis now holds the value since it has been running so previousmillis holds the startmillis, this is the reason why it stops counting and ends the lift_down routines.

const int Gate_limitpin = 11;
const int Max_hieghtlimitpin = 10;
const int Motor_enable = 2;
const int upbuttonPin = 13;
const int DwnbuttonPin = 12;
const int LIFT_DWN_COIL = 3;
const int LIFT_UP_COIL = 4;
const int HEIGHT_REACHED = 8;
boolean UP_FLAG = false;
boolean DWN_FLAG = false;
unsigned long StartMillis = millis();
//unsigned long StartMillis = millis();
int UP_duty = 50;
int Duty  =90;
int Duty_SLOW1=80;
int Duty_MAX=100;
unsigned long interval =10000;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LIFT_UP_COIL, OUTPUT);
  pinMode(LIFT_DWN_COIL, OUTPUT);
  pinMode(upbuttonPin, INPUT);
  digitalWrite(upbuttonPin, HIGH);
  pinMode(DwnbuttonPin, INPUT);
  digitalWrite(DwnbuttonPin, HIGH);
  pinMode(HEIGHT_REACHED, OUTPUT);
  pinMode( Gate_limitpin, INPUT);
  digitalWrite(Gate_limitpin, HIGH);
  pinMode(Max_hieghtlimitpin, INPUT);
  digitalWrite(Max_hieghtlimitpin, HIGH);
  pinMode(Motor_enable, OUTPUT);
  pinMode(HEIGHT_REACHED, INPUT);
  digitalWrite(HEIGHT_REACHED, HIGH);
  Serial.println(" STORED VALUES ");

    Serial.println(" *****....***** ");
    Serial.println(Duty);
    Serial.println(Duty_MAX);
    Serial.println(Duty_SLOW1);
    Serial.println(interval);
    Serial.flush();
    }


void loop() {
  if ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Serial.println("Gate open or max hieght limit switch active");
    LOCKED ();
  }
  if ((digitalRead(upbuttonPin) == LOW) && (digitalRead(HEIGHT_REACHED) == HIGH)) {
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    UP_FLAG = 1;
    Serial.println("lifting");
    LIFTING_UP ();
  }
  // #### TURNS ON THE PUMP AND LIFT COIL FOR .3 SECONDS TO PREVENT INTAIL DROP OF PLATFORM #################
  if (digitalRead(DwnbuttonPin) == LOW) {
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    digitalWrite(LIFT_DWN_COIL, HIGH);
    UP_duty = 50;
    Duty = UP_duty;
    delay(200);
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Duty = 0;
    DWN_FLAG = 1;
    StartMillis = 0;
    previousMillis = 0;
    Serial.println("LOWERING");
    LIFTING_DOWN ();
  }


}
void LIFTING_UP ()
{
  while (true)
  {
    Serial.print("Duty =");
    Serial.println(Duty);
  
    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
    if (digitalRead(Gate_limitpin) == LOW)
    {
      Serial.println( "GATE OPEN, CLOSE GATE");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      // digitalWrite(UP_FLAG, LOW);
      // digitalWrite(DWN_FLAG, LOW);
      return;
    }

    // ##### FAIL SAFE LIMIT SWITCH #########

    if (digitalRead(Max_hieghtlimitpin) == LOW) {
      Serial.println( "FAIL SAFE LIMIT SWITCH ACTIVATED");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      digitalWrite(UP_FLAG, LOW);
      digitalWrite(DWN_FLAG, LOW);
      return;
    }
    // #### SETS THE LIFT SPEED #################

    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == HIGH)) {
      Duty++;
      delay(100);
      if (Duty > Duty_MAX) {  // SETS MAX LIFT SPEED;
        Duty = Duty_MAX;
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW))   {
      Duty--;
      delay(50);
      if (Duty < Duty_SLOW1)  {
        Duty = Duty_SLOW1;
        Serial.println( "STAGE 1 COMPLETE SLOWING DOWN");
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####

    if  (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW  && Duty < Duty_SLOW1 + 5)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println( "LIFTING COMPLETED");
      Duty = 5;          //make sure the mosfets are fully off on power up
      
      return;
    }

  }  // end of while

} // end of LIFTING_UP
// #### IF LIMIT SWITCHES ARE ACTIVE STOP ALL FUNCTIONS #################
void LOCKED ()
{
  while (true)
  {
    while ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println("GATE OPEN, CLOSE GATE");
    }
    return;
  }
}
// #### LOWER THE PLATFORM IF LIMIT SWITCHES ARE GOOD #################
void  LIFTING_DOWN ()
{
  while (true)
  {
      // Serial.println(StartMillis);
  
  // previousMillis = StartMillis;
    //delay(100);
    //'######## CHECKS IF THE GATE IS OPEN ###########
    if ((DWN_FLAG == 1) && (digitalRead(Gate_limitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      DWN_FLAG =  true;
      while (digitalRead(Gate_limitpin) == LOW) {
        digitalWrite(Motor_enable, LOW);
        digitalWrite(LIFT_DWN_COIL, LOW);
        digitalWrite(LIFT_UP_COIL, LOW);
        Serial.println("GATE OPEN, CLOSE GATE");
      }
      return;
    }
    //####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
    StartMillis = millis();
    if (DWN_FLAG == 1 && (StartMillis - previousMillis >= interval)) {
      previousMillis = StartMillis;
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      Serial.println("LOWERING COMPLETED");
     // StartMillis = 0;
      return;
    }
    
  }
}

What does StartMillis mean? Nothing. Using meaningful names will make things easier. Something like liftStartedDown, maybe.

What does previousMillis mean? Nothing.

What does interval mean? Nothing.

I suspect that if you use meaningful names for these three variables, it will become obvious what the problem is.

PaulS:
I suspect that if you use meaningful names for these three variables, it will become obvious what the problem is.

I knew what the values meant but like you said, may be for others makes it harder to follow or not understanding the meaning, I've changed the variables
StartMillis to Liftdownstarted
previousMillis to LiftdownpreviousMillis
interval to Liftdowntime
Like you said it may be staring me in the face what the error/issue is but I can't still see what the issue could be, I did try to add debounce to the buttons but still the same issue so I did not re-add it, May be I've tried to many times and lost my way it

const int Gate_limitpin = 11;
const int Max_hieghtlimitpin = 10;
const int Motor_enable = 2;
const int upbuttonPin = 13;
const int DwnbuttonPin = 12;
const int LIFT_DWN_COIL = 3;
const int LIFT_UP_COIL = 4;
const int HEIGHT_REACHED = 8;
boolean UP_FLAG = false;
boolean DWN_FLAG = false;
unsigned long Liftdownstarted = millis();
//unsigned long Liftdownstarted = millis();
int UP_duty = 50;
int Duty  =90;
int Duty_SLOW1=80;
int Duty_MAX=100;
unsigned long Liftdowntime =10000;
unsigned long LiftdownpreviousMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LIFT_UP_COIL, OUTPUT);
  pinMode(LIFT_DWN_COIL, OUTPUT);
  pinMode(upbuttonPin, INPUT);
  digitalWrite(upbuttonPin, HIGH);
  pinMode(DwnbuttonPin, INPUT);
  digitalWrite(DwnbuttonPin, HIGH);
  pinMode(HEIGHT_REACHED, OUTPUT);
  pinMode( Gate_limitpin, INPUT);
  digitalWrite(Gate_limitpin, HIGH);
  pinMode(Max_hieghtlimitpin, INPUT);
  digitalWrite(Max_hieghtlimitpin, HIGH);
  pinMode(Motor_enable, OUTPUT);
  pinMode(HEIGHT_REACHED, INPUT);
  digitalWrite(HEIGHT_REACHED, HIGH);
  Serial.println(" STORED VALUES ");

    Serial.println(" *****....***** ");
    Serial.println(Duty);
    Serial.println(Duty_MAX);
    Serial.println(Duty_SLOW1);
    Serial.println(Liftdowntime);
    Serial.flush();
    }


void loop() {
  if ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Serial.println("Gate open or max hieght limit switch active");
    LOCKED ();
  }
  if ((digitalRead(upbuttonPin) == LOW) && (digitalRead(HEIGHT_REACHED) == HIGH)) {
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    UP_FLAG = 1;
    Serial.println("lifting");
    LIFTING_UP ();
  }
  // #### TURNS ON THE PUMP AND LIFT COIL FOR .3 SECONDS TO PREVENT INTAIL DROP OF PLATFORM #################
  if (digitalRead(DwnbuttonPin) == LOW) {
    delay(50);
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    digitalWrite(LIFT_DWN_COIL, HIGH);
    UP_duty = 50;
    Duty = UP_duty;
    delay(200);
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Duty = 0;
    DWN_FLAG = 1;
    Liftdownstarted = 0;
   LiftdownpreviousMillis = 0;
    
    Serial.println("LOWERING");
    LIFTING_DOWN ();
  }


}
void LIFTING_UP ()
{
  while (true)
  {
    Serial.print("Duty =");
    Serial.println(Duty);
  
    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
    if (digitalRead(Gate_limitpin) == LOW)
    {
      Serial.println( "GATE OPEN, CLOSE GATE");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      // digitalWrite(UP_FLAG, LOW);
      // digitalWrite(DWN_FLAG, LOW);
      return;
    }

    // ##### FAIL SAFE LIMIT SWITCH #########

    if (digitalRead(Max_hieghtlimitpin) == LOW) {
      Serial.println( "FAIL SAFE LIMIT SWITCH ACTIVATED");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      digitalWrite(UP_FLAG, LOW);
      digitalWrite(DWN_FLAG, LOW);
      return;
    }
    // #### SETS THE LIFT SPEED #################

    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == HIGH)) {
      Duty++;
      delay(100);
      if (Duty > Duty_MAX) {  // SETS MAX LIFT SPEED;
        Duty = Duty_MAX;
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW))   {
      Duty--;
      delay(50);
      if (Duty < Duty_SLOW1)  {
        Duty = Duty_SLOW1;
        Serial.println( "STAGE 1 COMPLETE SLOWING DOWN");
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####

    if  (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW  && Duty < Duty_SLOW1 + 5)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println( "LIFTING COMPLETED");
      Duty = 5;          //make sure the mosfets are fully off on power up
      
      return;
    }

  }  // end of while

} // end of LIFTING_UP
// #### IF LIMIT SWITCHES ARE ACTIVE STOP ALL FUNCTIONS #################
void LOCKED ()
{
  while (true)
  {
    while ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println("GATE OPEN, CLOSE GATE");
    }
    return;
  }
}
// #### LOWER THE PLATFORM IF LIMIT SWITCHES ARE GOOD #################
void  LIFTING_DOWN ()
{
  while (true)
  {
      // Serial.println(Liftdownstarted);
  
  // LiftdownpreviousMillis = Liftdownstarted;
    //delay(100);
    //'######## CHECKS IF THE GATE IS OPEN ###########
    if ((DWN_FLAG == 1) && (digitalRead(Gate_limitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      DWN_FLAG =  true;
      while (digitalRead(Gate_limitpin) == LOW) {
        digitalWrite(Motor_enable, LOW);
        digitalWrite(LIFT_DWN_COIL, LOW);
        digitalWrite(LIFT_UP_COIL, LOW);
        Serial.println("GATE OPEN, CLOSE GATE");
      }
      return;
    }
    //####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
    Liftdownstarted = millis();  //Liftdownstarted = currentmillis
    if ((DWN_FLAG == 1) && (Liftdownstarted - LiftdownpreviousMillis >= Liftdowntime)) { // Is liftstarted - liftdownpreviousmillis more than 10 seocnds stop lifdown
      LiftdownpreviousMillis = Liftdownstarted; // make lifdownstarted = liftdownpreviuosmillis 
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      Serial.println("LOWERING COMPLETED");
      Liftdownstarted = 0; //again to make sure reset lifdownstarted 
      return;
    }
    
  }
}

I set Startmillis to 0

Why ? Startmillis (or your new name for it) should surely be set to millis() so that it can be compared with millis() later to determine how much time has passed.

UKHeliBob:
Why ? Startmillis (or your new name for it) should surely be set to millis() so that it can be compared with millis() later to determine how much time has passed.

I've even removed those lines and just made sure and still the same, If I go through a lift up cycle and then down is comes up with straight away, This result seems to be hit and miss, Again results from serial data

STORED VALUES 
 *****....***** 
100
50
90
10000
LOWERING ***** No lift just pressed down
LOWERING COMPLETED ***** straight away with a second or 2 
LOWERING       ***** second press
LOWERING COMPLETED ***** 10 seconds pass (CORRECT)
LOWERING           ****** third press 
LOWERING COMPLETED ***** straight away with a second or 2 
lifting  *********try lifting
Duty =0
Duty =1
Duty =2
Duty =3
Duty =4
Duty =5
Duty =6
Duty =7
Duty =8
Duty =9
Duty =10
Duty =11
Duty =12
Duty =13
Duty =14
Duty =15
Duty =16
STAGE 1 COMPLETE SLOWING DOWN
LIFTING COMPLETED 
LOWERING   ***** presed for first down after lift
LOWERING COMPLETED ***** straight away with a second or 2 
LOWERING  ***********pressed second time 
LOWERING COMPLETED ***** straight away with a second or 2 
LOWERING  ***********pressed third time 
LOWERING COMPLETED
 ***** 10 seconds pass (CORRECT)
    Liftdownstarted = 0;

From your code in reply #7
If that is not your latest code then please post it here.

Latest code with Liftdownstarted removed still same errors as stated above hit and miss results

const int Gate_limitpin = 11;
const int Max_hieghtlimitpin = 10;
const int Motor_enable = 2;
const int upbuttonPin = 13;
const int DwnbuttonPin = 12;
const int LIFT_DWN_COIL = 3;
const int LIFT_UP_COIL = 4;
const int HEIGHT_REACHED = 8;
boolean UP_FLAG = false;
boolean DWN_FLAG = false;
unsigned long Liftdownstarted = millis();
//unsigned long Liftdownstarted = millis();
int UP_duty = 50;
int Duty  =90;
int Duty_SLOW1=80;
int Duty_MAX=100;
unsigned long Liftdowntime =10000;
unsigned long LiftdownpreviousMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LIFT_UP_COIL, OUTPUT);
  pinMode(LIFT_DWN_COIL, OUTPUT);
  pinMode(upbuttonPin, INPUT);
  digitalWrite(upbuttonPin, HIGH);
  pinMode(DwnbuttonPin, INPUT);
  digitalWrite(DwnbuttonPin, HIGH);
  pinMode( Gate_limitpin, INPUT);
  digitalWrite(Gate_limitpin, HIGH);
  pinMode(Max_hieghtlimitpin, INPUT);
  digitalWrite(Max_hieghtlimitpin, HIGH);
  pinMode(Motor_enable, OUTPUT);
  pinMode(HEIGHT_REACHED, INPUT);
  digitalWrite(HEIGHT_REACHED, HIGH);
  Serial.println(" STORED VALUES ");

    Serial.println(" *****....***** ");
    Serial.println(Duty);
    Serial.println(Duty_MAX);
    Serial.println(Duty_SLOW1);
    Serial.println(Liftdowntime);
    Serial.flush();
    }


void loop() {
  if ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Serial.println("Gate open or max hieght limit switch active");
    LOCKED ();
  }
  if ((digitalRead(upbuttonPin) == LOW) && (digitalRead(HEIGHT_REACHED) == HIGH)) {
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    UP_FLAG = 1;
    Serial.println("lifting");
    LIFTING_UP ();
  }
  // #### TURNS ON THE PUMP AND LIFT COIL FOR .3 SECONDS TO PREVENT INTAIL DROP OF PLATFORM #################
  if (digitalRead(DwnbuttonPin) == LOW) {
    delay(50);
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    digitalWrite(LIFT_DWN_COIL, HIGH);
    UP_duty = 50;
    Duty = UP_duty;
    delay(200);
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Duty = 0;
   DWN_FLAG = 1;
  
    Serial.println("LOWERING");
    LIFTING_DOWN ();
  }


}
void LIFTING_UP ()
{
  while (true)
  {
    Serial.print("Duty =");
    Serial.println(Duty);
  
    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
    if (digitalRead(Gate_limitpin) == LOW)
    {
      Serial.println( "GATE OPEN, CLOSE GATE");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      // digitalWrite(UP_FLAG, LOW);
      // digitalWrite(DWN_FLAG, LOW);
      return;
    }

    // ##### FAIL SAFE LIMIT SWITCH #########

    if (digitalRead(Max_hieghtlimitpin) == LOW) {
      Serial.println( "FAIL SAFE LIMIT SWITCH ACTIVATED");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      digitalWrite(UP_FLAG, LOW);
      digitalWrite(DWN_FLAG, LOW);
      return;
    }
    // #### SETS THE LIFT SPEED #################

    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == HIGH)) {
      Duty++;
      delay(100);
      if (Duty > Duty_MAX) {  // SETS MAX LIFT SPEED;
        Duty = Duty_MAX;
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW))   {
      Duty--;
      delay(50);
      if (Duty < Duty_SLOW1)  {
        Duty = Duty_SLOW1;
        Serial.println( "STAGE 1 COMPLETE SLOWING DOWN");
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####

    if  (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW  && Duty < Duty_SLOW1 + 5)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println( "LIFTING COMPLETED");
      Duty = 5;          //make sure the mosfets are fully off on power up
      
      return;
    }

  }  // end of while

} // end of LIFTING_UP
// #### IF LIMIT SWITCHES ARE ACTIVE STOP ALL FUNCTIONS #################
void LOCKED ()
{
  while (true)
  {
    while ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println("GATE OPEN, CLOSE GATE");
    }
    return;
  }
}
// #### LOWER THE PLATFORM IF LIMIT SWITCHES ARE GOOD #################
void  LIFTING_DOWN ()
{
  while (true)
  {
     
  
  
    //'######## CHECKS IF THE GATE IS OPEN ###########
    if ((DWN_FLAG == 1) && (digitalRead(Gate_limitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      DWN_FLAG =  true;
      while (digitalRead(Gate_limitpin) == LOW) {
        digitalWrite(Motor_enable, LOW);
        digitalWrite(LIFT_DWN_COIL, LOW);
        digitalWrite(LIFT_UP_COIL, LOW);
        Serial.println("GATE OPEN, CLOSE GATE");
      }
      return;
    }
    //####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
    Liftdownstarted = millis();  //Liftdownstarted = currentmillis
    if ((DWN_FLAG == 1) && (Liftdownstarted - LiftdownpreviousMillis > Liftdowntime)) { // Is liftstarted - liftdownpreviousmillis more than 10 seocnds stop lifdown
      LiftdownpreviousMillis = Liftdownstarted; // make lifdownstarted = liftdownpreviuosmillis 
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      Serial.println("LOWERING COMPLETED");
            return;
    }
    
  }
}

    Liftdownstarted = millis();  //Liftdownstarted = currentmillisYou are doing this over and over, not just when the lift starts moving down.

Personally I would change the program into a state machine using switch/case to control which code is executed each time through loop() based on the lift state.

Helibob
Has I'm learning I got the idea from the example blink with out delay, The idea is press lift up once you can do nothing else until the lift has stopped, Then after 5-10minutes you press lift down and you can't do nothing until the lift is lowered fully, Now 10 seconds is only for testing it could be 1 minute or more depending how fast it is set up to come down, Any other case in between for emergency the operator can press the E-Stop button which cuts all power. it does not move very fast while lifting. This is some code I've converted from basic where it all works correctly but would like to change the controller to Arduino

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald
 
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

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

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

I understand where you got the idea from.

Let's start with the lift stopped at the top.
If the door is not closed do not act on any button presses.
If the down button becomes pressed and the door is closed start the lift moving down.
Each time through loop() check whether the lift has reached its destination. If so, stop the lift.

Why do you need to time anything ?

UKHeliBob:
I understand where you got the idea from.

Let's start with the lift stopped at the top.
If the door is not closed do not act on any button presses.
If the down button becomes pressed and the door is closed start the lift moving down.
Each time through loop() check whether the lift has reached its destination. If so, stop the lift.

Why do you need to time anything ?

There are 2 limit switches at the top one is fail safe which is set 6" above the height limit switch,if height reached failes so it stops lift. Once at the top if up button is pressed nothing happens. The idea is once height reached it slows the lift down to a gradual stop at this point nothing should happen if height is reached and the up button is pressed again then the only way is down, I went for timed down function so don't after add extra limit switch/switches to say when it's down plus the design layout of the unit there is no where to to actually to mount a limit switch at the base, There is no timing up only down. There is other bits of code but only turn led on/off while a function is active. Same again once the lift is lowering the up/down buttons do nothing while lowering until lift down is timed out. It's all working perfectly apart from timing down I seem to get to 5 seconds. Is the another method of doing it to come out of loop and not going back until timing finished ?

If you really must use timing for the lift moving down then save the time from millis() when the lift starts moving down and each time through loop() check whether the required period has elapsed by subtracting the start time from the current time. Classic BWOD. Do not change the start time once the lift is moving,

start of loop()
  if the down button becomes pressed and the lift is at the top
    save the start time from millis()
    set a flag to indicate that the lift is moving
  end if

  if the lift is moving and millis() - start time is greater than the required period
    stop the lift
    clear flag to indicate that the lift has stopped
  end if
end of loop()

This is driving me insane, I've tried another approach after many other ways and now completely lost
Latest version of code below, What happens if I press down button first it counts 10 seconds then stops(which is correct see first press1), Then if I press it again 5 seconds later it only counts 4450 miilis(4.59 seconds)See second press1 and look at the variables at bottom, Now If I reset it go for lift first, the lift stop's because height reached is active (correctly working), then press down and instantly it stops the lift down and sends out LOWERING, LOWERING COMPLETED on serial port, I've not got a clue what's going on or happening :o :confused: :confused:

const int Gate_limitpin = 11;
const int Max_hieghtlimitpin = 10;
const int Motor_enable = 2;
const int upbuttonPin = 13;
const int DwnbuttonPin = 12;
const int LIFT_DWN_COIL = 3;
const int LIFT_UP_COIL = 4;
const int HEIGHT_REACHED = 8;
boolean UP_FLAG = false;
boolean DWN_FLAG = false;
boolean Lift_state=0;
unsigned long Liftdownstarted = millis();
//unsigned long Liftdownstarted = millis();
int UP_duty = 50;
int Duty  =90;
int Duty_SLOW1=80;
int Duty_MAX=100;
const long Liftdowntime =10000;
unsigned long LiftdownpreviousMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LIFT_UP_COIL, OUTPUT);
  pinMode(LIFT_DWN_COIL, OUTPUT);
  pinMode(upbuttonPin, INPUT);
  digitalWrite(upbuttonPin, HIGH);
  pinMode(DwnbuttonPin, INPUT);
  digitalWrite(DwnbuttonPin, HIGH);
  pinMode( Gate_limitpin, INPUT);
  digitalWrite(Gate_limitpin, HIGH);
  pinMode(Max_hieghtlimitpin, INPUT);
  digitalWrite(Max_hieghtlimitpin, HIGH);
  pinMode(Motor_enable, OUTPUT);
  pinMode(HEIGHT_REACHED, INPUT);
  digitalWrite(HEIGHT_REACHED, HIGH);
  Serial.println(" STORED VALUES ");

    Serial.println(" *****....***** ");
    Serial.println(Duty);
    Serial.println(Duty_MAX);
    Serial.println(Duty_SLOW1);
    Serial.println(Liftdowntime);
    Serial.flush();
    }


void loop() {
  if ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Serial.println("Gate open or max hieght limit switch active");
    LOCKED ();
  }
  if ((digitalRead(upbuttonPin) == LOW) && (digitalRead(HEIGHT_REACHED) == HIGH)) {
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    UP_FLAG = 1;
    Serial.println("lifting");
    LIFTING_UP ();
  }
  // #### TURNS ON THE PUMP AND LIFT COIL FOR .3 SECONDS TO PREVENT INTAIL DROP OF PLATFORM #################
  if (digitalRead(DwnbuttonPin) == LOW) {
    delay(50);
    digitalWrite(Motor_enable, HIGH);
    digitalWrite(LIFT_UP_COIL, HIGH);
    digitalWrite(LIFT_DWN_COIL, HIGH);
    UP_duty = 50;
    Duty = UP_duty;
    delay(200);
    digitalWrite(Motor_enable, LOW);
    digitalWrite(LIFT_UP_COIL, LOW);
    Duty = 0;
   DWN_FLAG = 1;
   Liftdownstarted = millis();  //Liftdownstarted = currentmillis
    Serial.println("LOWERING");
    LIFTING_DOWN ();
  }


}
void LIFTING_UP ()
{
  while (true)
  {
    Serial.print("Duty =");
    Serial.println(Duty);
  
    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
    if (digitalRead(Gate_limitpin) == LOW)
    {
      Serial.println( "GATE OPEN, CLOSE GATE");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      // digitalWrite(UP_FLAG, LOW);
      // digitalWrite(DWN_FLAG, LOW);
      return;
    }

    // ##### FAIL SAFE LIMIT SWITCH #########

    if (digitalRead(Max_hieghtlimitpin) == LOW) {
      Serial.println( "FAIL SAFE LIMIT SWITCH ACTIVATED");
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      digitalWrite(UP_FLAG, LOW);
      digitalWrite(DWN_FLAG, LOW);
      return;
    }
    // #### SETS THE LIFT SPEED #################

    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == HIGH)) {
      Duty++;
      delay(100);
      if (Duty > Duty_MAX) {  // SETS MAX LIFT SPEED;
        Duty = Duty_MAX;
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
    if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW))   {
      Duty--;
      delay(50);
      if (Duty < Duty_SLOW1)  {
        Duty = Duty_SLOW1;
        Serial.println( "STAGE 1 COMPLETE SLOWING DOWN");
      }
    }

    // ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####

    if  (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW  && Duty < Duty_SLOW1 + 5)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println( "LIFTING COMPLETED");
      Duty = 5;          //make sure the mosfets are fully off on power up
      
      return;
    }

  }  // end of while

} // end of LIFTING_UP
// #### IF LIMIT SWITCHES ARE ACTIVE STOP ALL FUNCTIONS #################

void LOCKED ()
{
  while (true)
  {
    while ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      Serial.println("GATE OPEN, CLOSE GATE");
    }
    return;
  }
}
// #### LOWER THE PLATFORM IF LIMIT SWITCHES ARE GOOD #################
void  LIFTING_DOWN (){
  while (DWN_FLAG)  {
      //'######## CHECKS IF THE GATE IS OPEN ###########
    if ((DWN_FLAG = 1) && (digitalRead(Gate_limitpin) == LOW)) {
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      digitalWrite(LIFT_UP_COIL, LOW);
      DWN_FLAG =  0;
      while (digitalRead(Gate_limitpin) == LOW) {
        digitalWrite(Motor_enable, LOW);
        digitalWrite(LIFT_DWN_COIL, LOW);
        digitalWrite(LIFT_UP_COIL, LOW);
        Serial.println("GATE OPEN, CLOSE GATE");
      }
      return;
    }
    //####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
    unsigned long Liftdownstarted = millis();
    if (Liftdownstarted - LiftdownpreviousMillis >= Liftdowntime) {
       LiftdownpreviousMillis = Liftdownstarted;
       delay(50);
         if (Lift_state == 0)
      Lift_state = 1;
   else
   Lift_state = 0;
    if ((DWN_FLAG == 1) && (Lift_state == 1))
      digitalWrite(Motor_enable, LOW);
      digitalWrite(LIFT_DWN_COIL, LOW);
      Serial.println("LOWERING COMPLETED");
    DWN_FLAG =  0;
    
    return;
    }
  }
     
  }

It seems to me that you are making things more complicated that necessary. I find it impossible to follow your code but I did notice that you have 2 different variables named Liftdownstarted so as far as I am concerned all bets are off.

   unsigned long Liftdownstarted = millis();

in the LIFTING_DOWN() function and

unsigned long Liftdownstarted = millis();

as a global variable.

I suggest that you write a simple test program to read a switch input and light an LED for a period when the switch becomes activated then integrate the principles into your program.

UKHeliBob:
It seems to me that you are making things more complicated that necessary. I find it impossible to follow your code but I did notice that you have 2 different variables named Liftdownstarted so as far as I am concerned all bets are off.

   unsigned long Liftdownstarted = millis();

in the LIFTING_DOWN() function and

unsigned long Liftdownstarted = millis();

as a global variable.

I suggest that you write a simple test program to read a switch input and light an LED for a period when the switch becomes activated then integrate the principles into your program.

I've taken one out but still exactly same results (one in at the top), This is where I keep getting lost and trying different methods and forget to take them back out, I thought this was a simple test program it's the cut down version of my main program. Little guidance would be good, I set DWN_FLAG as a flag to say button pressed then set another flag Lift_down flag but still the same thing. Remember I'm still fairly new to this syntax. IF there is an easy way then example would be good I don't expect others to write all the code. From your bit of code above is what I thought I'd followed ??
Thanks for taking the time to reply,I will see if I can get just a simple single button press turning LED on/off with millis