Requesting help writing some code for buttons

Hello, and thanks in advance for the help. I am using an UNO, programming with Arduino 1.5.6 r2. I am running 2, 28BYJ-48 stepper motors with their little 2PH640 H bridge drivers. I (barely) wrote a routine that runs the 2 motors back and forth around 1/2 revolution automatically. I want to add 4 buttons to my project to move each motor backward or forward with each button click. I will adjust the distance setting in the program to set the response to each press of the buttons. It would be desirable for the motors to run as long as the button is held down, but move the distance specified with a single click of the button. Below is my code, the i variable loop could be removed once the button routines are in place-----

include <Stepper.h>

#define STEPS 48 // 48 steps per rotation
Stepper stepper1f(STEPS, 11, 9, 10, 8); //open forward stepper A
Stepper stepper1r(STEPS, 8, 10, 9, 11); //open reverse stepper A
Stepper stepper2f(STEPS, 7, 5, 6, 4); //open forward stepper B
Stepper stepper2r(STEPS, 4, 6, 5, 7); //open reverse stepper B
void setup()
{
int i;

stepper1f.setSpeed(160); //set speed
stepper1r.setSpeed(160); //set speed
stepper2f.setSpeed(160); //set speed
stepper2r.setSpeed(160); //set speed
for (i=0;i<4;i++) {
stepper1f.step(1000); //distance
stepper1r.step(1000); //distance
stepper2f.step(1000); //distance
stepper2r.step(1000); //distance
}
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}

void loop()
{}

I am not a software guy. My brain just is not wired for it. Im pretty good with hardware though. This particular project is 80's vintage wafer prober which uses a linear stepper motor XY stage, which is a pretty cool thing, gutted of its old electronics and replaced with an UNO running GRBL and using Universal Gcode sender and some modern stepper motor drivers. The prober now thinks its a CNC machine. The weakness of UGS is that it can only handle the 3 main axis, X,Y and Z. I also need Theta and tilt adjustment and dont want to run another copy of UGS to handle the theta and tilt. One or 2 dedicated UNO's with buttons should be fine for those fixed adjustments.

The UNO has 4 remaining digital IO channels and all of its analog IO. I hope one of you coders helps me, What would take me a week or more may only take you 15 minutes.

Thanks,
Gary

20180625_133242.jpg

20180625_133219.jpg

20180625_133228.jpg

Mr Delta has been reported to the administrators as rude and insulting. If a new section is where my request belongs, I ask a moderator to move it there. If theres another user here that considers these things fun and interesting and is not hindered by forum rules and trolls, please offer to help me if you like.

Mr Delta has been reported to the administrators as rude and insulting

No, reported to the moderators.
It's written on the control you used.

No action.

Ok, Bad start here on the forum.. Here is my attempt after reading more. It does not work quite right. Can anyone offer advice?
It uploads ok, but does not respond to button clicks on digital 2, 3, 12 or 13. If I comment out the digital read statements, it runs each stepper command once then puts all the stepper drive lines low.

#include <Stepper.h>

#define STEPS 48 // 48 steps per rotation
Stepper stepper1f(STEPS, 11, 9, 10, 8); //open forward stepper A
Stepper stepper1r(STEPS, 8, 10, 9, 11); //open reverse stepper A
Stepper stepper2f(STEPS, 7, 5, 6, 4); //open forward stepper B
Stepper stepper2r(STEPS, 4, 6, 5, 7); //open reverse stepper B
void setup() 
{
int button_1 = 2;
int button_2 = 3;
int button_3 = 12;
int button_4 = 13;

pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(button_3, INPUT);
pinMode(button_4, INPUT);
  
  
stepper1f.setSpeed(160); //set speed
stepper1r.setSpeed(160); //set speed
stepper2f.setSpeed(160); //set speed
stepper2r.setSpeed(160); //set speed 
  
   
if (digitalRead(button_1) == HIGH)
    stepper1f.step(1000);  

if (digitalRead(button_2) == HIGH)
    stepper1r.step(1000);
 
if (digitalRead(button_3) == HIGH)
    stepper2f.step(1000); 
 
if (digitalRead(button_4) == HIGH)
    stepper2r.step(1000);
 
// else

  
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW); 
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);  
}

void loop() 
{}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

GB123:
Ok, Bad start here on the forum.. Here is my attempt after reading more.

You should have read "How to use this forum" to learn how to post code correctly.

You should work through a basic button example including the correct wirings that are possible.
You should check your button setup against those possibilities.
We can not help because you did not specify how you wired your butttons.

