■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
/*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!