0
votes

I'm doing a school project using a few stepper motors driven by drv8825 drivers (actually, it will be an overhead travelling crane), with an Arduino UNO. First of all, we haven't got a huge amount of money to spend. Now, the problem is this: we are going to use a PID control, through an MPU6050 acc+gyro, that will control the speed of the motors (the load is supposed to be as firm as possible during the movements), and the only two ways to change the speed of a stepper motor are:

1) Through a delay method, for example:

for (i = 0; i<400; i++)
{
    digitalWrite(StepPinB, LOW);
    delayMicroseconds(500);
    digitalWrite(StepPinB, HIGH);
    delayMicroseconds(500); 

}

Using this method, the Arduino will be busy all the time, so I will not be able to check continuously the value of the sensor, in order to reach the correct speed. Because of this, I can't use this method.

2) Through an external regulation of the drv8825's clock frequency, for example using a "voltage-frequency converter" (the variable voltage could be given by an "Arduino PWM" + "low-pass filter") or using an "astable multivibrator" ("astable oscillator") with a "digital potentiometer" to change the frequency. I've already tried the "voltage-frequency converter" method, but it didn't work due to the noises of the circuit.

I'd like you to help me to find out what's the better way to change the stepper motor's speed continuously during the program (and also to find the better way to make a variable frequency, as shown in the point 2).

2

2 Answers

1
votes

you are using the same time (500us) so the code below may suits you (using arduino millis() funcion):

   unsigned long timerBefore = 0; //global variable
   int inc=0;
   void Temporize(){
      unsigned long timerNow=millis();
      if((unsigned long)(timerNow-timerBefore)>=(500){
         timerBefore=millis();
         inc++;
         if(inc==1)
           digitalWrite(StepPinB, LOW);
         if(inc==2){
           digitalWrite(StepPinB, HIGH);
           inc=0;
         }
     }
 }

then you can call that function inside of that for without slave your microcontroller like your code.

0
votes

This is not a concrete Answer, because my Reputation doesn't allow me to comment. But I have another suggestion: Have you thought about a Timer Interrupt instead of the delays.

#include "TimerOne.h"

long X{1000000};
int volatile altVar{1};

void clockSignal();

void setup(){
  timer.initialize(X);
  timer.attachInterrupt(clockSignal());
}

void loop(){
  readSensor();
}

// Gets called every X cycles;
void clockSignal(){
  if(altVar == 1){
    digitalWrite(stepPinB, LOW);
  }else{
    digitalWrite(stepPinB, HIGH);
  }
  altVar *= -1;
}

You will profit of a completly free Loop function. There is space and time for everything you want to do and it gets only shortly Interrupted for giving the Clock cycle.