Defining two stepper objects with swapped hardware settings is probably not the way to make a stepper turn backwards.
So it seems you should work through some basic stepper examples too.

Just FYI, using an h-bridge to drive steppers is possibly the least efficient - unless you happen to have a spare h-bridge just laying around. There are dedicated stepper controllers/chips, or software-only stepping logic to directly drive the motor(s) through (FE)Transistors.

As for buttons & switches, as noted above - the basic tutorials explain the best methods- and specifically using the change of state - rather than just the current state of high or low.

Good luck - we're all listening and happy to help.

Thanks last chance. I have 5 of the cheap 28BYJ geared steppers and their h bridge drivers. They will work fine for my application. This particular UNO will power up and do nothing but respond to 4 buttons. Motor 1 forward, motor 1 reverse, motor 2 forward, motor 2 reverse. The buttons are wired traditionally, with 5v on one side and 10k resistor/ground/digital pin on the other side

I have improved my code and have the top function in the loop working with button pushes but the program doesnt look at the next 3 button push functions. The code is below.. Any suggestions so it loops through all 4 buttons? This my 1st day of programming so..

#include <Stepper.h>
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3; 
const int buttonPin3 = 12; 
const int buttonPin4= 13; 

int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 

#define STEPS 48 // 48 steps per rotation
Stepper stepper1f(STEPS, 11, 9, 10, 8); //open forward stepper 1
Stepper stepper1r(STEPS, 8, 10, 9, 11); //open reverse stepper 1
Stepper stepper2f(STEPS, 7, 5, 6, 4); //open forward stepper 2
Stepper stepper2r(STEPS, 4, 6, 5, 7); //open reverse stepper 2

void setup() {
  
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);

stepper1f.setSpeed(160); //set speed
stepper1r.setSpeed(160); 
stepper2f.setSpeed(160); 
stepper2r.setSpeed(160); 
}

void loop() {

buttonState1 = digitalRead(buttonPin1 );
if (buttonState1 == HIGH) {
stepper1f.step(10);   //distance per push
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW); 

buttonState2 = digitalRead(buttonPin2 );
if (buttonState2 == HIGH) {
stepper1r.step(10);   //distance per push
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 
 buttonState3 = digitalRead(buttonPin3 );
if (buttonState3 == HIGH) {
stepper2f.step(10);   //distance per push
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 
 buttonState4 = digitalRead(buttonPin4 );
if (buttonState4 == HIGH) {
stepper2r.step(10);   //distance per push
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);

} 
}
}
}
}

Thanks for the input. I removed redundant motors and reversed the direction as you suggested.

I used the cntl T as well. I tried removing the digital write low to turn off the stepper coils, but they stayed on after the 1st working button was pushed so I kept them in.

I will take your advice and wire the switches to ground and use the internal resistors, but after the code is working. That will clean up the wiring considerably!

I understand what you are saying that subsequent buttons are ignored unless the previous one is pushed. Are you saying that by shuffling some brackets around that I can have all 4 buttons active and independent?

The newest code is below. I am reading up on C++ but would appreciate a little help making it work.

#include <Stepper.h>
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 12;
const int buttonPin4 = 13;

int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

#define STEPS 48 // 48 steps per rotation
Stepper stepper1(STEPS, 11, 9, 10, 8); //forward stepper 1(Out order making up for crossed wiring)
Stepper stepper2(STEPS, 7, 5, 6, 4); //forward stepper 2 (Out order making up for crossed wiring)


void setup() {

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  stepper1.setSpeed(160); //set speed
  stepper2.setSpeed(160);

}

void loop() {

  buttonState1 = digitalRead(buttonPin1 );
  if (buttonState1 == HIGH) {
    stepper1.step(10);   //distance per push
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, LOW);
   digitalWrite(11, LOW);

    buttonState2 = digitalRead(buttonPin2 );
    if (buttonState2 == HIGH) {
      stepper1.step(-10);   //distance per push
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);

      buttonState3 = digitalRead(buttonPin3 );
      if (buttonState3 == HIGH) {
        stepper2.step(10);   //distance per push
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        digitalWrite(7, LOW);

        buttonState4 = digitalRead(buttonPin4 );
        if (buttonState4 == HIGH) {
          stepper2.step(-10);   //distance per push
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
          digitalWrite(6, LOW);
          digitalWrite(7, LOW);

        }
      }
    }
  }
}

Look at how your "if" statements are nested - if buttonstate1 is LOW, you don't even read the second button.
Put a closing } brace immediately after the code that should be executed if buttonstate1 is HIGH