Millis() inside of a state machine

setup:
input: single button that turns on the device (button), a top switch (SwitchTopPin) and a bottom switch (SwitchBottomPin)
output: a PWM controlled motor that raises the table (PWMPin), a motor direction control (PWMDirPin) and an angle leveling motor thats controlled via relays ( AngleMotorUpPin and AngleMotorDownPin)

only on the upward motion of the table (void startMovingUp) I would like to add a delay until the Angle Motors kick in.

help appreciated

#include <ezButton.h>

#define DEBOUNCE_TIME 100

ezButton buttonPin(4);
ezButton SwitchTopPin(23);
ezButton SwitchBottomPin(25);

  const int PWMPin =  13;    // the number of the PWM pin
  const int PWMDirPin = 18; // PWM direction
  const int AngleMotorUpPin = 19; // UP angle motor
  const int AngleMotorDownPin = 21; // Down angle motor
  const int LocksPin = 22; // Lock Relay
  unsigned long AngleStartMillis;
  unsigned long currentMillis;
  const unsigned long AngleWait = 60000;
//  const unsigned long AngleOperate = 60000;


enum State { IDLE, AT_TOP, AT_BOTTOM, MOVING_UP, MOVING_DOWN };


State tableState = IDLE;

void startMovingUp() {
  Serial.println("Moving UP");
  tableState = MOVING_UP;
  digitalWrite(LocksPin, LOW);
  digitalWrite(PWMDirPin, LOW);
  analogWrite(PWMPin, 2);

  currentMillis = millis();
  if (currentMillis - AngleStartMillis >= AngleWait) {
    digitalWrite(AngleMotorUpPin, LOW);
    digitalWrite(AngleMotorDownPin, HIGH);
  }
  
}

void startMovingDown() {
  Serial.println("Moving DOWN");
  tableState = MOVING_DOWN;
  digitalWrite(PWMDirPin, HIGH);
  analogWrite(PWMPin, 2);
  
    digitalWrite(AngleMotorUpPin, HIGH);
    digitalWrite(AngleMotorDownPin, LOW);
  
}

void stopMotor() {
  analogWrite(PWMPin, 0);
    digitalWrite(AngleMotorUpPin, LOW);
    digitalWrite(AngleMotorDownPin, LOW);
  if (PWMDirPin == LOW){
    digitalWrite(PWMDirPin, HIGH);
  }
  else digitalWrite(PWMDirPin, LOW);
  Serial.println("Motor STOPPED");
}

void setup() {
  Serial.begin(115200);

  pinMode(PWMPin, OUTPUT);
  pinMode(PWMDirPin, OUTPUT);
  stopMotor();
  pinMode(AngleMotorUpPin, OUTPUT);
  pinMode(AngleMotorDownPin, OUTPUT);
  pinMode(LocksPin, OUTPUT);

  buttonPin.setDebounceTime(DEBOUNCE_TIME);
  SwitchTopPin.setDebounceTime(DEBOUNCE_TIME);
  SwitchBottomPin.setDebounceTime(DEBOUNCE_TIME);

  SwitchTopPin.loop();
  SwitchBottomPin.loop();

  if (SwitchTopPin.getState() == LOW) {
    tableState = AT_TOP;
    Serial.println("Initial state: AT_TOP");
  } else if (SwitchBottomPin.getState() == LOW) {
    tableState = AT_BOTTOM;
    Serial.println("Initial state: AT_BOTTOM");
  } else {
    tableState = IDLE;
    Serial.println("Initial state: IDLE");
  }
}



void loop() {
  buttonPin.loop();
  SwitchTopPin.loop();
  SwitchBottomPin.loop();

  if (SwitchTopPin.isPressed()) {
    stopMotor();
    tableState = AT_TOP;
    Serial.println("Table is at the TOP");
  }
  if (SwitchBottomPin.isPressed()) {
    stopMotor();
    tableState = AT_BOTTOM;
    Serial.println("Table is at the BOTTOM");
  }

  if (buttonPin.isPressed()) {
    AngleStartMillis = millis();
    switch (tableState) {
      case IDLE:
        startMovingUp();
        break;
      case AT_TOP:
        startMovingDown();
        break;
      case AT_BOTTOM:
        startMovingUp();
        break;
      case MOVING_UP:
          stopMotor();
          tableState = IDLE;
          break;
      case MOVING_DOWN:
        stopMotor();
        tableState = IDLE;
        break;
    }
  }
}



I would like to add a delay until the Angle Motors kick in

Please describe the problem. What have you tried, what did you expect to happen and what happened instead?

My current code is above. I placed

  currentMillis = millis();
  if (currentMillis - AngleStartMillis >= AngleWait) {
    digitalWrite(AngleMotorUpPin, LOW);
    digitalWrite(AngleMotorDownPin, HIGH);
  }

into my StartMovingUp function
and placed

AngleStartMillis = millis();

into my button.isPressed area within the main loop.

The Angle motor does not turn on at all. I am wondering if my AngleStartMillis is somehow being reset with the loop?

The code in the "if" snippet will execute only once, until AngleStartMillis is appropriately updated.

Review the logic in the code, and the Blink Without Delay code example to see how millis() interval timing is done.

The Angle motor does not turn on at all.

To learn why not, put in Serial.print statements. For example:

  if (buttonPin.isPressed()) {
    AngleStartMillis = millis();
    Serial.println(tableState);
    switch (tableState) {
      case IDLE:
        startMovingUp();
        break;

solution was to move the millis counting to the void loop

  switch(tableState) {
    case MOVING_UP:
      currentMillis = millis();
      if (AngleStartMillis + AngleWait <= currentMillis) {
       digitalWrite(AngleMotorUpPin, LOW);
       digitalWrite(AngleMotorDownPin, HIGH);        
      }
      break;
  }

this looks pretty inelegant. any syntax improvements i can make?

You should have continued your discussion in the other topic where you got your code from me.

Rewrite so that you use subtraction.
It will eliminate the millis overflow problem...

@maxspek
change
currentMillis = millis();
if (AngleStartMillis + AngleWait <= currentMillis) {
to
if (millis()-AngleStartMillis >= AngleWait) {

But much more importantly, try to understand why the form is important, and ask if you don't understand.