Arduino Countdown.

Happy Arduino Day -2020.

MODERATOR EDIT : inappropriate comment removed

I Want to make 20 second countdown, time start by ir sensor.
Any one can write the code please.

No

We can write your own and we can help you get it working, or you can go to https://forum.arduino.cc/index.php?board=26.0 and offer to pay someone to write it for you.

@bijoy01

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here. :slight_smile:

Capture millis() into a start time variable when your sensor "trips", the details of which will depend on the specifics of the sensor, but might perhaps be that its output goes from high to low, then each time though loop() compare the new, ever-increasing millis() minus the start time, to 20 seconds.

If it's not 20 seconds yet, do nothing. If it is, you're done.

Is the IR sensor a trip / interruption sensor, or are you planning to use an IR remote control to start the timer?

What do you want the project to do once the 20 seconds expires?

i want make for hand washing .

bijoy01:
i want make for hand washing .

That doesn't answer the question of what you want it to do!

Sounds like it's an ir proximity sensor?- probably has a digital output high then goes low if blocked?

See what I said above.

ok, I get it. PERFECT for the situation we're all in right now!

Do you have your hardware yet? -- the IR sensor and display, or are you asking for recommendations for those?

Or... instead of a countdown display, maybe you could just have an LED flash until the 20 seconds is up. It would be a lot easier for you to wire up if you don't have the display and aren't sure how to connect it up.

Without display only ir sensor ,Start by ir sesor whatever countdown stop it auto reset and and star again by ir sensor.

Try this. I have no idea what your hardware is, YMWV.

// https://forum.arduino.cc/index.php?topic=672112
// ir sensor with countdown
// led on during countdown

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  button = 14;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
bool timing = false;
unsigned long timingStartedAt;
int timeFor = 5000;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(" ir sensor with countdown");
  pinMode(button, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(button);
  lastButtonState = buttonState;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... interrupt the sensor");
  Serial.println(" ");
}

void loop()
{
  checkSensor();

  if (timing && millis() - timingStartedAt >= timeFor)
  {
    timing = false;
    Serial.println("... done");
    digitalWrite(LED_BUILTIN, LOW);
  }

} //loop

void checkSensor()
{
  // read the button:
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (!timing && buttonState == LOW) //... and if it's now low, that's a press, but not already busy
    {
      Serial.print("Started");
      timingStartedAt = millis();
      timing = true;
      digitalWrite(LED_BUILTIN, HIGH);
    }// change to low
    else
    {
      //Serial.println(", new absence");
    }//change to high

    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}//checkSensor

my hardware is :Arduino,
IR Sensor module,
buzzer/led (for output)

(MOD EDIT)
ir-sensor-module-for-arduino-2f-rasberry-pi-500x500.jpg

ir-sensor-module-for-arduino-2f-rasberry-pi-500x500.jpg

You may want to put a small piece of tube over those or tape between them.
Some here trigger by accident when used without it.
A larger straw works quite well just cut down to come level with the end of the LED's

Also take the time to learn how to post pictures.
Help for that in one of the links I gave you !

If that just has a hi/low on its "OUT" then my code should work.

A PIR motion sensor would probably work better in this application.

bijoy01:
i want make for hand washing .

blomcrestlight:
Try this. I have no idea what your hardware is, YMWV.

// https://forum.arduino.cc/index.php?topic=672112

// ir sensor with countdown
// led on during countdown

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
              (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won’t change:
const int  button = 14;

// Variables will change:
bool buttonState;        // current state of the button
bool lastButtonState;    // previous state of the button
bool timing = false;
unsigned long timingStartedAt;
int timeFor = 5000;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(" ir sensor with countdown");
  pinMode(button, INPUT_PULLUP);

//initialize button states
  buttonState = digitalRead(button);
  lastButtonState = buttonState;

//turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

Serial.println(" “);
  Serial.println(“setup() done… interrupt the sensor”);
  Serial.println(” ");
}

void loop()
{
  checkSensor();

if (timing && millis() - timingStartedAt >= timeFor)
  {
    timing = false;
    Serial.println("… done");
    digitalWrite(LED_BUILTIN, LOW);
  }

} //loop

void checkSensor()
{
  // read the button:
  buttonState = digitalRead(button);

// compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (!timing && buttonState == LOW) //… and if it’s now low, that’s a press, but not already busy
    {
      Serial.print(“Started”);
      timingStartedAt = millis();
      timing = true;
      digitalWrite(LED_BUILTIN, HIGH);
    }// change to low
    else
    {
      //Serial.println(", new absence");
    }//change to high

// Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}//checkSensor

Thanks, Your code working.
Advance How to add this line in sketch.

// If one second as gone by sound buzzer
if (BuzzTrigger == 99){
digitalWrite (Buzzer, HIGH) ;// Buzzer On
delay (9) ;// Delay 2ms
digitalWrite (Buzzer, LOW) ;// Buzzer Off
BuzzTrigger = 0; // Trigger for countdown sound
}
else {
delay (9);
BuzzTrigger = BuzzTrigger + 1;

This "bips" led 13....

// https://forum.arduino.cc/index.php?topic=672112
// ir sensor with countdown
// led on during countdown

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  button = 14;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
bool timing = false;
unsigned long timingStartedAt;
int timeFor = 5000;
unsigned long lastBip;
int bipInterval = 100;
bool bipState;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(" ir sensor with countdown");
  pinMode(button, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(button);
  lastButtonState = buttonState;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... interrupt the sensor");
  Serial.println(" ");
}

void loop()
{
  checkSensor();

  if (timing && millis() - lastBip >= bipInterval)
  {
    lastBip = millis();
    digitalWrite(LED_BUILTIN, bipState);
    bipState=!bipState;
  }

  if (timing && millis() - timingStartedAt >= timeFor)
  {
    timing = false;
    Serial.println("... done");
    digitalWrite(LED_BUILTIN, LOW);
  }

} //loop

void checkSensor()
{
  // read the button:
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (!timing && buttonState == LOW) //... and if it's now low, that's a press, but not already busy
    {
      Serial.print("Started");
      timingStartedAt = millis();
      timing = true;
      digitalWrite(LED_BUILTIN, HIGH);
    }// change to low
    else
    {
      //Serial.println(", new absence");
    }//change to high

    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}//checkSensor