I have this code for a DC motor (using this library)
#include <Arduino.h>
#include <Vnh2sp30.h>
// ENA A B PWM CS inv
Vnh2sp30 mtrL( A0, 7, 8, 5, A2, 0);
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
String first = Serial.readStringUntil(',');
Serial.read();
String second = Serial.readStringUntil(',');
Serial.read();
String third = Serial.readStringUntil(',');
Serial.read();
String echo = Serial.readString();
Serial.println(echo);
if(first == "start"){
Serial.println("Starting DC motor");
int duration = atoi(second.c_str());
int power = atoi(third.c_str());
int starttime = millis();
int endtime = starttime;
while ((endtime - starttime) <= duration) // mS
{
endtime = millis();
mtrL.run(1023);
}
mtrL.stop();
Serial.println("DONE");
}
}
}
Then I have this code to drive multiple stepper motors (using this library):
#include "BasicStepperDriver.h"
int incomingByte = 0; // for incoming serial data
String readString = "";
void setup() {
Serial.begin(9600);
motors.begin();
}
void loop() {
if (Serial.available() > 0) {
String first = Serial.readStringUntil(','); // start
Serial.read();
String second = Serial.readStringUntil(','); // motor number
Serial.read();
String third = Serial.readStringUntil(','); // motor steps
Serial.read();
String fourth = Serial.readStringUntil(','); // duration (millisec)
Serial.read();
String fifth = Serial.readStringUntil(','); // deg
if(first == "start") {
if(second == "ALL") {
Serial.println("Starting stepper motor ALL");
int steps = atoi(third.c_str());
int duration = atoi(fourth.c_str());
int deg = atoi(fifth.c_str());
BasicStepperDriver stepper1(steps, DIR, STEP, ENBL);
stepper1.enable();
stepper1.setMicrostep(MICROSTEPS);
stepper1.setRPM(MAX_RPM);
BasicStepperDriver stepper2(steps, DIR_2, STEP_2, ENBL_2);
stepper2.enable();
stepper2.setMicrostep(MICROSTEPS);
stepper2.setRPM(MAX_RPM);
BasicStepperDriver stepper3(steps, DIR_3, STEP_3, ENBL_3);
stepper3.enable();
stepper3.setMicrostep(MICROSTEPS);
stepper3.setRPM(MAX_RPM);
BasicStepperDriver stepper4(steps, DIR_4, STEP_4, ENBL_4);
stepper4.enable();
stepper4.setMicrostep(MICROSTEPS);
stepper4.setRPM(MAX_RPM);
int starttime = millis();
int endtime = starttime;
while ((endtime - starttime) <= duration) // mS
{
stepper1.rotate(deg);
stepper2.rotate(deg);
stepper3.rotate(deg);
stepper4.rotate(deg);
endtime = millis();
}
stepper1.disable();
stepper2.disable();
stepper3.disable();
stepper4.disable();
Serial.println("DONE");
}
}
}
}
My goal is to merge this two codes together and still be able to run both the DC motor and one (or all) the stepper motors all together simultaneously. The first code is running on Arduino UNO and the other code runs on Arduino MEGA board and has been tested to work. The goal is to migrate the code from Arduino UNO to the MEGA board. Driving the motor for a specific time will block Arduino from executing the Stepper motors and vice versa with the DC motor. What can be done with these codes to make sure it can run the DC motor and Stepper motors at the same time?
1 Answer 1
Use the AccelStepper library along with the Adafruit MotorShield library
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-software
-
I don't have a adafruit motor shield just the ones from Chinaxybrek– xybrek2016年11月29日 16:46:12 +00:00Commented Nov 29, 2016 at 16:46
-
The library is standard. You don't need the shield. Read the AccelStepper guide and it explains how to use motors and steppers at the same time.Visual Micro– Visual Micro2016年11月29日 16:48:16 +00:00Commented Nov 29, 2016 at 16:48
-
Would it work for ST Vnh2sp30 driver hardware? I'm not sure if the pinouts match.xybrek– xybrek2016年11月29日 16:51:31 +00:00Commented Nov 29, 2016 at 16:51
-
I managed to compile this for Stepper motor connections, problem is with the connection to the VNH2SP30 and if it would be non-blocking.xybrek– xybrek2016年11月29日 19:25:22 +00:00Commented Nov 29, 2016 at 19:25
-
1I think you van set the pins but the point of Accel is to do non-blocking.Visual Micro– Visual Micro2016年11月29日 23:44:29 +00:00Commented Nov 29, 2016 at 23:44
while ((endtime - starttime) <= duration) // ms
is what you need to change because it keeps the loop from doing other actions while driving the motor, hence it is referred to as blocking. The AccelStepper library mentioned by @Visual Micro uses a non-blocking approach but you need to change your approach. Use some variable to detect the duration and let the code in loop() constantly check it each time though instead of using a while loop. That way each motor can get the attention it needs instead of taking turns.