I have a program I am using to try and replace the delay() fn with millis().
It runs a motor in one direction for a period of time and then in the opposite. If I use the delay() fn in the Pause()fn it works perfectly. If I try and replace it with the Millis() fn (from Blinkwithoutdelay) it does not. I am obviously missing something, but what?
int MotorRun = 11;
int Direction1 = 8;
int Direction2 = 13;
//int Dwell = 3000;
int Speed = 100;
int Millis;
unsigned long currentMillis;
unsigned long previousMillis = 0;
const long Dwell = 3000;
void setup() {
Serial.begin(9600);
pinMode(MotorRun, OUTPUT);
pinMode(Direction1, OUTPUT);
pinMode(Direction2, OUTPUT);
Serial.println("In Setup");
currentMillis = Millis;
}
void FWD() {
digitalWrite(Direction2, HIGH); //Establishes forward direction of Channel B
digitalWrite(Direction1, LOW); //Disengage the Brake for Channel B
analogWrite(MotorRun, Speed); //Spins t // put your main code here, to run repeatedly:
Pause(Speed, Dwell);
Stop();
Pause(Speed, Dwell);
}
void REV() {
digitalWrite(Direction2, LOW);
digitalWrite(Direction1, LOW);
analogWrite(MotorRun, Speed);
Pause(Speed, Dwell);
Stop();
Pause(Speed, Dwell);
}
void Pause(int Speed, int Dwell) {
//delay(Dwell); // Replace with Millis()
{
Serial.println(" In Pause Function ");
unsigned long currentMillis = millis();
Serial.println(Speed);
if (currentMillis - previousMillis >= Dwell) {
previousMillis = currentMillis;
}
analogWrite(MotorRun, Speed);
}
}
void Stop() {
analogWrite(MotorRun, 0);
}
void loop() {
FWD();
REV();
}
To continue the current FWD, REV, Stop action for the period Dwell. As I say it does exactly that when I use Delay() but not when I try and use Millis().
Thank you. My understanding, from other fora, is that using While blocks the program. So, in this case, it is impossible not to block the rest of the program?
If you don't want Pause() to block, then you will need to refactor your code into more of a state machine. You can not just call Pause(), Stop(), Pause() if the Pause() function returns before it is done. If you google "Finite State Machine" you will get lots of examples.
Basically, you keep track of what state you are in and when to transition to the next state