I am trying to get a simple non-blocking replacement for:
digitalWrite(stepPinX, HIGH);
delayMicroseconds(500);
digitalWrite(stepPinX, LOW);
delayMicroseconds(500);
This is what I tried, and the stepper motor stays still:
const int dirPinX = 0;
const int stepPinX = 2;
unsigned long startMicros = micros();
unsigned long currentMicros;
const unsigned long period = 500;
void setup()
{
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
digitalWrite(dirPinX, HIGH);
}
void loop()
{
stepX();
}
void stepX()
{
currentMicros = micros(); //get the current "time" (time since program start)
if (currentMicros - startMicros >= period) //test whether period elapsed
{
digitalWrite(stepPinX, HIGH);
}
if ((currentMicros - startMicros >= period) && (currentMicros - startMicros <= period*2))
{
digitalWrite(stepPinX, LOW);
}
startMicros = currentMicros;
}
Any suggestions on how to correct this? (I do not want to use a library like Accelstepper. I want to use millis/micros.)
if
statement will execute only very infrequently ... why have a separateif
statement for the two output states?if (currentMicros - startMicros >= period)
... keep track ofstepPinX
state