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
}
}