#include <Arduino.h>
// Define a new data type for the position state
enum state {
DOWN, // Door is in the down position
UP, // Door is in the up position
STOP, // Door movement is stopped
LOWER, // Door is moving downward
RAISE // Door is moving upward
};
// Variable to track the current position state
state position = DOWN;
// Define pin connections
#define Button_Raise_Pin 1 // Pin for the raise button
#define LimitSwitch_Up_Pin 2 // Pin for the upper limit switch
#define Stop_Pin 9 // Pin for the stop button
#define Button_Lower_Pin 5 // Pin for the lower button
#define LimitSwitch_Down_Pin 6 // Pin for the lower limit switch
#define Door_Raise_Pin 3 // Pin to control raising the door
#define Door_Up_Pin 4 // Pin to control holding the door up
#define Door_Lower_Pin 7 // Pin to control lowering the door
#define Door_Down_Pin 8 // Pin to control holding the door down
#define enable A0;
#define int1 A1;
#define int2 A2;
// Define variables
bool Button_Raise = LOW; // State of the raise button
bool LimitSwitch_Up = LOW; // State of the upper limit switch
bool Button_Lower = LOW; // State of the lower button
bool LimitSwitch_Down = LOW; // State of the lower limit switch
bool Button_Stop = LOW; // State of the stop button
void setup() {
// Setup pin modes
// UP
pinMode(Button_Raise_Pin, INPUT_PULLUP); // Set raise button pin as input with pull-up resistor
pinMode(LimitSwitch_Up_Pin, INPUT_PULLUP); // Set upper limit switch pin as input with pull-up resistor
pinMode(Door_Raise_Pin, OUTPUT); // Set raise control pin as output
pinMode(Door_Up_Pin, OUTPUT); // Set hold-up control pin as output
// DOWN
pinMode(Button_Lower_Pin, INPUT_PULLUP); // Set lower button pin as input with pull-up resistor
pinMode(LimitSwitch_Down_Pin, INPUT_PULLUP); // Set lower limit switch pin as input with pull-up resistor
pinMode(Door_Lower_Pin, OUTPUT); // Set lower control pin as output
pinMode(Door_Down_Pin, OUTPUT); // Set hold-down control pin as output
// STOP
pinMode(Stop_Pin, INPUT_PULLUP); // Set stop button pin as input with pull-up resistor
pinMode(enable, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void loop() {
// Read input pins
Button_Raise = !digitalRead(Button_Raise_Pin); // Read the raise button state (inverted logic due to pull-up resistor)
LimitSwitch_Up = !digitalRead(LimitSwitch_Up_Pin); // Read the upper limit switch state (inverted logic due to pull-up resistor)
Button_Lower = !digitalRead(Button_Lower_Pin); // Read the lower button state (inverted logic due to pull-up resistor)
LimitSwitch_Down = !digitalRead(LimitSwitch_Down_Pin); // Read the lower limit switch state (inverted logic due to pull-up resistor)
Button_Stop = !digitalRead(Stop_Pin); // Read the stop button state (inverted logic due to pull-up resistor)
switch (position) {
case DOWN:
// Set the door control pins to lower the door
digitalWrite(Door_Lower_Pin, LOW);
digitalWrite(Door_Down_Pin, HIGH);
if (Button_Raise == HIGH) {
position = RAISE; // If the raise button is pressed, transition to the RAISE state
}
break;
case RAISE:
// Deactivate the lower control and activate the raise control
digitalWrite(Door_Down_Pin, LOW);
digitalWrite(Door_Raise_Pin, HIGH);
analogWrite(int1, HIGH);
analogWrite(int2, LOW);
analogWrite(enable, 100); // Motor 1 soll mit der Geschwindigkeit "200" (max. 255) rotieren
if (LimitSwitch_Up == HIGH) {
position = UP; // If the upper limit switch is triggered, transition to the UP state
} else if (Button_Stop == HIGH) {
position = STOP; // If the stop button is pressed, transition to the STOP state
}
break;
case UP:
// Deactivate the raise control and activate the hold-up control
digitalWrite(Door_Raise_Pin, LOW);
digitalWrite(Door_Up_Pin, HIGH);
analogWrite(int1, LOW);
analogWrite(int2, LOW);
if (Button_Lower == HIGH) {
position = LOWER; // If the lower button is pressed, transition to the LOWER state
}
break;
case LOWER:
// Deactivate the hold-up control and activate the lower control
digitalWrite(Door_Up_Pin, LOW);
digitalWrite(Door_Lower_Pin, HIGH);
analogWrite(int1, LOW);
analogWrite(int2, HIGH);
analogWrite(enable, 100); // Motor 1 soll mit der Geschwindigkeit "200" (max. 255) rotieren
if (LimitSwitch_Down == HIGH) {
position = DOWN; // If the lower limit switch is triggered, transition to the DOWN state
} else if (Button_Stop == HIGH) {
position = STOP; // If the stop button is pressed, transition to the STOP state
}
break;
case STOP:
// Deactivate both the raise and lower controls
digitalWrite(Door_Raise_Pin, LOW);
digitalWrite(Door_Lower_Pin, LOW);
analogWrite(int1, LOW);
analogWrite(int2, LOW);
analogWrite(enable, 100); // Motor 1 soll mit der Geschwindigkeit "200" (max. 255) rotieren
if (Button_Raise == HIGH) {
position = RAISE; // If the raise button is pressed, transition to the RAISE state
} else if (Button_Lower == HIGH) {
position = LOWER; // If the lower button is pressed, transition to the LOWER state
}
break;
}
}