Need help with my millis code

Hi i needed a delay function in my code so instead of using delay i used the millis function because
dont want a timer that pause my program. So i wrote my millis code and it worked till when i where
trying it out i noticed its working like a delay it pauses the program so it cant multi task till the 3 sec is up. Could any one help me out?

Its not all of the program i took only out what the code that i need help with.

boolean timing = false;
unsigned long startTime;

void setup() {


}

void loop() {
            //switch 3 off deley restart function
          if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
          {
            //What it does.
            actionTransmitter.sendSignal(1, 'C', false);
            switchstatus3 = false;
            timing = true;
            startTime = millis();
            if (timing) {

              while (millis() - startTime < 3000); 

              actionTransmitter.sendSignal(1, 'C', true);
              switchstatus3 = true;
            }
          }

}

You have correctly identified the problem. You are using millis() timing incorrectly.

Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

The BlinkWithoutDelay example shows how to do this.

Just in case this point is not clear, the while statement is also “blocking” code. As long as “(millis() - startTime < 3000)” you are stalled.

–Michael

Edit: a second look reveals that your syntax is messed. “While” statements get brackets like “if” statements to enclose their relevant code, not a semicolon.
–Michael

The demo Several Things at a Time is an extended example of BWoD.

...R

mjward:
Just in case this point is not clear, the while statement is also “blocking” code. As long as “(millis() - startTime < 3000)” you are stalled.

–Michael

Edit: a second look reveals that your syntax is messed. “While” statements get brackets like “if” statements to enclose their relevant code, not a semicolon.
–Michael

There is nothing at all wrong with that syntax - it is an empty while loop. It does nothing, until the condition is true.

Sadly, that one statement performs EXACTLY the same function as delay().

Regards,
Ray L.

Ok i tried doing as Robin2's example but i failed. Only thing that works is actionTransmitter.sendSignal(1, 'C', false); nothing else. I may have missed something.

//timmer
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const int blinkDuration = 500;
const int Interval = 6000;

void setup() {
  

}

void loop() {

          currentMillis = millis();
          //switch 3 off deley on function. restart function
          if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
          {
            if (currentMillis - previousMillis >= Interval) {
              actionTransmitter.sendSignal(1, 'C', false);
              switchstatus3 = false;
              previousMillis += Interval;
            }
            else {
              if (currentMillis - previousMillis >= blinkDuration) {
                actionTransmitter.sendSignal(1, 'C', true);
                switchstatus3 = true;
                previousMillis += Interval;
                previousMillis += blinkDuration;
              }
            }
          }
}

RayLivingston--

Well, I was half right anyhow.
Never saw that syntax before. Thanks for clueing me in.

--Michael

micky97:
Ok i tried doing as Robin2's example but i failed. Only thing that works is actionTransmitter.sendSignal(1, 'C', false); nothing else. I may have missed something.

You need to explain what you are trying to achieve.

I don't think that ELSE is appropriate in the way you have used it - I certainly don't recall using it like that in my examples. Remember that the IF for currentMillis might be tested 1000 times before it becomes true.

Just a thought ... does the ELSE belong to
if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
if so your }s are not right.

...R

Ok it was a bad try :slight_smile:
How about this? Am i getting closer?

unsigned long previousMillis = 0;
const int Interval = 6000;

void setup() {
  

}

void loop() {
          unsigned long currentMillis = millis();
          //switch 3 off deley on function. restart function
          if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
          {
            actionTransmitter.sendSignal(1, 'C', false);
            switchstatus3 = false;
            if (currentMillis - previousMillis >= Interval) {
              previousMillis = millis();
              actionTransmitter.sendSignal(1, 'C', true);
              switchstatus3 = true;
            }
          }
}

Here is the how it would look if i used delay

void setup() {


}

void loop() {

  //switch 3 off deley on function. restart function
  if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
  {
    actionTransmitter.sendSignal(1, 'C', false);
    switchstatus3 = false;
    delay (6000)
    actionTransmitter.sendSignal(1, 'C', true);
    switchstatus3 = true;

  }
}

Am i getting closer?

No. Look at the blink without delay code (again). Notice how, on each pass through loop(), irrespective of what has gone on before, it checks to see if it is time to do something. Can you say the same about your code?

Referring to the first piece of code in Reply #8, I think you need something like this

void loop() {
    unsigned long currentMillis = millis();
    //switch 3 off deley on function. restart function
    if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0)
    {
        actionTransmitter.sendSignal(1, 'C', false);
        switchstatus3 = false;
        previousMillis = currentMillis; // to get the clock started
        stringFound = true; // so the next piece only works after this piece
    }
    if (stringFound == true) {
        if (currentMillis - previousMillis >= Interval) {
            actionTransmitter.sendSignal(1, 'C', true);
            switchstatus3 = true;
            stringFound = false;
        }
    }
}

Notice that the timing piece is now separate from the detection of the valid string so the timing can be checked many times even if no data is received.

The variable previousMillis would probably be more appropriately named stringFoundMillis

...R

Robin2
Many thanks!! finally its working.