I am trying to run a device which has 2 motors and 3 limit switches. Essentially, this device needs to lift a weight using a motor (defined as motor 1 in this scenario) until it reaches limit switch A. I want to use limit switch A to stop motor 1 and start rotating motor 2 in a counter clockwise direction. Motor 2 has an armrest attached to it and it can rotate freely. The armrest will eventually trigger limit switch B which I want to it to do the following cycle:
- Keep running motor 2 running counter clockwise for "x" amount of seconds.
- Stop Motor 2 for "x" amount of seconds.
- Rotate motor clockwise for "x" amount of seconds.
(This process rotates the arm 360 degrees and the clock wise rotation places the armrest in the testing position)
Once the cycle above is completed. Motor 1 will rotate the other direction to lower the weight. The weight should keep lowering until it reaches limit switch C. I want to use limit witch C to do the following cycle:
- Stop motor 1 for "x" amount of seconds
- rotate motor 1 to lift weight until it reaches limit switch A
The process should repeat from here on now.
So far the switches repeat its own cycle twice. I manually pressed each switch once and this was the output from the monitor.

Lastly, I/m aware that there is no code to control the speed for motor 1. I am using a different motor controller and I still need to figure out how it operates.
My existing code is below
#include <ezButton.h>
ezButton limitSwitchA(10); // create ezButton object that attach to pin 10;
ezButton limitSwitchB(9); // create ezButton object that attach to pin 9;
ezButton limitSwitchC(8); // create ezButton object that attach to pin 8;
// Motor setup
const int ENA_PIN = 7; // the Arduino pin connected to the ENA pin L298N for Motor 2
const int IN1_PIN = 6; // the Arduino pin connected to the IN1 pin L298N for Motor 2
const int IN2_PIN = 5; // the Arduino pin connected to the IN2 pin L298N for Motor 2
const int LPWM = 3; // the Arduino pin connected to the RPWM pin for Motor 1
const int RPWM = 2; // the Arduino pin connected to the LPWM pin for Motor 1
int var = 100;
void setup() {
Serial.begin(9600);
limitSwitchA.setDebounceTime(25); // set debounce time to 50 milliseconds
limitSwitchB.setDebounceTime(25); // set debounce time to 50 milliseconds
limitSwitchC.setDebounceTime(25); // set debounce time to 50 milliseconds
// initialize digital pins as outputs.
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
}
void loop() {
// Limit Switches
limitSwitchA.loop(); // MUST call the loop() function first
limitSwitchB.loop(); // MUST call the loop() function first
limitSwitchC.loop(); // MUST call the loop() function first
//Motors
digitalWrite(RPWM, HIGH); // rotate Motor 1 counter clockwise
digitalWrite(LPWM, LOW); // rotate Motor 1 counter clockwise
if(limitSwitchB.isPressed()){
Serial.println("The limit switch B: UNTOUCHED -> TOUCHED");
digitalWrite(RPWM, LOW); // stop Motor 1
digitalWrite(LPWM, LOW); // stop Motor 1
digitalWrite(IN1_PIN, LOW); // control Motor 2 to spin counter clockwise
digitalWrite(IN2_PIN, HIGH); // control Motor 2 to spin counter clockwise
for (int speed = 255; speed >= 0; speed--) {
analogWrite(ENA_PIN, speed);
delay(10);
}
delay(1000);
Serial.println("End Stage 1");
}
if(limitSwitchA.isPressed()) {
Serial.println("The limit switch A: UNTOUCHED -> TOUCHED");
delay (500);
digitalWrite(IN1_PIN, LOW); // stop motor Motor 2
digitalWrite(IN2_PIN, LOW); // stop motor Motor 2
delay(100);
digitalWrite(IN1_PIN, HIGH); // control Motor 2 to spin clockwise
digitalWrite(IN2_PIN, LOW); // control Motor 2 to spin clockwise
delay(250);
for (int speed = 0; speed <= 255; speed++) {
analogWrite(ENA_PIN, speed); // control the speed
delay(10);
}
digitalWrite(IN1_PIN, LOW); // stop motor Motor 2
digitalWrite(IN2_PIN, LOW); // stop motor Motor 2
digitalWrite(RPWM, LOW); // control Winch Motor spins clockwise to lower Weight
digitalWrite(LPWM, HIGH); // control Winch Motor spins clockwise to lower Weight
Serial.println("End Stage 2");
}
if(limitSwitchC.isPressed()){
Serial.println("The limit switch C: UNTOUCHED -> TOUCHED");
delay(500);
digitalWrite(RPWM, LOW); // stop Motor 1
digitalWrite(LPWM, LOW); // stop Motor 1
delay(2000);
Serial.println("End Stage 3");
}
}