Hello
I am using a TB6600 stepper driver using the code example below and it works fine but I need a way for it to work without using delayMicroseconds, because I don't want delays when I eventually start adding other functions. I've tried using other available libraries but they require inputs of 2 or 4 pin connections when I am using 3 Arduino pins. I've tried using the example of "blink without delays" but the motor starts to stutter & doesn't run properly.
I am new to Arduino & would greatly appreciate if someone could put me on the right path to write a function(s) to control my stepper motor independently.
Thanks in advance.
CODE:
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize the serial port:
Serial.println("Ready to go!!!");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
for (int x = 0; x < 200; x++) { //previously 800
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}
Hi LarryD
Please see code below. 500 micros is the maximum time between digital writes before the motor throws a wobble. So it seems like when I add functions & additional code that causes a problems as it would take more than 500 micros to make the next digitalWrite, rather than having only a simple "for loop" in my sketch..
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
int x = 0;
unsigned long previousMicros = 0;
const long interval = 500;
unsigned long previousMicros2 = 0;
const long interval2 = 500; //
unsigned long checkintervalfinished = 0;
unsigned long checkintervalfinished2 = 0;
unsigned long currentMicros;
unsigned long currentMicros2;
int y = 0;
int HIGHonoff =0;
int LOWonoff =1;
int seconddelay=0;
int firstdelay=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize the serial port:
Serial.println("Ready to go!!!");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
currentMicros = micros();
currentMicros2 = micros();
// runonce
if (y == 0){
y = 1;
checkintervalfinished = currentMicros;
checkintervalfinished2 = currentMicros;
previousMicros = currentMicros;
previousMicros2 = currentMicros;
digitalWrite(dirPin, HIGH);
}
if (x < 200) {
Serial.println("looping 222");
x++;
loop1();
// -------------------------loop2();
}
}
void loop1(){
if(HIGHonoff ==0){
digitalWrite(stepPin, HIGH);
HIGHonoff=1;
}
if(firstdelay ==0){
if (micros() >= checkintervalfinished + interval ){
// if (currentMicros - previousMicros >= interval) {
//Serial.println(currentMicros);
Serial.println("firstdelay");
// previousMicros = currentMicros;
digitalWrite(stepPin, LOW);
LOWonoff = 0 ;
firstdelay = 1;
}
}
if(LOWonoff ==0){
Serial.println("LOWonoff");
LOWonoff = 1 ;
checkintervalfinished = currentMicros;
checkintervalfinished2 = currentMicros;
previousMicros = currentMicros;
previousMicros2 = currentMicros;
// seconddelay
// loop2();
}
if (firstdelay == 1 ) loop2();
}
void loop2(){
if (micros() >= checkintervalfinished2 + interval ){
//if (currentMicros2 - previousMicros2 >= interval2) {
//Serial.println(currentMicros2);
Serial.println("loop2");
checkintervalfinished = currentMicros;
checkintervalfinished2 = currentMicros;
previousMicros = currentMicros;
previousMicros2 = currentMicros;
firstdelay=0;
seconddelay=0;
HIGHonoff=0;
Serial.println("break");
//digitalWrite(stepPin, LOW);
}
}
void runonce(){
digitalWrite(dirPin, HIGH);
Serial.println(x);
if (x < 200) { //previously 800
x++;
delayMicroseconds(500);
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
}
//else (digitalWrite(stepPin, LOW));
}
I've installed MobaTools library and it seems to be on the right track, the only problem is the comments in the code are written in German , so its taking me a while to work it out using google translate and I also need to figure out how to make it work without using button pins in the example.
Thanks for the advice but AccelStepper functions require 4 pins, while I am only using 3 pins (stepPin = 5, dirPin = 2 and enPin = 8).
Also, 500 micros is the maximum delay between digitalWrite(s) before my motor starts to malfunction, so I can't get it working with millis. I tried 0.5 millis which I thought would be the same as 500 micros but to no avail.
Thank you all for your advice and recommendations. The only example I managed to get working properly was MobaTools library (now that I have the english version of the manual).
However, there is one issue using MobaTools library, whenever the motor stops moving it seems like it de-magnetises the coils, so the motor shaft has no resistance/tension which I need to hold my robot arm in position until I am ready to move to the next position.
This does not not happen if I use a simple for loop and delayMicroseconds but I would prefer to use MobaTools library, however I can't see anything in the user manual that address this issue?
That can only be done by the enable input of the driver. This is done by MoToStepper only if you explicitely tell MoToStepper to do so ( with the attachEnable() method ). If you don't attach the enable pin to MoToStepper it has no possibility to disable the coils.