I'm using a Arduino Due to driving steppers by a TB6560 stepper driver. The velocity of steppers should be controlled by commands over the Serial port. Per Serial communication, I send commands to do a reference drive (case 1) or drive with a certain velocity (case 2). Due to its necessary to receive dates every time it's not possible to use delays, so I generate the square wave signal with running throw a loop and change the Output only if necessary.
currentMic = micros();
if (currentMic - prevMic >= velocity) {
if (state_puls == HIGH) {
state_puls = LOW;
}
else {
state_puls = HIGH;
}
prevMic = currentMic;
}
digitalWrite(PUL1, state_puls);
This code works perfectly, but if I use communication (Serial.begin(115200); Serial.setTimeout(1);), it stops the process and it's only possible to generate a maximum frequency of 98Hz, but I must reach a maximum of 8kHz (with delayMicroseconds() it's possible).
currentMic = micros();
while (Serial.available()) {
while (!Serial.available()) {}
read_serial = Serial.readString();
}
if (read_serial.startsWith(string_drive_reference)) {
case_state = 1;
}
else if (read_serial.startsWith(string_drive_velocity)) {
case_state = 2;
DIR = read_serial.substring(2, 3).toInt();
faktor = read_serial.substring(3, 4).toInt();
value = read_serial.substring(4).toInt();
}
if (read_serial == "") {
return;
}
read_serial = "";
switch (case_state) {
case 1:
reference_drive1();
break;
case 2:
velocity = value * pow(10, faktor);
if (currentMic - prevMic >= velocity) {
if (state_puls == HIGH) {
state_puls = LOW;
}
else {
state_puls = HIGH;
}
prevMic = currentMic;
}
digitalWrite(PUL1, state_puls);
break;
}
My question is, is there a possibility to speed up the communication or generate a square wave signal where I can change the frequency continuously by using the Serial Communication?
stepper.ino (4.36 KB)