Need help with some code for the 6 axis robot arm im building

okay so as the title says im in need some help with the following code....

What im wanting todo is press buttons to make certain stepper motors move in a certain direction,

So far if i press Button number 1 Motor One turns clockwise if i press Motor button 2 Motor One turns Anti clockwise.... the bit im stuck on is getting it to see that im pressing Motor button 3 and 4 for Motor Two Direction clockwise and anticlockwise im using the Arduino Mega 2560 and the stepper drivers are A4988

// constants won't change. They're used here to set pin numbers:
const int motorButton1 = 2;     // Motor one
const int motorButton2 = 3;     // Motor one
const int stepPin0 = 52;                   // Motor one
const int dirPin1 = 53;                    // Motor one

const int motorButton3 = 4;     // Motor Two
const int motorButton4 = 5;     // Motor Two
const int stepPin2 = 50;                   // Motor Two
const int dirPin3 = 51;                    // Motor Two


// variables will change:
int buttonState0 = 0;         // variable for reading the pushbutton status for Motor One
int buttonState1 = 0;         // variable for reading the pushbutton status for Motor One
int buttonState2 = 0;         // variable for reading the pushbutton status for Motor Two
int buttonState3 = 0;         // variable for reading the pushbutton status for Motor Two

void setup() {
  

  pinMode(motorButton1, INPUT_PULLUP);    // Motor one
  pinMode(motorButton2, INPUT_PULLUP);    // Motor one
  pinMode(stepPin0,OUTPUT);              // Motor one
  pinMode(dirPin1,OUTPUT);               // Motor one

  pinMode(motorButton3, INPUT_PULLUP);    // Motor one
  pinMode(motorButton3, INPUT_PULLUP);    // Motor one
  pinMode(stepPin2,OUTPUT);              // Motor one
  pinMode(dirPin3,OUTPUT);               // Motor one
}


void loop() {
  
  buttonState0 = digitalRead(motorButton1);  // read the status of the pushbutton value Motor One:
  buttonState1 = digitalRead(motorButton2);  // read the status of the pushbutton value Motor One:
  buttonState2 = digitalRead(motorButton3);  // read the status of the pushbutton value Motor Two:
  buttonState3 = digitalRead(motorButton4);  // read the status of the pushbutton value Motor Two:

  

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState0 == LOW) {
    // turn LED on:
    digitalWrite(dirPin1,HIGH); 
    digitalWrite(stepPin0,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin0,LOW); 
    delayMicroseconds(500); 
  for(int x = 0; x < 42; x++); 
  }
  
  if (buttonState1 == LOW) {
    // turn LED off:
     digitalWrite(dirPin1,LOW); 
    digitalWrite(stepPin0,LOW); 
    delayMicroseconds(500); 
    digitalWrite(stepPin0,HIGH); 
    delayMicroseconds(500); 
  for(int x = 0; x < 42; x++); 
  }
if (buttonState2 == LOW) {
    // turn LED on:
    digitalWrite(dirPin3,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin2,LOW); 
    delayMicroseconds(500); 
  for(int x = 0; x < 42; x++); 
}
  
  if (buttonState3 == LOW) {
    // turn LED off:
     digitalWrite(dirPin3,LOW); 
    digitalWrite(stepPin2,LOW); 
    delayMicroseconds(500); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(500); 
  for(int x = 0; x < 42; x++); 

  } 
  }

im stuck on is getting it to see that im pressing Motor button 3 and 4 for Motor Two Direction clockwise and anticlockwise

At its simplest just repeat the code for the first 2 buttons and the motor using different names for the variables and obviously different pins. There are better ways to do this but start simple

  for(int x = 0; x < 42; x++);

What is the purpose of this line of code ?

UKHeliBob:
At its simplest just repeat the code for the first 2 buttons and the motor using different names for the variables and obviously different pins. There are better ways to do this but start simple

  for(int x = 0; x < 42; x++);

What is the purpose of this line of code ?

I have tried Repeating the code with different names for the Variables and different pins the code i have put above is supposed to beable to run the two steppermotor a5988 and its not for some reason

im not 100% sure i think its somthing todo with timing how fast the pins go from low to high to the stepper a4988 driver   for(int x = 0; x < 42; x++); example 42 for one revolution

  for(int x = 0; x < 42; x++);

Counts from 0 to 41
Does nothing with the count variable value
The semicolon is the only code executed 42 times
Will be optimised away by the compiler

UKHeliBob:

  for(int x = 0; x < 42; x++);

Counts from 0 to 41
Does nothing with the count variable value
The semicolon is the only code executed 42 times
Will be optimised away by the compiler

than you for your input, iv wiped that out of the code now, still need to find out why its not controlling the second stepper motor

The first thing that I would do is to make the variable names consistent. State names should have the same number suffix as the pins they relate to, what happened to stepPin1, dirPin0 and dirPin2, for instance. Incorporating motor names, or at leas M0, M1 etc into the pin names for each motor would also make the code easier to read. Remove nonsense comments such as

    // turn LED on:

and

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

Add some Serial.print()s to the program so that you know which parts are being executed and the value of pertinent variables at that point

UKHeliBob:
The first thing that I would do is to make the variable names consistent. State names should have the same number suffix as the pins they relate to, what happened to stepPin1, dirPin0 and dirPin2, for instance. Incorporating motor names, or at leas M0, M1 etc into the pin names for each motor would also make the code easier to read. Remove nonsense comments such as

    // turn LED on:

and

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

Add some Serial.print()s to the program so that you know which parts are being executed and the value of pertinent variables at that point

i have made these changes and i have just ordered 6 New Dual L298N H Bridge Stepper Motor Drivers as the a4988 stepper drivers i was using dont seam to be working as i think i might of broken them :frowning:

well the 6 H-bridge stepper drivers arrived today and the bad news is i have a total of 8 stepper motors i can only drive 2 motors out of the 8 with these h-bridge drivers, as the motors that dont work with them are drawing 4.6ish amps and the stepper driver is on rated to 2amps per coil, hmmm, what to try next?

anybody got and spare stepper drivers they could send my way for me to try before i go ahead and buy others as i dont want to wast to much money....

thanks for any help guys and gals

There are large H-bridges available on ebay depending on how large your stepper motors are.

zoomkat:
There are large H-bridges available on ebay depending on how large your stepper motors are.

could you post some links for me?

"could you post some links for me?"

The below are typical of the larger H-bridges.

https://www.ebay.com/itm/Dual-Motor-Driver-Module-Board-H-bridge-MOSFET-IRF3205-3-36V-15A-Peak30A-Part/293239841560?hash=item444674e318:g:3fcAAOSwhvtdhJFW