I have several stepper motors connected to Arduino and a python tkinter GUI to control them. Buttons in tkinter are bound to run motor when pressed and stop when released.
This behavior is handled by one function at the end of the loop:
void runMotor() {
if (activeMotor.run) { //If in active phase:
digitalWrite(activeMotor.dirPin, activeMotor.direction); //Set the direction.
long speed = ((60000000L) / ((long)activeMotor.RPM * ((long)MOTOR_REV * (long)DRIVER_MODE))) / 2; //Calculate the signal period for speed.
digitalWrite(activeMotor.stepPin, HIGH); //Pulse ON.
delayMicroseconds(speed); //Period halftime.
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
delayMicroseconds(speed); //Period halftime.
} else {
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
}
}
At the top of the loop I have a switch case that handles different serial commands sent by GUI and a function that changes which motor should be currently active/inactive.
void setActiveMotor(bool is_running, int step_pin=-1, int dir_pin=-1, int direction=-1) {
activeMotor.run = is_running; //Set active/inactive phase.
activeMotor.stepPin = step_pin; //Set step pin.
activeMotor.dirPin = dir_pin; //Set direction pin.
activeMotor.direction = direction; //Set direction for the stepper motor.
}
Both use a struct for an active motor:
struct ActiveMotor {
int stepPin;
int dirPin;
int direction;
int RPM;
bool run;
};
ActiveMotor activeMotor;
Now I want the GUI to display how many total steps were made by each motor on its button release and I want another struct to hold these values:
struct TotalSteps {
float motor1;
float motor2;
float motor3;
float motor4;
};
TotalSteps totalSteps;
I can do that by either creating another variable for activeMotor struct and using pointer to point to the variable of the currently used motor:
struct ActiveMotor {
int stepPin;
int dirPin;
int direction;
int RPM;
bool run;
float *steps;
};
void setActiveMotor(bool is_running, float &motorTotalSteps, int step_pin=-1, int dir_pin=-1, int direction=-1) {
activeMotor.run = is_running; //Set active/inactive phase.
activeMotor.stepPin = step_pin; //Set step/pulse pin.
activeMotor.dirPin = dir_pin; //Set direction pin.
activeMotor.direction = direction; //Set direction for the stepper motor.
activeMotor.steps = &motorTotalSteps;
}
void runMotor() {
if (activeMotor.run) { //If in active phase:
digitalWrite(activeMotor.dirPin, activeMotor.direction); //Set the direction.
long speed = ((60000000L) / ((long)activeMotor.RPM * ((long)MOTOR_REV * (long)DRIVER_MODE))) / 2; //Calculate the signal period for speed.
digitalWrite(activeMotor.stepPin, HIGH); //Pulse ON.
delayMicroseconds(speed); //Period halftime.
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
delayMicroseconds(speed); //Period halftime.
*activeMotor.steps += MICROSTEP_SIZE; //Add 1/8 step (motor driver step mode.)
} else {
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
}
}
Or by making a global variable that will contain an "id" of an active motor and then processing it with two switch cases:
int activeMotorID;
void setActiveMotor(bool is_running, int motor_id, int step_pin=-1, int dir_pin=-1, int direction=-1) {
activeMotor.run = is_running; //Set active/inactive phase.
activeMotor.stepPin = step_pin; //Set step/pulse pin.
activeMotor.dirPin = dir_pin; //Set direction pin.
activeMotor.direction = direction; //Set direction for the stepper motor.
switch (motor_id) {
case 1:
activeMotorID = 1;
break;
case 2:
activeMotorID = 2;
break;
case 3:
activeMotorID = 3;
break;
case 4:
activeMotorID = 4;
break;
}
}
void runMotor() {
if (activeMotor.run) { //If in active phase:
digitalWrite(activeMotor.dirPin, activeMotor.direction); //Set the direction.
long speed = ((60000000L) / ((long)activeMotor.RPM * ((long)MOTOR_REV * (long)DRIVER_MODE))) / 2; //Calculate the signal period for speed.
digitalWrite(activeMotor.stepPin, HIGH); //Pulse ON.
delayMicroseconds(speed); //Period halftime.
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
delayMicroseconds(speed); //Period halftime.
switch (activeMotorID) {
case 1:
totalSteps.motor1 += MICROSTEP_SIZE;
break;
case 2:
totalSteps.motor2 += MICROSTEP_SIZE;
break;
case 3:
totalSteps.motor1 += MICROSTEP_SIZE;
break;
case 4:
totalSteps.motor1 += MICROSTEP_SIZE;
break;
}
} else {
digitalWrite(activeMotor.stepPin, LOW); //Pulse OFF.
}
}