Multiple Stepper motors with ADA Board & potentiometers

Hey there

Totally new to the forum, but not to arduino - ive dabbled in some basics.

Basically im trying to find a way of controlling 2 stepper motors with 2 potentiometers. this is the code ive been using - im getting both of the motors to move in sync but not individually - please can someone help as im dying here!

#include <AFMotor.h> // imported AFMotor library

AF_Stepper motor1(200, 1); // 1.8 degree/step stepper motor attached to M1 and M2
AF_Stepper motor2(200, 2); // 1.8 degree/step stepper motor attached to M3 and M4

int motordriveA = 3;
int motordriveB = 5;

int delayTime = 500;
int val1 = 3;
int val2 = 5;

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper controlled by potentiometer test!”);

motor1.setSpeed(500); // 30 rpm

}
void moveForward() {
motor1.step(1, FORWARD, INTERLEAVE);
motor2.step(1, FORWARD, INTERLEAVE);

}

void moveBackward() {
motor1.step(1, BACKWARD, INTERLEAVE);
motor2.step(1, BACKWARD, INTERLEAVE);

}

void loop() {
int val1 = analogRead(3);
Serial.println(val1); // shows the pot value in the serial monitor
if (val1 > 540) {
// move faster the higher the value from the potentiometer
delayTime = 2048 - 1024 * val1 / 512 + 1;
moveForward();
} else if (val1 < 480) {
// move faster the lower the value from the potentiometer
delayTime = 1024 * val1 / 512 + 1;
moveBackward();
} else {
delayTime = 480;
}

int val2 = analogRead(5);
Serial.println(val2); // shows the pot value in the serial monitor
if (val2 > 540) {
// move faster the higher the value from the potentiometer
delayTime = 2048 - 1024 * val2 / 512 + 1;
moveForward();
} else if (val2 < 480) {
// move faster the lower the value from the potentiometer
delayTime = 1024 * val2 / 512 + 1;
moveBackward();
} else {
delayTime = 480;
}

}

sorry to be a pain. I dont quite understand as I've just cobbled this together looking at other code - would you mind removing the code i dont need and reposting?

        delayTime = 2048 - 1024 * val1 / 512 + 1;

Have you printed out the value of delayTime that you are computing? For any value in val1 greater than 32, the 1024 * val1 portion of this equation will overflow an int. The computation is done using ints because val1 and 1024 are both ints. You could force larger intermediate variable types to be used by using 1024L in the equation.

However, 1024 / 512 is 2, so the need to perform division escapes me.

Where is delayTime ever used?

Thank you so, so much. Its working a dream :-). You guys rock!

The only other thing i need to work out now is how to make the steppers run more quickly - the rpm doesnt make a massive difference…

Part of what controls the speed of a stepper motor is the current and voltage that is driving it. Not enough, and the stepper motor will be slow. How are you powering your stepper motor?

In previous tests i have managed to get the stepper motors to turn more quickly using the rpm function. Im running an extra 5v into the ada fruit motor board to increase torque. The motor steps by 1.8 degrees at a time, if i up the rpm its spins more quickly. At the moment the pots seem to be a bit temperamental in increasing and decreasing the speed of the direction of turning. Anyone any ideas how i might improve this?

if I use # const int stepsPerRevolution = 200 # in theory you would be able to make a full rotation quicker if you did less steps but it would also be less accurate - am i making sense?

if I use # const int stepsPerRevolution = 200 # in theory you would be able to make a full rotation quicker if you did less steps but it would also be less accurate - am i making sense?

No. If you lie about the physical characteristics of the actual motor you have, you can not expect the software to then drive it properly.

if i up the rpm its spins more quickly.

This is what it is supposed to do.

Your latest post seems to contradict your previous posts. Are you having an issue, or not? If so, what is the issue?

The latest code I have - the one that has been updated on here is working. However, I would like to be able to make the steppers run more quickly. In previous tests not using this code but just using the steppers with the example code in arduino, i've managed to get them to spin more quickly. What I would like to be able to do ( and currently cant) is use the existing code posted but have a much better degree of control of speed using the steppers.

Lay mans terms: I want to make them spin quicker using current code with a tweak.

What I would like to be able to do ( and currently cant) is use the existing code posted

Which code would that be? The code you posted, or the code someone else posted? We can't see over your shoulder. We can only offer advice on what is actually posted here.

the code updated on here...

