Some motors running, others aren't with code

Hi, everyone.
So, I'm working on a project using an Arduino Mega 2560 Rev3 attached to a RAMPS 1.4 Board. It's not for a RepRap 3D printer, but for something completely unrelated. The Arduino has 4 motors and 20x4 LCD screen attached to it. Two are Stepper Motors, and the other Two are Screw Linear Motors.

I created a program to test the motors, but when I run it, only one of the motors (a NEMA 17 Stepper Motor -known as SM 2) actually turns. The code isn't complex at all, but it still isn't working. I know the other 3 motors are actually working, because when I run the actual open source program to test it, they run for a little bit. But because of a problem - perhaps the same reason my test code isn't working - after running for a little, they fail to execute the other 95% of the program. The LCD Screen works 100% fine.

Can anyone help me out? Please let me know if there's any information you need. I've attempted to format my Motor Test Code below, as well as attached the code for the Motor Test and the open source program as well.
original_AMT_code.ino (35.1 KB)
motors_test.ino (3.2 KB)

[table][tr][td][size=14pt][sub]Code:[/sub][/size][hr][/td][/tr][tr][td][size=9pt][tt]#include <AccelStepper.h>
#include <LiquidCrystal.h>

//======= LCD ========================================================
#define LCD_SPEAKER 37

#define LCD_RS 16
#define LCD_Enable 17
#define LCD_D4 23
#define LCD_D5 25
#define LCD_D6 27
#define LCD_D7 29
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
//======= ROTARY======================================================
#define ROTARY_EN1 31
#define ROTARY_EN2 33
#define ROTARY_BUTTON 35
Rotary rotary = Rotary(ROTARY_EN1, ROTARY_EN2);
//======= ENDSTOPS ===================================================
#define END_STOP_1 3
#define END_STOP_2 2
#define END_STOP_3 14
#define END_STOP_4 15
//======= STEPPERS ===================================================
#define USEDRIVER 1
//---- Stepper Motor 1 (RAMPS X)-------------------------------------
#define SM_1_STEP_PIN 54
#define SM_1_DIR_PIN 55
#define SM_1_ENABLE_PIN 38
AccelStepper SM_1 = AccelStepper(USEDRIVER, SM_1_STEP_PIN, SM_1_DIR_PIN);
//---- Stepper Motor 2 (RAMPS Y)-------------------------------------
#define SM_2_STEP_PIN 60
#define SM_2_DIR_PIN 61
#define SM_2_ENABLE_PIN 56
AccelStepper SM_2 = AccelStepper(USEDRIVER, SM_2_STEP_PIN, SM_2_DIR_PIN);
//---- Stepper Motor 3 (RAMPS E0)-------------------------------------
#define SM_3_STEP_PIN 26
#define SM_3_DIR_PIN 28
#define SM_3_ENABLE_PIN 24
AccelStepper SM_3 = AccelStepper(USEDRIVER, SM_3_STEP_PIN, SM_3_DIR_PIN);
//---- Stepper Motor 4 (RAMPS E1)-------------------------------------
#define SM_4_STEP_PIN 36
#define SM_4_DIR_PIN 34
#define SM_4_ENABLE_PIN 30
AccelStepper SM_4 = AccelStepper(USEDRIVER, SM_4_STEP_PIN, SM_4_DIR_PIN);

void initStepper(void *s, int ENABLE_PIN)
{ AccelStepper *stepper = (AccelStepper *)s;
stepper->setMaxSpeed(200 * 16 * 15000); //Faster than original speed

stepper->setAccelerationcolor=#000000[/color];
stepper->setMinPulseWidthcolor=#000000[/color];

stepper->setEnablePincolor=#000000[/color];
stepper->setPinsInverted(false, false, true); //invert logic of enable pin
stepper->enableOutputscolor=#000000[/color];
stepper->stopcolor=#000000[/color];
}

void setTwisterStepper(void *s, float maxspeed, float acccel)
{ AccelStepper *stepper = (AccelStepper *)s;
stepper->setMaxSpeedcolor=#000000[/color];
stepper->setAccelerationcolor=#000000[/color];
}
void setupcolor=#000000[/color] {
//Sets the maximum speed in steps per second:
SM_1.setMaxSpeed(1000 * 16 * 2);
SM_2.setMaxSpeed(1000 * 16 * 2);
SM_3.setMaxSpeed(1000 * 16 * 2);
SM_4.setMaxSpeed(1000 * 16 * 2);
SM_1.setEnablePincolor=#000000[/color];
SM_3.setEnablePincolor=#000000[/color];
SM_4.setEnablePincolor=#000000[/color];
SM_1.enableOutputscolor=#000000[/color];
SM_3.enableOutputscolor=#000000[/color];
SM_4.enableOutputscolor=#000000[/color];
SM_1.stopcolor=#000000[/color];

SM_3.stopcolor=#000000[/color];
SM_4.stopcolor=#000000[/color];

lcd.print("Is this working?");
}

