// Define the pins for the motor and switches
int motorPin1 = 2;
int motorPin2 = 3;
int buttonPin1 = 4;
int buttonPin2 = 5;
int limitSwitch1 = 6;
int limitSwitch2 = 7;
// Define the motor state variables
bool motorForward = false;
bool motorReverse = false;
// Define the button state variables
bool button1Pressed = false;
bool button2Pressed = false;
// Define the limit switch state variables
bool limitSwitch1Pressed = true;
bool limitSwitch2Pressed = true;
void setup() {
// Set the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Set the button pins as inputs
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Set the limit switch pins as inputs
pinMode(limitSwitch1, INPUT);
pinMode(limitSwitch2, INPUT);
}
void loop() {
// Check if button 1 is pressed
if (digitalRead(buttonPin1) == LOW) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
}
// Check if button 2 is pressed
if (digitalRead(buttonPin2) == LOW) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
}
// Check if limit switch 1 is pressed
if (digitalRead(limitSwitch1) == HIGH) {
digitalWrite(motorPin1, LOW);
}
// Check if limit switch 2 is pressed
if (digitalRead(limitSwitch2) == HIGH) {
digitalWrite(motorPin2, LOW);
}
else
{
// Check if button 1 is pressed
if (button1Pressed) {
// Reverse the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
motorForward = false;
motorReverse = true;
}
// Check if button 2 is pressed
if (button2Pressed) {
// Forward the motor
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
motorForward = true;
motorReverse = false;
}
}
}