Please, need help on making system to control 2 motors simultaneously w/ buttons

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

/*control 2 steper motors simultaneusly using stepper library function calls
*/
#include <Stepper.h>

//digital pin 22~25 has a pushbutton attached. give it a name
const int button1 = 23;
const int button2 = 24;
const int button3 = 25;
const int buttonh = 22;

//enter the steps per rev for motors - IN1, IN2 (input for motors, but output for arduino/driver)
int stepsInRev = 200;
Stepper myStepper1 (stepsInRev, 10, 11, 12, 13);
Stepper myStepper2 (stepsInRev, 2, 3, 4, 5);

//variable to store the last call to the serial port
int Position = 0;

//move motors forward: equivalent to outward motion of crank in wheelchair system
void rotate(int steps) {
Serial.println("moving");
myStepper1.step(steps);
myStepper2.step(steps);
// delay(1);
}

void setup() {
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(buttonh, INPUT);
myStepper1.setSpeed(60);
myStepper2.setSpeed(60);
}

void loop() {
int command = check();

rotate(command);

}

/*
 Range of height control is first set with the unit of 20 rotations.
 After the algorithm is tested out, the number units will be modified
*/
int check() {
switch (Position) {
  case 0:
    if (digitalRead(button1) == HIGH) {
      Position += 20;
      Serial.println("Moving to Position 1: 20 rotations forward");
      return 20;
    }
    else if (digitalRead(button2) == HIGH) {
      Position += 40;
              Serial.println("Moving to Position 2: 40 rotations forward");
      return 40;
    }
    else if (digitalRead(button3) == HIGH) {
      Position += 60;
              Serial.println("Moving to Position 3: 60 rotations forward");
      return 60;
    }
    else if (digitalRead(buttonh) == HIGH) {
              Serial.println("Already at the position");
      return 0;
    }

  case 20:
    if (digitalRead(button1) == HIGH) {
      Serial.println("Already at the position");
      return 0;
    }
    else if (digitalRead(button2) == HIGH) {
      Position += 20;
      Serial.println("Moving to Position 2: 20 rotations forward");
      return 20;
    }
    else if (digitalRead(button3) == HIGH) {
      Position += 40;
      Serial.println("Moving to Position 3: 40 rotations forward");
      return 40;
    }
    else if (digitalRead(buttonh) == HIGH) {
      Position -= 20;
      Serial.println("Moving to Position h: 20 rotations backward");
      return -20;
    }

  case 40:
    if (digitalRead(button1) == HIGH) {
      Position -= 20;
      Serial.println("Moving to Position 1: 20 rotations backward");
      return -20;
    }
    else if (digitalRead(button2) == HIGH) {
     Serial.println("Already at the position");
      return 0;
    }
    else if (digitalRead(button3) == HIGH) {
      Position += 20;
      Serial.println("Moving to Position 3: 20 rotations forward");
      return 20;
    }
    else if (digitalRead(buttonh) == HIGH) {
      Position -= 40;
      Serial.println("Moving to Position h: 40 rotations backward");
      return -40;
    }

  case 60:
    if (digitalRead(button1) == HIGH) {
      Position -= 40;
      Serial.println("Moving to Position 1: 40 rotations backward");
      return -40;
    }
    else if (digitalRead(button2) == HIGH) {
      Position -= 20;
      Serial.println("Moving to Position 2: 20 rotations backward");
      return -20;
    }
    else if (digitalRead(button3) == HIGH) {
      Serial.println("Already at the position");
      return 0;
    }
    else if (digitalRead(buttonh) == HIGH) {
      Position -= 60;
      Serial.println("Moving to Position h: 40 rotations backward");
      return -60;
    }
}
}

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

I am trying to control 2 stepper motors (NEMA 23) simultaneously according to the 4 button inputs.

button h should behave as an input to make motors come back to initial position button 1 should behave as an input to make motors go to Position 1 (20 rotations above from initial position) button 2 should behave as an input to make motors go to Position 2 (40 rotations above from initial position) button 3 should behave as an input to make motors go to Position 3 (60 rotations above from initial position)

Code successfully compiled in Arduino IDE. However, any of the buttons do not make motors rotate.
Circuit construction was verified valid by an acknowledged specialist, and various buttons were tested out in the circuit to test defect in buttons, as well as wires and component ports. By means, there is something wrong with the code.

Please give me any suggestion and solution to modify the code’s behavior, and let me know if there are any program defects that may cause the problem.

I am using L298N Dual H Bridge module, and Arduino Mega

Thank you!

Hello there!

Please try to post code using the code tags. It helps others find problems easier.

That being said, it seems that your buttons indicate action when they are pressed. Therefore, are there pull-down resistors to prevent input pin floating?

Code successfully compiled in Arduino IDE

No its doesn't it will stall on this line:-

rotate(command)

No semicolon on the end of that line.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code. And it will also tell you how to ask a question and what to include.
Hint a schematic of your hardware.

Circuit construction was verified valid by an acknowledged specialist,

That is your opinion, we assume nothing here.

That depends. Are they used as pull-ups or pull-downs?

Grumpy_Mike:
No its doesn't it will stall on this line:-

rotate(command)

No semicolon on the end of that line.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code. And it will also tell you how to ask a question and what to include.
Hint a schematic of your hardware.That is your opinion, we assume nothing here.

Thank you for the information, I have edited my post.
rotate(command) was a copy & paste issue, I fixed that part as well.
Providing schematic will take some time. Meanwhile, I would appreciate to figure out if there are any defects in coding

I would appreciate to figure out if there are any defects in coding

Sorry but without a schematic the code is more or less meaningless. As it stands it does not make much sense to me.

bos1714:
That depends. Are they used as pull-ups or pull-downs?

I have read [https://www.arduino.cc/en/Tutorial/DigitalPins].
The circuit currently has resistors placed between every buttons, and they eventually connect to both +5V and Ground along the connections to Arduino. Are you suggesting to focus on the circuit setup?
Above link also states "A 10K resistor is a good value for a pullup or pulldown resistor."

What do you mean they eventually connect to 5V and GND? The pull resistor should connect from the pin to GND only.

All that SWITCH CASE stuff looks horribly convoluted. This seems to be a test or learning program. If so make it simple.

For example have a variable that is incremented (or decremented) by 1 every time the button is pressed.

Then depending on the value of the variable move a specified number of steps and use another variable to keep track of the fact that the movement has been done so nothing more happens until the next button press.

If you want two motors to move at the same time the standard Stepper library is not really suitable as it blocks until it completes a move. You can get around that by getting it to move one step at a time and controlling the speed with your own code. But a better option is to use the AccelStepper library

...R
Stepper Motor Basics
Simple Stepper Code

bos1714:
What do you mean they eventually connect to 5V and GND? The pull resistor should connect from the pin to GND only.

I will need to look more into how circuit is constructed and attach current schematic later on today (I have not constructed the circuit, other person provided it). May I ask how the code will differ according to different roles of resistors in this case then?

May I ask how the code will differ according to different roles of resistors in this case then?

Depending on the configuration of the pull resistor a button press will register as a HIGH or a LOW.

It will also determin if the pinMode call needs INPUT or INPUT_PULLUP

If you have it wrong it will not register it at all.