void loopcolor=#000000[/color] {
// put your main code here, to run repeatedly:
//Sets the speed in steps per second:
SM_1.setSpeedcolor=#000000[/color];
SM_2.setSpeedcolor=#000000[/color];
SM_3.setSpeedcolor=#000000[/color];
SM_4.setSpeedcolor=#000000[/color];
//Step the motor with a constant speed as set by set speed:
SM_1.runSpeedcolor=#000000[/color];
SM_2.runSpeedcolor=#000000[/color];
SM_3.setCurrentPositioncolor=#000000[/color];
SM_4.setCurrentPositioncolor=#000000[/color];
SM_3.moveTocolor=#000000[/color];
SM_4.moveTocolor=#000000[/color];

}[/tt][/size][/td][/tr][/table]

Why not just post the code in code tags ?

You have only enabled the outputs for SM_1, SM_3, and SM_4. You are only calling runSpeed for SM_1 and SM_2. From what I can tell it looks like only one motor will actually work...

To clarify, in a earlier version of the test, I called runSpeed() on SM_3 and SM_4 as well. However, they still weren't working. Using moveTo() resulted in similar failure.
SM_2 is the motor which is working.

My understanding is you always have to call runSpeed for all of the motors.

runSpeed()
boolean AccelStepper::runSpeed ()
Poll the motor and step it if a step is due, implementing a constant speed as set by the most recent call to setSpeed(). You must call this as frequently as possible, but at least once per step interval

moveTo()
void AccelStepper::moveTo(long absolute)
Set the target position. The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().

I see. So should I call runSpeed() before calling moveTo()?

Since it is all in loop() I think it should work, but it is not necessary to call moveTo() over and over again.

Thanks for your help, but I tried it and it still didn't work.

Post your updated sketch. Maybe there is something else. I don't have the hardware to test right now.

#include <AccelStepper.h>
#include <LiquidCrystal.h>
#include <Rotary.h>

//======= LCD ========================================================
#define LCD_SPEAKER 37

#define LCD_RS 16
#define LCD_Enable 17
#define LCD_D4 23
#define LCD_D5 25
#define LCD_D6 27
#define LCD_D7 29
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
//======= ROTARY======================================================
#define ROTARY_EN1 31
#define ROTARY_EN2 33
#define ROTARY_BUTTON 35
Rotary rotary = Rotary(ROTARY_EN1, ROTARY_EN2);
//======= ENDSTOPS ===================================================
#define END_STOP_1 3
#define END_STOP_2 2
#define END_STOP_3 14
#define END_STOP_4 15
//======= STEPPERS ===================================================
#define USEDRIVER 1
//---- Stepper Motor 1 (RAMPS X)-------------------------------------
#define SM_1_STEP_PIN 54
#define SM_1_DIR_PIN 55
#define SM_1_ENABLE_PIN 38
AccelStepper SM_1 = AccelStepper(USEDRIVER, SM_1_STEP_PIN, SM_1_DIR_PIN);
//---- Stepper Motor 2 (RAMPS Y)-------------------------------------
#define SM_2_STEP_PIN 60
#define SM_2_DIR_PIN 61
#define SM_2_ENABLE_PIN 56
AccelStepper SM_2 = AccelStepper(USEDRIVER, SM_2_STEP_PIN, SM_2_DIR_PIN);
//---- Stepper Motor 3 (RAMPS E0)-------------------------------------
#define SM_3_STEP_PIN 26
#define SM_3_DIR_PIN 28
#define SM_3_ENABLE_PIN 24
AccelStepper SM_3 = AccelStepper(USEDRIVER, SM_3_STEP_PIN, SM_3_DIR_PIN);
//---- Stepper Motor 4 (RAMPS E1)-------------------------------------
#define SM_4_STEP_PIN 36
#define SM_4_DIR_PIN 34
#define SM_4_ENABLE_PIN 30
AccelStepper SM_4 = AccelStepper(USEDRIVER, SM_4_STEP_PIN, SM_4_DIR_PIN);

void initStepper(void *s, int ENABLE_PIN)
{ AccelStepper *stepper = (AccelStepper *)s;
stepper->setMaxSpeed(200 * 16 * 15000); //Faster than original speed

stepper->setAcceleration(200);
stepper->setMinPulseWidth(20);

stepper->setEnablePin(ENABLE_PIN);
stepper->setPinsInverted(false, false, true); //invert logic of enable pin
stepper->enableOutputs();
stepper->stop();
}

void setTwisterStepper(void *s, float maxspeed, float acccel)
{ AccelStepper *stepper = (AccelStepper *)s;
stepper->setMaxSpeed(maxspeed);
stepper->setAcceleration(acccel);
}
void setup() {
//Sets the maximum speed in steps per second:
SM_1.setMaxSpeed(1000 * 16 * 2);
SM_2.setMaxSpeed(1000 * 16 * 2);
SM_3.setMaxSpeed(1000 * 16 * 2);
SM_4.setMaxSpeed(1000 * 16 * 2);
SM_1.setEnablePin(SM_1_ENABLE_PIN);
SM_3.setEnablePin(SM_3_ENABLE_PIN);
SM_4.setEnablePin(SM_4_ENABLE_PIN);
SM_1.enableOutputs();
SM_3.enableOutputs();
SM_4.enableOutputs();
SM_1.stop();

SM_3.stop();
SM_4.stop();

lcd.print("Is this working?");
}