#include <AFMotor.h>  // imported AFMotor library

    AF_Stepper motor1(200, 1);   // 1.8 degree/step stepper motor attached to M1 and M2
    AF_Stepper motor2(200, 2);   // 1.8 degree/step stepper motor attached to M3 and M4
    
    int motordriveA = 3;
    int motordriveB = 5;
   
 
    int delayTime = 500;
    int val1 = 3;
    int val2 = 5;
   


    void setup() {
       Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Stepper controlled by potentiometer test!"); 

    motor1.setSpeed(500);  // 30 rpm   

       
      }
    void moveForward() {   // you can not use this function. It moves both motors together <<<<<<<<<<<<<<<<<<<<<<<<
      motor1.step(1, FORWARD, INTERLEAVE);
      motor2.step(1, FORWARD, INTERLEAVE);
        
    }

    void moveBackward() {   // you can not use this function. It moves both motors together <<<<<<<<<<<<<<<<<<<<<<<<
      motor1.step(1, BACKWARD, INTERLEAVE);
      motor2.step(1, BACKWARD, INTERLEAVE);
 

    }

    void loop() {
      int val1 = analogRead(3);
      Serial.println(val1);  // shows the pot value in the serial monitor
      if (val1 > 540) {
        // move faster the higher the value from the potentiometer
        delayTime = 2048 - 1024 * val1 / 512 + 1;
        motor1.step(1, FORWARD, INTERLEAVE);   // was: moveForward(); <<<<<<<<<<<<<<<<<<<<<<<<
      } else if (val1 < 480) {
        // move faster the lower the value from the potentiometer
        delayTime = 1024 * val1 / 512 + 1;
        motor1.step(1, BACKWARD, INTERLEAVE); // was: moveBackward(); <<<<<<<<<<<<<<<<<<<<<<<<
      } else {
        delayTime = 480;
      }
      
      int val2 = analogRead(5);
      Serial.println(val2);  // shows the pot value in the serial monitor
      if (val2 > 540) {
        // move faster the higher the value from the potentiometer
        delayTime = 2048 - 1024 * val2 / 512 + 1;
        motor2.step(1, FORWARD, INTERLEAVE);   // was: moveForward(); <<<<<<<<<<<<<<<<<<<<<<<<
      } else if (val2 < 480) {
        // move faster the lower the value from the potentiometer
        delayTime = 1024 * val2 / 512 + 1;
        motor2.step(1, BACKWARD, INTERLEAVE);   // was: moveBackward(); <<<<<<<<<<<<<<<<<<<<<<<<
      } else {
        delayTime = 480;
      }

}

You still haven't explained why you are computing delayTime, when it is never used.

You haven't explained why you have not modified the code to make the motors behave independently, as you first asked how to do.

You haven't explained what changing the value in the call to setSpeed() accomplishes.

It's really hard to help you when you don't answer questions.

I have no idea what you are talking about. The code is cut and paste from other ones that i have found online. IF you read the beginning of the post it clearly says im NEW to this. I have no idea what im doing, thanks for the help.....

The code is cut and paste from other ones that i have found online.

We know it must be perfect then... Must be your hardware that is at fault then.

IF you read the beginning of the post it clearly says im NEW to this.

If you don't know why you are doing something, you should either learn why, or quit doing it.

One does not get from NEW to experienced without trying to understand why some code works and some doesn't. That comes from understanding what each line is doing.

Look at all the places in your code where delayTime occurs. It is always on the left side of the equal sign. That means that you are only ever assigning it a value. You never use that value that you assigned to delayTime, so assigning it a value is unnecessary. You can, and should, remove any useless code, to provide an example that illustrates your problem.

If this code does not allow you to get the speed of the motors as high as you'd like, but other code does, you need to post that code, too. We can't guess why some code works, while other code doesn't, without seeing both.

cool - thanks for being so patient- heres the code i was using to change the speed using different tests on the stepper. If i change the RPM here it works fine, however on the code previously posted the rpm doesnt seem to affect things at all.

P.s. the code im using is from here: Arduino motor/stepper/servo control - How to use

#include <AFMotor.h>


AF_Stepper motor(48, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(10);  // 10 rpm   

  motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop() {
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 

  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);

  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 

  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

That code calls AFMotor::release() in the setup function. Having done that, I can't see that the code in loop does anything.

I don't have a stepper motor handy, nor do I have a AdaFruit driver, so I can't verify that.

If that is the case, then the only differences, between that code that works (to adjst speed) and your code that doesn't, that appear relevant are the ways that the constructor and motor.step() are called.

In this code, the number of steps per revolution is 48. In your code, the number of steps per revolution is 200.

The correct value to specify is based on the stepper motor you have. I'm not sure that you can expect correct behavior for all functions if the wrong number of steps/revolution is specified.

In this code, the 3rd argument is SINGLE. In your code, the 3rd argument in INTERLEAVE.

What I would do is modify the code in which you can adjust the speed, to have setup() use INTERLEAVE as the 3rd argument, and see if the speed is still adjustable.

Then, I'd set the number of steps/revolution to 200, and see if the speed is still adjustable.