using millis function in place of delay

Hi all -

There are plenty of suggestions for using millis instead of delay.

I’m struggling to introduce required delay using millis, below is what I’m trying to do, I, know time duration is not being incremented but do not know how I, write it

unsigned long currentmillis = millis();
while ((currentmillis < duration)) {
digitalWrite(8, HIGH);
// delay(20);
digitalWrite(8, LOW);
continue;

enclosing the code, pl suggest me how I, can achieve the delay with millis.

Thanks,

prg1-2-conscutive-error-pogrammer-with-reset.ino (3.23 KB)

I'm struggling to introduce required delay using millis

WHY are you trying to do that?

  attachInterrupt(pin, rising_edge_totalizer, RISING);//This logic fo due //2 pins are used as interrupt.

The first argument to attachInterrupt() is NOT a pin number.

  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);

Why are you writing pins 4 through 7 LOW and then microseconds later writing them HIGH?

rockwellramesha:
pl suggest me how I, can achieve the delay with millis.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

Always use subtraction when working with millis() - for example

if (millis() - lastTimeSomethingHappened >= intervalBetweenHappenings) {

...R

OOOOOOps!

Sorry,attached a wrong file, here is the one I’m working on

prg1-2-conscutive-error-pogrammer.ino (2.84 KB)

@Robin2
The fact that OP is using a while statement suggests to me that he/she is not approaching the problem from anywhere near the correct direction. It may well be that delay() is perfectly OK to use. It's hard to say, though, because the posted code doesn't use delay() at all.

While supplying links is perfectly reasonable most of the time, I don't think this is such a time.

    unsigned long currentmillis = millis();
    while ((currentmillis<duration)) {
      digitalWrite(8, HIGH);
      //    delay(20);
      digitalWrite(8, LOW);
      continue;
    }

It makes NO sense to compare now to an interval.

The continue statement will jump to the end of the block, bypassing any code between it and the end of the block. Using that statement as the last statement in the block is a sign of cluelessness.

Once the body of the while statement starts executing, it will NEVER end, because the condition, if true to start with, will remain true forever, since neither variable’s value is changed in the body of the while statement.

I, want to avoid delay when fault occurs switching on LED for 20 msecs,as I, will be missing counting the pulses.

I,just need LED to be "ON" for 20 ms on occurrence of fault and parallel thread of counting of pulses should continue

Pl suggest, how do I, work around for that

I, want to avoid delay when fault occurs switching on LED for 20 msecs,as I, will be missing counting the pulses.

That is nonsense, since the pulses are counted in ISRs triggered by interrupts which continue to happen while delay() is doing its head-in-the-sand bit.

Thanks!