0
\$\begingroup\$

I am trying to control two different motors using an Arduino and some drivers. The problem I have is that using millis() (I need to use a timer without delay() because I want more than one motor at the same time) for delaying these motors is usually found in LED tutorials; they see numbers in milliseconds so it makes sense to set how many seconds you wanna blink.

I am trying to grab the potentiometer reading and convert it to the right numbers so my motors' pulses make sense.

This my code so far:

//1st Motor
int driverPUL = 7; // PUL- pin
int driverDIR = 6; // DIR- pin
int spd = A0;      // Potentiometer

// Variables
int pd = 500;      // Pulse Delay period
long previousMillis = 0; 
long interval = 1000; 

void setup() {
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
}
 
void loop() {
  pd = map((analogRead(spd)), 0, 1023, 2000, 50);
  unsigned long currentMillis = millis();

  /* The following code works for an LED, but I want it for the motor with potentionmeter
  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      digitalWrite(driverDIR,LOW);
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    // OLD CODE THAT WORKED WITH DELAY
    pd = map((analogRead(spd)), 0, 1023, 2000, 50);
    pd2 = map((analogRead(spd2)), 0, 1023, 2000, 50);
    digitalWrite(driverDIR, LOW);
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
*/
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Indeed, you can't really do things that way when you have more than one stepper motor that needs to move at the same time. To get an idea of how multiple motor movements actually are done, spend some time reading the code of open source arduino based 3d printer firmwares or more generic CNC g-code executors. \$\endgroup\$ Nov 18, 2020 at 5:43
  • \$\begingroup\$ @ChrisStratton but if youre able to control leds at different speeds why not motors and just vary the speed to the potentiometer \$\endgroup\$ Nov 18, 2020 at 5:54
  • 1
    \$\begingroup\$ A potentiometer is an input not an output. If you take time to understand what driving a stepper motor entails, you'll begin to understand why it's a bit tricky to drive two at differing rates in a way that maintains the evenness necessary for full rated torque. You probably don't want to "re-invent the wheel" here but rather study existing solutions of the sort mentioned above. \$\endgroup\$ Nov 18, 2020 at 5:56
  • \$\begingroup\$ Can you capitalise and punctuate the text properly for legibility? Your compiler wouldn't let you away with being so sloppy. Why should we? \$\endgroup\$
    – Transistor
    Nov 18, 2020 at 5:59
  • \$\begingroup\$ @ChrisStratton thank you i will study a cnc with ardunio, but just out of curiosity do you think accelstepper library can get it done? \$\endgroup\$ Nov 18, 2020 at 6:03

1 Answer 1

1
\$\begingroup\$

Why not using arduino libraries? For instance this StepperDriver library.

Usually all stepper drivers like A4988, DRV8825 and industrial drivers use STEP (or pulse) DIR (direction) and EN (enable). It should not be too complicate to connect the potentiometer readings.

Check out the libraries exemples about speed here

Hope that's helpful.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.