I am running 2 stepper motors:
NEMA 17 with TB6600
NEMA 23 with TMC2160
The code below relates to the NEMA 23 and TMC2160
I need to run the stepper until the IR sensor on INT0 is fired (LOW), I then STOP (Disable) the stepper.
Code:
#define stepper23dirPin 10
#define stepper23clkPin 11
#define stepper23enPin 13
#define irPin 2
bool loadingLabels = false;
int irPinStatus;
void INT0_IR(void) {
irPinStatus = digitalRead(irPin);
if (irPinStatus == 1) {
loadingLabels = true;
} else if (irPinStatus == 0) {
loadingLabels = false;
}
}
void setup() {
Serial.begin(115200);
pinMode(stepper23clkPin, OUTPUT);
pinMode(stepper23dirPin, OUTPUT);
pinMode(stepper23enPin, OUTPUT);
pinMode(irPin, INPUT);
attachInterrupt(digitalPinToInterrupt(irPin), INT0_IR, CHANGE);
loadingLabels = true;
}
void loop() {
irPinStatus = digitalRead(irPin);
if (loadingLabels) {
digitalWrite(stepper23enPin, LOW); // Enable Stepper23
if (irPinStatus == 1) {
Serial.println("Loading Labels");
digitalWrite(stepper23clkPin, HIGH);
digitalWrite(stepper23clkPin, LOW);
delayMicroseconds(300);
} else if (irPinStatus == 0) {
Serial.println("Loading Labels Stop");
digitalWrite(stepper23enPin, HIGH); // Disable Stepper23
loadingLabels = false;
}
}
//This code runs faster
// digitalWrite(stepper23enPin, LOW); // Enable Stepper23
// digitalWrite(stepper23clkPin, HIGH);
// digitalWrite(stepper23clkPin, LOW);
// delayMicroseconds(300);
}
This code runs ok(ish).
But, runs very slow.
If I run the commended-out code only, it runs much faster, yet the code controlling the stepper is the same.
Another question:
Can I remove the code from loop() and place in another function?
Wawa
May 24, 2025, 6:03am
2
No experience with that driver, but
Which Arduino. Would that pulse be long enough for the driver to react?
Declan:
delayMicroseconds(300);
Did you convert (calculate) that to steps per second, and can the motor do that from standstill? I calculate more than 3000 steps/second.
Leo..
Declan:
But, runs very slow.
Remove the "Serial.println()" statements.
This is my current code that works well.
#define stepper23dirPin 10
#define stepper23clkPin 11
#define stepper23enPin 13
#define irPin 2
bool loadingLabels = false;
int irPinStatus;
void INT0_IR(void) {
irPinStatus = digitalRead(irPin);
if (irPinStatus == 1) {
loadingLabels = true;
} else if (irPinStatus == 0) {
loadingLabels = false;
}
}
void setup() {
Serial.begin(115200);
pinMode(stepper23clkPin, OUTPUT);
pinMode(stepper23dirPin, OUTPUT);
pinMode(stepper23enPin, OUTPUT);
pinMode(irPin, INPUT);
attachInterrupt(digitalPinToInterrupt(irPin), INT0_IR, CHANGE);
loadingLabels = true;
}
void loop() {
irPinStatus = digitalRead(irPin);
if (loadingLabels) {
digitalWrite(stepper23enPin, LOW); // Enable Stepper23
if (irPinStatus == 1) {
digitalWrite(stepper23clkPin, HIGH);
digitalWrite(stepper23clkPin, LOW);
delayMicroseconds(300);
} else if (irPinStatus == 0) {
delay(500);
for (int n = 0; n < 300; n++) {
digitalWrite(stepper23clkPin, HIGH);
digitalWrite(stepper23clkPin, LOW);
delayMicroseconds(300);
}
digitalWrite(stepper23enPin, HIGH); // Disable Stepper23
}
}
}
How do I remove this from loop() and call it from void INT0_IR(void) in a separate function "loadLabels()"
xfpd
May 24, 2025, 3:56pm
6
bool flag = 0; // flag indicating ISR was called
void setup() {
// setup code
}
void ISR() { // your ISR callback
flag = 1; // indicate ISR was called
// ISR code
}
void loop () {
if (flag == 1) { // if ISR was called...
flag = 0; // ... reset ISR flag and...
loadLabels(); // ... call your function
}
}
void loadLabels() {
// your function
}
Declan
June 16, 2025, 6:31am
7
I have switched to the Accelstepper library