Using a Switch and Two Buttons to Control Stepper Motor

Hello,

To make a long story short I am wanting to use a switch (ON-OFF-ON) and two push buttons to control my motor. On the switch, each "ON" toggle with control the direction of the motor. When toggled "ON" it should travel continuously (in either counter clockwise or clockwise depending on which "ON" is toggled. The buttons will do the same (one controls clockwise step while the other controls counter-clockwise step) but instead of it stepping continuously it will only step once. I used help from here Stepper Motor with DRV8825 and Arduino Tutorial (4 Examples) to assist with my wiring setup.

My main question is, did I code this right? I understand that debouncing the buttons would be needed so I used this library GitHub - j-bellavance/EdgeDebounceLite: Just replace digitalRead() with debounce.pin() for help. I did not understand coding two buttons from the tutorials by Arduino.

Everything complied correctly so I am unsure of the real issue in the code as for I am a beginner.

Schematic

Datasheets

DRV8825 - https://www.makerguides.com/wp-content/uploads/2019/02/DRV8825-Datasheet.pdf

NEMA 17 - https://images-na.ssl-images-amazon.com/images/I/91zyUMD1hWL.pdf

#include <EdgeDebounceLite.h>

EdgeDebounceLite debounce;

byte buttonPins[]{12,13};

const int cwPin = 7;
const int ccPin = 8;
const int sleepPin = 4;
const int ScwPin = 12;
const int SccPin = 13;
const int stepPin = 2;
const int dirPin = 3;

int stepsPerRevolution = 6400; // when using 1/32-full step
int revolutionPerMin = 120;


void setup() {

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(cwPin, INPUT);
  pinMode(ccPin, INPUT);
  pinMode(sleepPin, INPUT);
  pinMode(ScwPin, INPUT);
  pinMode(SccPin, INPUT);
  
}

void loop() 
{

  if (digitalRead(cwPin == HIGH)) 
  { // Switch enabled to move motor clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up    
    digitalWrite(dirPin, HIGH); // Enables Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) 
    {

      //start = millis();
      
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    } 
  }
  if (digitalRead(ccPin == HIGH)) 
  { // Switch enabled to move motor counter-clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, LOW); // Enables Counter-Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) 
    {
      
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
  }
  if (debounce.pin(ScwPin == HIGH)) 
  { // Button is pressed to move motor clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, HIGH); // Enables Clockwise Rotation
    
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  if (debounce.pin(SccPin == HIGH)) 
  { // Button pressed to move motor counter-clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, LOW); // Enables Counter-Clockwise Rotation
    
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  else 
  { // Switch is OFF and no buttons are pressed; the motor is put to sleep.

    digitalWrite(sleepPin, LOW); // Motor put in Sleep Mode
  }
}

Doesn't the DRV8825 reset pin need to be held HIGH? Please don't say you are using a 9V smoke detector battery, they are hopeless for a 2 Amp motor.
0J4232.600.png

0J4232.600.png

JCA34F:
Doesn't the DRV8825 reset pin need to be held HIGH? Please don't say you are using a 9V smoke detector battery, they are hopeless for a 2 Amp motor.

Wow, I forgot that part. I've attached the RESET Pin to the junction that M0, M1, and M2 were connected too. Now the motor works, but doesn't do each task coded.

Hehe, currently I am using a Power Supply that ranges from 0-30 Volts, however, i am wanting to make the project mobile. Would there be a better, yet still mobile, battery?

How does your program work now? Your schematic link does not work, here's how to post images.
How to post image:

I've attached the RESET Pin to the 5V on the Arduino and now my motor is moving, however it does not do what i need it to do.

Once powered on the motor will rotate one full direction and then change direction and complete another full rotation. So it moves from 0 degrees to 360 degrees, then back from 360 degrees to 0 degrees. Toggling the switch or pushing the buttons has no effect on the motor.

pinMode(sleepPin, INPUT);

Should be:

pinMode(sleepPin, OUTPUT);

JCA34F:
Should be:

pinMode(sleepPin, OUTPUT);

That makes sense. I've fixed that now. My problem is that with the current code (I'll post the updated version), my motor still moves on its own without the control of the switch.

#include <EdgeDebounceLite.h>

EdgeDebounceLite debounce;

byte buttonPins[]{12,13};

const int cwPin = 7;
const int ccPin = 8;
const int sleepPin = 4;
const int ScwPin = 12;
const int SccPin = 13;
const int stepPin = 2;
const int dirPin = 3;

int stepsPerRevolution = 6400; // when using 1/32-full step
int revolutionPerMin = 120;


void setup() {

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(cwPin, INPUT);
  pinMode(ccPin, INPUT);
  pinMode(sleepPin, OUTPUT);
  pinMode(ScwPin, INPUT);
  pinMode(SccPin, INPUT);
  
}

void loop() {

  if (digitalRead(cwPin == HIGH)) { // Switch enabled to move motor clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up    
    digitalWrite(dirPin, HIGH); // Enables Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) {

      
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    } 
  }
  if (digitalRead(ccPin == HIGH)) { // Switch enabled to move motor counter-clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, LOW); // Enables Counter-Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) {
    
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
  }
  if (debounce.pin(ScwPin == HIGH)) { // Button is pressed to move motor clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, HIGH); // Enables Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) {
    
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
  }
  if (debounce.pin(SccPin == HIGH)) { // Button pressed to move motor counter-clockwise.

    digitalWrite(sleepPin, HIGH); // Motor Wakes Up
    digitalWrite(dirPin, LOW); // Enables Counter-Clockwise Rotation
    
    for (int i = 0; i < stepsPerRevolution; i++) {
    
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
  }
  else { // Switch is OFF and no buttons are pressed; the motor is put to sleep.

    digitalWrite(sleepPin, LOW); // Motor put in Sleep Mode
    
  }
  }

I've also attached a photo of what my circuit physically looks like.

I do realize after looking over my photo that I am missing my wires from the push buttons to the digital inputs on the Arduino. Here is the update from that modification...

Nothing has changed. once the Vmot on the Driver is powered it starts stepping one direction till it completes a turn then will rotate back to its origin. toggling the switch (ON-OFF-ON type of switch) and the buttons do nothing.

Here is an updated picture of the circuit.