Help needed! Stepper forward and backward with two press buttons

Hello to all,

I'm trying to do a simple project with an Arduino Nano, a driver A4988, a stepper motor nema17 with 0,9 degrees per step, and two momentaneous press buttons.

The motor will be mounted in a variable capacitor and I want to ajust the variable capacitor in small steps to the left or right with the press buttons.

I've downloaded a library for A4988 and I could make the motor moves using an example code. The movement with example code is not very linear but works forward and backward.

All I want is to Insert two buttons. When I press button 1, the motor do 5 steps to the left each time I press the button. If I press the button 2, the motor do 5 steps to the right each time I press the button.

I started with this code:

const int EN=2; //ENABLE PIN
const int Step=3; // STEP PIN
const int dir=4; // DIRECTION PIN
/----------------------------SETUP FUNCTION--------------------------/
void setup()
{
pinMode(EN,OUTPUT); // ENABLE AS OUTPUT
pinMode(dir,OUTPUT); // DIRECTION AS OUTPUT
pinMode(Step,OUTPUT); // STEP AS OUTPUT
digitalWrite(EN,LOW); // SET ENABLE TO LOW
}
/----------------------------LOOP FUNCTION--------------------------/
void loop()
{
digitalWrite(dir,LOW); // SET DIRECTION LOW FOR FORWARD ROTATION
for(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN
{
digitalWrite(Step,HIGH); // STEP HIGH
delay(1); // WAIT
digitalWrite(Step,LOW); // STEP LOW
delay(1); // WAIT
}
delay(10); // DELAY BEFOR SWITCH DIRECTION
digitalWrite(dir,HIGH); // SET DIRECTION HIGH FOR BACKWARD ROTATION
for(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN
{
digitalWrite(Step,HIGH); // STEP HIGH
delay(1); // WAIT
digitalWrite(Step,LOW); // STEP LOW
delay(1); // WAIT
}
delay(10); // DELAY BEFOR SWITCH DIRECTION
}

Please, anyone can help me adding the function of two buttons on this code?

thanks in advance, best regards

After reading the "How to use this forum" post, please edit your post to add code tags.

Forum members usually don't write code for you, they help you fix what goes wrong with your attempt. Of course the code will depend on your custom installation, which you haven't described.

So, before you start, study the examples that come with Arduino, which show you how to read buttons, a potentiometer, and other useful techniques. In the Arduino IDE, select File>Examples> ...

The movement with example code is not very linear

It would be, if you powered the motor correctly, and set the A4988 current limit correctly.

Have a look at the second example in this Simple Stepper Code

...R
Stepper Motor Basics

With AccelStepper library the code would in outline be:

AccelStepper motor (AccelStepper::DRIVER, Step, dir) ;

void setup ()
{
  motor.setAcceleration (...) ;
  motor.setMaxSpeed (...) ;
}

void loop ()
{
  motor.run()
  if (... up button becomes pressed ...)
    motor.move (5) ;
  if (... down button becomes pressed ...)
    motor.move (-5) ;
}

You'll need to debounce the buttons and detect change to pressed state of course, without using delay()
as AccelStepper relies on run() being called frequently.

Other architecture (Pi instead of Arduino), and keyboard keys instead of buttons.

Stepper is connected to microscope for z axis movements (4.4µm/half-step):

Details in "Re: diy autofocus with /dev/video16 and stepper motor?" posting.
Simple small python code used this time:
https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=278268&p=1692610#p1692610