millis() use question

Hi guys,

This will look for the condition in the while loop for 40mS before exiting the while loop, right?
If rightA and leftC do not occur in time (40mS), then the write for righttouchlight will not occur.

      timer1 = millis();
      timer1end = millis();
      while ((timer1end-timer1)<40 ) // wait to see if other touch occurs
      {
        rightA = digitalRead(right_A);  // 0 = right touch
        leftC = digitalRead(left_C); // 0 = right ground - no touch
        if (rightA == 0 && leftC == 1) // right touch scored in time
        {     
          digitalWrite(righttouchlight, LOW);
        }  // turn it on
        timer1end = millis();
      } // finish waiting for time

Hi guys,

Greetings.

This will look for the condition in the while loop for 40mS before exiting the while loop, right?

Yes.

Well, on average the while loop will run for slightly more than 39.5 ms. It will always run for more than 39ms and less than or equal to 40ms.

If rightA and leftC do not occur in time (40mS), then the write for righttouchlight will not occur.

Usually, something "occurs" if the digital is high (obviously not always). By "rightA occurring" you mean that rightA is LOW?

If the event occurs, should the loop be broken? Do you also want the full 40ms delay to occur?

Note: This question really belongs in a Software section.

You haven't shown the types of timer1 and timer1end.

There's lots of scope for things to work incorrectly if these are not longs and/or when millis() wraps.

For at least the first minute or so, you should be fine.

There’s lots of scope for things to work incorrectly if these are not unsigned longs and/or when millis() wraps.

I fixed that for you… :slight_smile:

Thanks guys. Maybe software would have better; I have a couple of swords and a bunch of hardware (well, 2 chips and a mess of LEDs) that I monitoring, seemed like hardware to me:-)

Yes, both are unsigned long type. This thing could be running all day - should I do something to reset millis() so I don't run into a situation of timer1 being something like

4,294,967,290 (FF FF FF FA hex)

and timer1end rolling to

0,000,000,005 (00 00 00 05 hex)

or will that just end in 00 00 00 0B, which would be a valid & correct result, as there are no higher bits to worry about?

(hmm, 4,294,967,295 mS / 1000 = 4,294,967 seconds / 60 = 71582 minutes /60 = 1194 hrs /24 = 49 days. Guess powering down at the end of the day will be enough!)

In this case the outputs of a comparator I am looking at are sitting high, the occurrence is on the low occurring. I only want the if condition to occur when one is low (=occurred) and the other has not. The comparator is open collector, so I am using logic low as On to turn on LEDs so I can see what the arduino is seeing to make its decisions on. (I suppose I could flip all the inputs around, and pull the anodes low to keep them turned off. I am confortable dealing with it either way).

If the event occurs, the righttouchlight will go on (pulling a cathode low) and the remaining 40mS becomes a don't care. There is an arbitrarily selected 3 second delay after the light goes to keep it visible, so I am not concerned about breaking out of the loop.

should I do something to reset millis()

How often do you reset your watch, to keep it from overflowing? Never, I'm guessing, and it overflows every day, precisely at midnight. But, you don't have any trouble know how long ago 3:13 PM yesterday was, do you?

If timer1 is 4,294,967,290, and timer1end is 5, what is timer1end - timer1?

The answer is 10. Subtracting across an overflow still works.

Well, I guess we are in agreement then! Thanks for the confirmation.