void loop() {
// put your main code here, to run repeatedly:
//Sets the speed in steps per second:
SM_1.setSpeed(2000);
SM_2.setSpeed(2000);
SM_3.setSpeed(2000);
SM_4.setSpeed(2000);
//Step the motor with a constant speed as set by set speed:
SM_1.runSpeed();
SM_2.runSpeed();
SM_3.runSpeed();
SM_4.runSpeed();
SM_3.setCurrentPosition(0);
SM_4.setCurrentPosition(0);
SM_3.moveTo(-1000);
SM_4.moveTo(-1000);

}
motors_test.ino (3.3 KB)

Try this. If this doesn't work you may have a signal inversion issue. It could still be code so you will have to get someone who is more familiar with AccelStepper to look at it. You should review the AccelStepper documentation carefully as I may have made some bad assumptions. You could also try move() instead of moveTo() but you will need to change the logic since move() moves to a relative position.

#include <AccelStepper.h>

//======= STEPPERS ===================================================
#define USEDRIVER 1
//---- Stepper Motor 1 (RAMPS X)-------------------------------------
#define SM_1_STEP_PIN 54
#define SM_1_DIR_PIN 55
#define SM_1_ENABLE_PIN 38
AccelStepper SM_1 = AccelStepper(USEDRIVER, SM_1_STEP_PIN, SM_1_DIR_PIN);
//---- Stepper Motor 2 (RAMPS Y)-------------------------------------
#define SM_2_STEP_PIN 60
#define SM_2_DIR_PIN 61
#define SM_2_ENABLE_PIN 56
AccelStepper SM_2 = AccelStepper(USEDRIVER, SM_2_STEP_PIN, SM_2_DIR_PIN);
//---- Stepper Motor 3 (RAMPS E0)-------------------------------------
#define SM_3_STEP_PIN 26
#define SM_3_DIR_PIN 28
#define SM_3_ENABLE_PIN 24
AccelStepper SM_3 = AccelStepper(USEDRIVER, SM_3_STEP_PIN, SM_3_DIR_PIN);
//---- Stepper Motor 4 (RAMPS E1)-------------------------------------
#define SM_4_STEP_PIN 36
#define SM_4_DIR_PIN 34
#define SM_4_ENABLE_PIN 30
AccelStepper SM_4 = AccelStepper(USEDRIVER, SM_4_STEP_PIN, SM_4_DIR_PIN);

void setup() 
{
  //Sets the maximum speed in steps per second:
  SM_1.setMaxSpeed(1000 * 16 * 2);
  SM_2.setMaxSpeed(1000 * 16 * 2);
  SM_3.setMaxSpeed(1000 * 16 * 2);
  SM_4.setMaxSpeed(1000 * 16 * 2);
  SM_1.setEnablePin(SM_1_ENABLE_PIN);
  SM_2.setEnablePin(SM_2_ENABLE_PIN);
  SM_3.setEnablePin(SM_3_ENABLE_PIN);
  SM_4.setEnablePin(SM_4_ENABLE_PIN);
  SM_1.enableOutputs();
  SM_2.enableOutputs();
  SM_3.enableOutputs();
  SM_4.enableOutputs();
  SM_1.setCurrentPosition(0);
  SM_2.setCurrentPosition(0);
  SM_3.setCurrentPosition(0);
  SM_4.setCurrentPosition(0);
}

void loop() 
{
  SM_1.moveTo(-1000);
  SM_2.moveTo(-1000);
  SM_3.moveTo(-1000);
  SM_4.moveTo(-1000);
  SM_1.setSpeed(2000);
  SM_2.setSpeed(2000);
  SM_3.setSpeed(2000);
  SM_4.setSpeed(2000);
  //Step the motor with a constant speed as set by set speed:
  SM_1.runSpeed();
  SM_2.runSpeed();
  SM_3.runSpeed();
  SM_4.runSpeed();
}

It still didn't work. Regardless, thank you so much for your help.

Hi,
Can you please post a schematic of your project, including power supply and component labels?
A hand drawn circuit will be fine, please not a Fritzy type image.

Can you also post a picture of your project, so we can see you component layout.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I don't know how to draw a schematic diagram, but here's one from the website.


Here's what it looks like on my end:

Here's what the total device should look like:
AMT-mooving (1)
And here's what it looks like on my end:

Let me know if I missed anything, or if anything's missing you need from the images.
The wires are a bit of mess now, but even when they're connected behind the device, it's not working.

So what do you think?

Hi,
Thanks for the info.
The Fritzy is not your circuit.
You need to reverse engineer your project and draw a schematic, that way you are checking ALL your wiring.

Do you have a DMM?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

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