A4988 and need change in code!

I need help to modify this code by considering:

  • adding a new bottom to pin 5
  • 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;
}

Please confirm that the program you show works properly.

I found the solution using AI ( Chat GPT) help.

// Stepper motor control code with A4988 driver, 5 revolutions per button press in clockwise and counterclockwise directions executed only once per button press
// by Superb, edited by Gemini

const int stepPin = 3;
const int dirPin = 4;
const int enablePin = 8;
const int RButton = 2;
const int LButton = 5;
const int stepsPerRevolution = 200; // Steps per revolution for Nema 17 motor
const int revolutionsPerButtonPress = 5;
int stepDelay = 600;

bool clockwise = true;
bool RButtonPressed = false;
bool LButtonPressed = false;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(RButton, INPUT_PULLUP);
  pinMode(LButton, INPUT_PULLUP);
  
  digitalWrite(enablePin, HIGH); // Enable the driver initially
}

void loop() {
  if (digitalRead(RButton) == LOW && !RButtonPressed) {
    rotateMotor(clockwise);
    RButtonPressed = true;
    LButtonPressed = false;
  }

  if (digitalRead(LButton) == LOW && !LButtonPressed) {
    rotateMotor(!clockwise);
    LButtonPressed = true;
    RButtonPressed = false;
  }
}

void rotateMotor(bool direction) {
  digitalWrite(enablePin, LOW); // Disable driver during rotation
  digitalWrite(dirPin, direction ? HIGH : LOW); // Set direction

  for (int i = 0; i < revolutionsPerButtonPress * stepsPerRevolution; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
  }

  digitalWrite(enablePin, HIGH); // Re-enable driver after rotation
}

Looks like you can't do two button presses for the same direction, which may be what you wanted.

Was adding a second button a solution to a problem with the first code you posted?

If so, I wonder if chatGPT coukd have fixed that.

a7

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.