Unable to move two different motors at different times

Hello,
I am basically new in coding and my main goal for this project is t move two stepper motors (28BYJ-48). In the void loop, the first two if statements work as intended. Top motor moves in between 25000milliseconds and the bottom motor moves in between the 45000milliseconds but after that, the rest of the if statements do not work. Don't know what I am doing wrong but I would appreciate any help

`//Downloaded the AccelStepper Library to control more than one motor :smiley:
#include <AccelStepper.h>

//Define step constant...4=full and 8=half
#define FULLSTEP 4

AccelStepper Top_motor (FULLSTEP,4, 6, 5, 7);
AccelStepper Lower_motor (FULLSTEP, 10, 12, 11, 13);

//This is the internal timer for Arduino
int timer;

void setup() {
//Timer
Serial.begin(9600);
millis();

//Motor stuff
Serial.begin(9600);
//Motor 1 (Top) ...max speed is 1000...
Top_motor.setMaxSpeed(1000.0);
Top_motor.setAcceleration(50.0);
Top_motor.setSpeed(400);
Top_motor.moveTo(4000);

//Motor 2 (Lower) NOITCE how the negative will move it counterclockwise if I wanted (on 2038)
//4000 is the max amount of steps which is like about 15 secs at a speed of 200...accel of 50.
Lower_motor.setMaxSpeed(1000.0);
Lower_motor.setAcceleration(50.0);
Lower_motor.setSpeed(400);
Lower_motor.moveTo(4000);
}

void loop() {
//Time begins

timer = millis();

//Move the top motor counterclockwise but do not move the bottom motor... the 4000 is on average 15 secs

//setCurrentPosition(0) as a restart? between code or maybe add stop();

if (timer > 0) {
Top_motor.moveTo(4000*-1);
Lower_motor.move(0);
Top_motor.run();
Lower_motor.run();
}

//Do not move the top motor but move the bottom motor clockwise...after two seconds to let it have time to stop and restart
if (timer > 25000) {

Top_motor.move(0);
Lower_motor.moveTo(4000);
Top_motor.run();
Lower_motor.run();
}

//reset

//Move the top motor clockwise but do not move the bottom motor

if (timer > 45000) {
Top_motor.moveTo(4000);
Lower_motor.setCurrentPosition(0);
Top_motor.run();
Lower_motor.run();
}

//Do not move the top motor but move the bottom motor counterclock

if (timer > 65000) {
Top_motor.moveTo(0);
Lower_motor.runToNewPosition(-Lower_motor.currentPosition());
Top_motor.run();
Lower_motor.run();
}

//Move the top motor counterclockwise but do not move the bottom motor

if (timer > 85000) {
Top_motor.runToNewPosition(4000*-1);
Lower_motor.moveTo(0);
Top_motor.run();
Lower_motor.run();
}

//Stops moving
if (timer > 105000) {
Top_motor.stop();
Top_motor.disableOutputs();
Lower_motor.stop();
Lower_motor.disableOutputs();
Top_motor.run();
Lower_motor.run();
}
}`

Post your code in tags </>.

Maybe some debug (Serial.println) statements would help show what it's doing.

Please read the first topic telling how to get the best from this forum.
Notice the use of autoformatting and code tags.
Anyway, good coding not using delays.

//Downloaded the AceelStepper Library to control more than one motor :D
#include <AccelStepper.h>

//Define step constant...4=full and 8=half
#define FULLSTEP 4

AccelStepper Top_motor (FULLSTEP,4, 6, 5, 7);
AccelStepper Lower_motor (FULLSTEP, 10, 12, 11, 13);

//This is the internal timer for Arduino
int timer;

void setup() {
//Timer
Serial.begin(9600);
millis();

//Motor stuff
Serial.begin(9600);
//Motor 1 (Top) ...max speed is 1000...
Top_motor.setMaxSpeed(1000.0);
Top_motor.setAcceleration(50.0);
Top_motor.setSpeed(400);
Top_motor.moveTo(4000);
  
//Motor 2 (Lower) NOITCE how the negative will move it counterclockwise if I wanted (on 2038)
//4000 is the max amount of steps which is like about 15 secs at a speed of 200...accel of 50.
Lower_motor.setMaxSpeed(1000.0);
Lower_motor.setAcceleration(50.0);
Lower_motor.setSpeed(400);
Lower_motor.moveTo(4000);
}

void loop() {
//Time begins

timer = millis();

//Move the top motor counterclockwise but do  not move the bottom motor... the 4000 is on average 15 secs

//setCurrentPosition(0) as a restart? between code or maybe add stop();

if (timer > 0) {
   Top_motor.moveTo(4000*-1);
 Lower_motor.move(0);
Top_motor.run();
Lower_motor.run(); 
}

//Do not move the top motor but move the bottom motor clockwise...after two seconds to let it have time to stop and restart
if (timer > 25000) {
 
 Top_motor.move(0);
 Lower_motor.moveTo(4000);
Top_motor.run();
Lower_motor.run(); 
}

//reset

//Move the top motor clockwise but do not move the bottom motor

if (timer > 45000) {
 Top_motor.moveTo(4000);
 Lower_motor.setCurrentPosition(0);
Top_motor.run();
Lower_motor.run(); 
}

//Do not move the top motor but move the bottom motor counterclock

if (timer > 65000) {
 Top_motor.moveTo(0);
 Lower_motor.runToNewPosition(-Lower_motor.currentPosition());
Top_motor.run();
Lower_motor.run(); 
}

//Move the top motor counterclockwise but do not move the bottom motor

if (timer > 85000) {
 Top_motor.runToNewPosition(4000*-1);
 Lower_motor.moveTo(0);
Top_motor.run();
Lower_motor.run(); 
}

//Stops moving
if (timer > 105000) {
  Top_motor.stop();
  Top_motor.disableOutputs();
  Lower_motor.stop();
  Lower_motor.disableOutputs();
  Top_motor.run();
Lower_motor.run(); 
}
}

You missed using the autoformat in the IDE bu I'll try to read it.

Know that millis() start from 0 and advances endlessly.
Look at time being greater than 105000. Every if part will go active. That doesn't look like useful.
Use time intervals like if( time > this && time <= that) { do the action1; }

timer a 16bit int holds what number as its highest?

change that to

unsigned long timer;

and you'll be able to handle numbers > 32K.

Thanks!

Would you suggest to follow up the an if-statement with else-if?

Thanks for the help...probably explains the issue on why the other if-statements did not want to active :D.

That would save some microseconds in the loop. Not bad but makes no significant change in the motor movements.

Hello
Well, I guess a FSM and a time handler could help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.