While I was reading over my code, I read that I have used delayMicroseconds() in the motor function.
Is there any way that I can use the millis() to prevent it from delaying the code?
void motor(int pinNum)
{
digitalWrite(pinNum, HIGH);
delayMicroseconds(5000);
digitalWrite(pinNum, LOW);
}
Shubs,
As 5000 micro seconds is 5milliseconds, that can be implemented as a state machine.
Define the states and set up two variables, one to hold the state and one to hold the time.
#define mtrStable = 0
#define mtrOn = 1
#define mtrOff = 2
var byte mtrState;
var byte mtrCount;
mtrState = mtrStable;
mtrCount = 0;
Then in the loop, instead of calling the motor() function, change the motor state variable:
if (someChar == 'a'){
mtrState = mtrOn;
mtrCount = 500;
}
then later check the motor state variable:
if (mtrState == mtrOn){
digitalWrite(motPIN, HIGH);
mtrCount = 500;
mtrCount --;
if (mtrCount <= 0){
// time's up, so stop
mtrState == mtrOff;
}
}
if (mtrState == mtrOff){
digitalWrite(motPIN, LOW);
mtrState = mtrStable;
}
...
Something like that should work.
Regards,
Mike