// Include the necessary libraries
#include <Arduino.h>
// Define the pins for the motor and switches
const int motorPin1 = 2;
const int motorPin2 = 3;
const int buttonPin1 = 4;
const int buttonPin2 = 5;
const int limitSwitch1 = 6;
const int limitSwitch2 = 7;
// Define the motor states
enum MotorState {
STOPPED,
FORWARD,
REVERSE
};
// Initialize the motor state
MotorState motorState = STOPPED;
// Initialize the button states
int buttonState1 = 0;
int buttonState2 = 0;
// Initialize the limit switch states
int limitSwitchState1 = 0;
int limitSwitchState2 = 0;
void setup() {
// Set the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Set the button pins as inputs
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// Set the limit switch pins as inputs
pinMode(limitSwitch1, INPUT);
pinMode(limitSwitch2, INPUT);
// Set the initial motor state to stopped
motorState = STOPPED;
}
void loop() {
// Read the button states
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// Read the limit switch states
limitSwitchState1 = digitalRead(limitSwitch2);
limitSwitchState2 = digitalRead(limitSwitch1);
// Check if the first button is pressed
if (buttonState1 == HIGH) {
// Set the motor state to forward
motorState = FORWARD;
}
// Check if the second button is pressed
if (buttonState2 == HIGH) {
// Set the motor state to reverse
motorState = REVERSE;
}
// Check if the first limit switch is pressed
if (limitSwitchState1 == HIGH) {
// Pause the motor
motorState = STOPPED;
}
// Check if the second limit switch is pressed
if (limitSwitchState2 == HIGH) {
// Pause the motor
motorState = STOPPED;
}
// Set the motor state based on the current state
switch (motorState) {
case STOPPED:
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
break;
case FORWARD:
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
break;
case REVERSE:
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
break;
}
}