by pressing the existing bottom in pin 2 the motor rotates 5 rounds clockwise
by pressing the new bottom (pin 5) the motor rotates 5 rounds counter-clockwise.
Could you please help me to complete the project. Thanks
// Stepper motor run code with A4988 driver, 5 revolutions per button press, disabled when idle
// by Superb, edited by Gemini
const int stepPin = 3;
int dirPin = 4;
int enablePin = 8;
int dirButton = 2;
int state = HIGH;
int reading;
int previous = LOW;
int stepDelay = 600;
const int stepsPerRevolution = 200;
const int revolutionsPerButtonPress = 5;
long time = 0;
long debounce = 200;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(dirButton, INPUT_PULLUP);
digitalWrite(enablePin, HIGH);
}
void loop() {
reading = digitalRead(dirButton);
// Debounce button press
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
digitalWrite(enablePin, LOW);
// Rotate 5 revolutions in the current direction
for (int i = 0; i < revolutionsPerButtonPress * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
state = !state; // Toggle direction for next press
digitalWrite(dirPin, state);
digitalWrite(enablePin, HIGH);
time = millis(); // Update time for debounce
}
previous = reading;
}
No Actually!
I wanted ChatGPT to write the code from scratch,
Asked:
"Write me a code for Arduino Uno to control a Nema 17 stepper motor considering:
the motor driver is: A4988
by pressing the RButton the motor rotates 5 revolutions clockwise
by pressing the LButton the motor rotates 5 revolutions counter clockwise
stepPin in pin 3
dirPin in pin 4
enablePin in pin 8 for driver enable when the motor is not rotating (after completing 5 rounds)
RButton in pin 2
LButton in pin 5
stepDelay = 600;"
He or she did it!
I'm a newcomer to codeing, so AI helped me to learn alot...