Skip to main content
1 of 2

Code not Executing Reliably on Push Button Activation

I have put together the following code to activate a motor using an Arduino Uno via a push button. Two push buttons should be pressed simultaneously to activate the motor for a specified number of steps, then when both buttons are pressed again the motor will spin in reverse, returning the motor to its original position. The code works, but not every time the button is pressed. I've gone through every line of the code and can't seem to find the issue. I know that the connections are correct since the code executes sometimes, but I don't know if there is an error in the code causing the unreliable response or if I have faulty/cheap push buttons with bad connections.

#include <Bounce2.h> // Include the Bounce2 library

// Define stepper motor connections:
#define dirPin 3
#define stepPin 2
#define motorEnablePin 4 // Motor driver's enable pin
#define buttonPin 8 // Push button connected to pin 8
#define buttonPin2 9 // Push button connected to pin 8

boolean motorRunning = false; // Flag to track motor rotation
boolean oddRotation = true; // Flag to track odd/even rotations

// Create a Bounce object for the button
Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce();

void setup() {
  // Declare pins as output or input:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(motorEnablePin, OUTPUT); // Enable pin as output
  pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor for the button
  pinMode(buttonPin2, INPUT_PULLUP); // Use internal pull-up resistor for the button
  digitalWrite(dirPin, LOW); // Set the spinning direction CW/CCW
  digitalWrite(motorEnablePin, HIGH); // Disable motor driver initially

  // Attach the debouncer to the button pin
  debouncer1.attach(buttonPin);
  debouncer2.attach(buttonPin2);
  debouncer1.interval(50); // Set debounce time
  debouncer2.interval(50);
}

void loop() {
  // Update the debouncer
  debouncer1.update();
  debouncer2.update();

  if (debouncer1.fell() && debouncer2.fell()) {
    // Button pressed and motor is not running, initiate motor rotation
    motorRunning = true;
    oddRotation = !oddRotation; // Toggle the rotation direction
    
    int stepsPerRevolution = 200; // Number of steps for one full revolution
    int desiredRevolutions = 10;  // Number of desired revolutions
    int totalSteps = stepsPerRevolution * desiredRevolutions;
    int stepCount = 0; // Counter for the steps taken
    
    // Set the motor rotation direction
    digitalWrite(dirPin, oddRotation ? LOW : HIGH);

    // Enable the motor driver
    digitalWrite(motorEnablePin, LOW);

    while (stepCount < totalSteps) {
      // Rotate the motor
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(600);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(600);
      
      stepCount++;

      // If the desired number of rotations is reached, stop the motor
      if (stepCount >= totalSteps) {
        // Disable the motor driver
        digitalWrite(stepPin, LOW);
        digitalWrite(motorEnablePin, HIGH);

        // Reset the flag to stop motor rotation
        motorRunning = false;
      }
    }

    // Disable the motor driver when idle
    digitalWrite(motorEnablePin, HIGH);
  }
}