So I tried to work with multiple stepper motors and this is code that I ended up with. It still doesn´t work properly... My plan is to send commands from GUI written in python and syntax of command that is sent via serial looks like this: 0|1|365|0|365|1. Where first, third and fifth parameter are steps for different motors and second, fourth and sixth parameter are directions of motors.(I didn´t pay attention to proper direction in code yet.) So this is my code at the moment:
const int motorM1StepPin = 9;
const int motorM1DirPin = 8;
const int motorM2StepPin = 11;
const int motorM2DirPin = 10;
const int motorBaseStepPin = 3;
const int motorBaseDirPin = 2;
unsigned long previousMotorBaseTime = millis();
unsigned long previousMotorM1Time = millis();
unsigned long previousMotorM2Time = millis();
long motorBaseInterval = 0;
long motorM1Interval = 0;
long motorM2Interval = 0;
long ratioB = 0;
long ratioM2 = 0;
// Direction variables
int M1direction = 0;
int M2direction = 0;
int M1stepTotal = 100;
int M2stepTotal = 0;
int BaseStepsTotal = 0;
int BaseDirection = 0;
void Calc_Interval(unsigned long StepB, unsigned long StepM1, unsigned long StepM2);
void setup() {
Serial.begin(9600);
pinMode(motorM1StepPin, OUTPUT);
pinMode(motorM1DirPin, OUTPUT);
pinMode(motorM2StepPin, OUTPUT);
pinMode(motorM2DirPin, OUTPUT);
pinMode(motorBaseStepPin, OUTPUT);
pinMode(motorBaseDirPin, OUTPUT);
digitalWrite(motorBaseDirPin, HIGH);
digitalWrite(motorM1DirPin, HIGH);
digitalWrite(motorM2DirPin, HIGH);
}
void loop() {
if (Serial.available() > 0) {
Serial.println("Command RECEIVED!");
String command = Serial.readStringUntil('\n');
command.trim();
unsigned long currentMotorBaseTime = millis();
unsigned long currentMotorM1Time = millis();
unsigned long currentMotorM2Time = millis();
digitalWrite(motorBaseStepPin, LOW);
digitalWrite(motorM1StepPin, LOW);
digitalWrite(motorM2StepPin, LOW);
// Use strtok to split the command into tokens based on the '|' delimiter
char *token = strtok(const_cast<char*>(command.c_str()), "|");
int values[5]; // Assuming there are four values: BaseSteps, BaseDir, M1step, M1direction, M2step, M2direction
int i = 0;
while (token != NULL && i < 9) {
values[i++] = atoi(token);
token = strtok(NULL, "|");
}
BaseStepsTotal = values[0];
BaseDirection = values[1];
M1stepTotal = values[2];
M1direction = values[3];
M2stepTotal = values[4];
M2direction = values[5];
Calc_Interval(BaseStepsTotal, M1stepTotal, M2stepTotal);
for (int step = 0; step < M1stepTotal; step++){
digitalWrite(motorBaseStepPin, LOW);
digitalWrite(motorM1StepPin, LOW);
digitalWrite(motorM2StepPin, LOW);
if (currentMotorBaseTime - previousMotorBaseTime > motorBaseInterval){
digitalWrite(motorBaseStepPin, HIGH);
previousMotorBaseTime = currentMotorBaseTime;
}
if (currentMotorM1Time - previousMotorM1Time > motorM1Interval){
digitalWrite(motorM1StepPin, HIGH);
previousMotorM1Time = currentMotorM1Time;
}
if (currentMotorM2Time - previousMotorM2Time > motorM2Interval){
digitalWrite(motorM2StepPin, HIGH);
}
Serial.println(step);
}
}
}
void Calc_Interval(unsigned long StepB, unsigned long StepM1, unsigned long StepM2){
ratioB = StepM1 / StepB;
ratioM2 = StepM2 / StepB;
motorM1Interval = 1000;
motorBaseInterval = motorM1Interval * ratioB;
motorM2Interval = motorM1Interval * ratioM2;
}
Code compiled properly and motor loop run properly as well(I mean it sends number of loop everytime it runs through it), however motors don´t move...
Any ideas on how to fix that ?
Thank you very much