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;
}
}
}