Running 2 or more motors at the same time

Hey Guys,

I am working on a project where I have to drive a stepper motor and BLDC motor with only one Arduino(Portenta Machine Contrl) at the same time. I am quite stuck on how to do this.

If anyone has some idea or suggestion, please do let me know.

Best Regards,
H_SE

Where are you stuck ?

Have you got sketches that run the 2 devices separately ?

How are the motors powered ?

Sample Sketch for Stepper,


#include <Arduino_MachineControl.h>
#include <Wire.h>
using namespace machinecontrol;
#define dirPin 1
#define stepP 6
#define stepsPerRevolution 400
int ftime = 0;
int ltime = 0;

float speed = 1000;

uint32_t stepPin  = stepP;
void setup()
{
  Serial.begin(9600);
  ftime = millis();
  digital_outputs.setAll(0);
  digital_programmables.setLatch();

}

void loop() 
{
  ltime = millis();
  if((ltime - ftime) > 10000)
  {
    digital_outputs.set(stepPin, 0);
  }
  else
  {
    for (int i=0; i <= 400; i++) {
      digital_outputs.set(stepPin, HIGH);
      delayMicroseconds(500);
      digital_outputs.set(stepPin, LOW);
      delayMicroseconds(500);
    }
    delay(1000);
  }
}

Sample sketch for BLDC

  #include <Arduino_MachineControl.h>
#include <Wire.h>

using namespace machinecontrol;

#define channel 0

int ftime = 0;
int ltime = 0;

float volt;
float max_speed = 4.5;
float max_volt = 5;

float motor_speed = 4.5;
float acc_time = 0.1;     //changng it odes not have any affect as the rate would also change
float acc = 2;
float dec = 2;


float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

float speed(float inp)
{
  float value = mapfloat(inp, 0, max_speed, 0, max_volt);
  return value;
}

float acceleration_rate(float acc_time, float acc)
{
  float velocity = acc * acc_time;
  float velocity_to_volt = velocity/(max_speed / max_volt);

  return velocity_to_volt;
}

float deceleration_rate(float acc_time, float dec)
{
  float dvelocity = dec * acc_time;
  float dvelocity_to_volt = (dvelocity/(max_speed / max_volt));

  return dvelocity_to_volt;
}

void setup()
{
  Serial.begin(9600);
  digital_outputs.setAll(0);
  digital_programmables.setLatch();
  analog_out.period_ms(0, 1);
  ftime = millis();

}

void loop()
{
  ltime = millis();
  volt = speed(motor_speed);
  float rate = acceleration_rate(acc_time,acc);
  float drate = deceleration_rate(acc_time,dec);
  if(((ltime - ftime) <= 15000) && (volt <= max_volt))
  {
    for(float i = 0;  i <= volt; i = i +rate)
    {
      analog_out.write(0, i);
      delay(acc_time * 1000);
   
    }
    // analog_out.write(0,volt);
    delay(5000);

    for(float i = volt; i>= 0; i = i - drate)
    {

      if(i < 0.125)      //0.12 is the limit, probably it is alos differnt for motors
      {
        i = 0;
        analog_out.write(0, i);
      }
      analog_out.write(0, i);
      delay(acc_time * 1000);


    }
    delay(2000);


  }
  else
  {
    // digital_outputs.set(5, LOW);
    analog_out.write(0, 0);
  }
}

I am controlling these motors with the help of Microstep driver and BLDC motor controller. They are also connected to a 24V power supply

The first thing that you will need to do is to get rid of the calls to delay() as when used to control one device it will prevent control of the other one and vice versa. Instead you need to implement non blocking timing, probably by using millis() for timing. are you familiar with that concept ?

If not then see Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Most, probably all for loops will have to go to and you will use the loop() function to do the looping for you in conjunction with millis() non blocking timing

A useful way to do this would be a state machine for each device so that you can control the section of code that is running for each device and its timing. Are you familiar with such a way of writing a program, probably by using switch/case to change between program states at the appropriate times

In summary, you have a lot of work on your hands to combine control of both devices and the 2 sketches cannot be simply merged for the reasons given above

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