How to reset a timer?

I have an IR sensor and 5 LED's.
So when any obstacle comes in front of the sensor, all the 5 leds must glow at once. And when the obstacle is removed away from the sensor , then each led should switch off separately with a time gap of 1 second...one after another.
If i use 'delay' in this code then there arises a problem.
When the led's are switching off separately and at that time if I put the obstacle back in front of the sensor, all the led's do not glow. I want them to glow at this point also. I think the delay command stops all the operations during its completion process.
So what can i do?
Please help! :slight_smile:

Saurabh98:
I think the delay command stops all the operations during its completion process.

You're right, it does exactly what is says on the box :slight_smile: Have a look at Blink without delay :slight_smile:

another way would be to use the Timer/Callback paradigm, which is event triggered and uses a timer to perform delayed functions. you may have to install the MsTimer2 library.

this example uses Direct Port Manipulation to affect the pins, so you must use the pins defined in the example. this just made it easier to do. :wink:

you will have to sort out if this is usable with the rest of your code... this essentially disables Timer2 for other uses, like PWM.

this compiles, not tested with LEDs

#include <MsTimer2.h>

const byte buttonPin = 5;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  DDRB = DDRB | 0b11111000; // digital pins {8,9,10,11,12} set to output here
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  if(buttonFivePressed()) // replace event trigger with your motion sensor...
  {
   sequenceLeds();
  }
}

void sequenceLeds(void)
{
  static byte leds = 0b11111000;  // are digital pins: {8,9,10,11,12}
  Serial.println(leds, BIN);
  if(leds == 0) 
  {
    PORTB = leds;
    leds = 0b11111000;
    MsTimer2::stop();
    return;
  }
  PORTB = leds;
  leds <<= 1;
  MsTimer2::set(1000, sequenceLeds); 
  MsTimer2::start();
}

bool buttonFivePressed(void)
{
  static unsigned long lastMillis = 0;
  static byte lastPress = HIGH;
  byte currentPress = digitalRead(buttonPin);
  if(currentPress != lastPress)
  {
    if(millis() - lastMillis < 200) return false;
    lastPress = currentPress;
    if(currentPress == LOW)
    {
      Serial.println(" button press detected!");
      lastMillis = millis();
      return true;
    }
  }
  return false;
}

Can somebody help me with a simple code for my problem? Maybe using blink without delay?
Please help it's urgent.
Thanks BulldogLowell. I'll try your suggestion!

septillion:
You're right, it does exactly what is says on the box :slight_smile: Have a look at Blink without delay :slight_smile:

Can you help me with a code to my problem by using blink without delay? I want the code.
Please help. It's urgent!
Thanks :smiley:

Saurabh98:
Can you help me with a code to my problem by using blink without delay? I want the code.
Please help. It's urgent!
Thanks :smiley:

you urgently have to blink a LED? many lives at stake, I suppose.

:grinning:

BulldogLowell:
you urgently have to blink a LED? many lives at stake, I suppose.

:grinning:

I appreciate your sarcasm... but yes i really do need a code for my problem. I need to show it in my college to my professor soon!
:wink:

Then I would start reading and understanding Blink without delay fast!

Saurabh98:
I appreciate your sarcasm...

thank you so much!

my wife, on the other hand, does not. :frowning:

BulldogLowell:
thank you so much!

my wife, on the other hand, does not. :frowning:

No doubt, wives are of the same attitude across the globe! XD
Which country ?

Saurabh98:
No doubt, wives are of the same attitude across the globe! XD
Which country ?

she's from Bolivia

BulldogLowell:
this compiles, not tested with LEDs

...

void sequenceLeds(void)
{
...
 Serial.println(leds, BIN);
...
 MsTimer2::set(1000, sequenceLeds);
...
}

Serial plus interrupt service routine. I wonder what could possibly go wrong.

Saurabh98:
So what can i do?

Finite state machine is the other topic of interest. Your description is a great starting point. For example, this is a great description of a transition...

So when any obstacle comes in front of the sensor...