help to create remote controled count down

hi

i want to create a remoted controled count down using ir remote

my problem is i want to add a reset option

the count down have 5 ; 10; 15 min

if i make delay(10601000)

i cant get reset commande from the ir remote ; i get it only if the delay finish

my arduino is arduino nano

thank you

Delay() blocks the code from doing anything until it is done, so you need a non-blocking method. Look into the example sketch, Blink Without Delay.

please can you add more detail ?

Sure.

The delay function should not be used when you are waiting for something else to happen. IE,

i cant get reset commande from the ir remote ; i get it only if the delay finish

Because it results in this happening.

So delay(10601000) is going to block you from doing anything for 10 minutes. This is 10 minutes of wasted time when the Arduino could be doing something else while its waiting for the 10 minutes to elapse.

You want to use a non blocking method like the one used in the Arduino example "Blink Without Delay". In this example, it shows you how to do something without waiting for something else to be finished. This is the Arduinos way of multitasking on a single thread processor.

thank you for your replay

i m not expert in arduino :frowning:

can you make code for me

and conbine the ir code and you code of the timer

The code is all in the following link:

but is in your Arduino's IDE too.

Where is the code you made or have?

this is the code

sketch_jun25a.ino (1.86 KB)

What does the count down timer control? Is it on for the set time duration or does it start once the timer is done?

it control 7 segment display

A single 7 segment display? Where is that in your code? Or are you setting the display with pin 2 - 8?

I cleaned up your code a little bit.

#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

byte numbers[] = { 
  B0010000, B1011011, B0001100, B0001001, B1000011 }; // insert the rest of the display values here

long time = millis();
byte idx = 0;

void setup()
{
  Serial.begin(9600);
  for(byte i = 2; i < 9; i++)
    pinMode(i, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value );
    if(results.value==2706322374)
    {
      decompteur(4);
      irrecv.resume(); // Receive the next value
    }
    else if(results.value==1231570528)
    {
      decompteur(2);
      irrecv.resume(); // Receive the next value
    }
    irrecv.resume(); // Receive the next value
  }
}

void decompteur(byte n)
{
  if( millis() - time >= 60*1000)
  {
    if(idx < n)
    {
      for(byte num = 0; num < 7; num++)
        digitalWrite(num+2, bitRead(numbers[n - idx], num) );
      idx++; 
    }
    else
      idx = 0;

    time = millis();
  }
}

void resetCounter() // call this function to reset the counter back to zero if you need to.
{
  idx = 0;
}

i want to change the code to add reset function ( make the counter 0)

I added it while you are typing.

you dont call the function resetCounter using IR

You add it where you need to, whatever button you want to use to reset it.
I don't know the IR button codes.

i will add ?

if(results.value==11111111)
    {
resetCounter();
}

i will use 2 of 7 segment display

what code i will change

and i will use 4511 to control 7 segment because number of pin is not enough

if i want to create a big 7 segment using 5 led/segment

arduino nano cant make working 572=70 led ?

You will need to add the rest of the numbers 0 - 9 and do some math to see what values you need to show based on the counter.

There should be pre-built 7 segment displays that have a library you can use to get them to work. This might be easier for you then wiring everything up and hoping for the best. But that's up to you.

As for you displays, (depending what you do) you need to look at the counter and see if it is greater or less than 10. This will tell your code what to display on each 7 segment disp. The first display will show 0 - 9 and the second will only show 0 and 1.

if i want to create a big 7 segment using 5 led/segment

Get a prebuilt display for this.

i dont understand you

can you create the code ?

This is easy enough to where you can write the code, but I can make a sample you can learn from.

Here is a simple count down timer.

long time = millis();
int M,Min, S, Sec;
boolean Reset = false, Stop = false, Paused = false;
volatile boolean timeFlag = false;

void setup()
{
  Serial.begin(115200);
  StartTimer(0,10);   
  // if minutes is set to NULL, it will be give a default value of 0, otherwise minutes will be set to input value
  // seconds is basically the same but its default value is 59
}

void loop() 
{
  if( !CountDownTimer() ) // check if the timer has reached 0
  {
    //ResetTimer(); // if it has, reset the timer. *OPTIONAL*
  }  

  if(TimeHasChanged() ) // this prevents the time from being constantly shown.
  {
    Serial.print(ShowMinutes());
    Serial.print(":");
    Serial.println(ShowSeconds()); // This DOES NOT format the time to 0:0x when seconds is less than 10. 
    // if you need to format the time to standard format, use the sprintf() function. 
  }
  
  StopTimerAt(0,2); // With this, you can stop the timer at any minute or second
}

boolean CountDownTimer()
{
  timeFlag = false;
  if(!Stop && !Paused) // if not Stopped or Paused, run the timer
  {
    if(millis() - time > 1000) // check the time difference and see if 1 second has elapsed
    {
      time = millis(); // 1 second HAS elapsed, so reset the time variable
      S > 0? S-- : (M-- , S = 59); // Check to see if S can be decremented and is still above 0.
      // If S goes below 0, reset it back to 59 and decrement M by 
      timeFlag = true;

      if(M == 0 && S == 0) // check to see if both M and S are 0
          Stop = true; // If so, stop the timer
    }
  }
  return !Stop; // return the state of the timer
}

void ResetTimer()
{
  StartTimer(Min, Sec);
  Stop = false;
}

void StopTimer()
{
  Stop = true;
}

void StopTimerAt(int minutes, int seconds)
{
  if(minutes == ShowMinutes() && seconds == ShowSeconds())
    Stop = true;
}

void PauseTimer()
{
  Paused = true;
}

void ResumeTimer() // You can resume the timer if you ever stop it.
{
  Paused = false;
}

void StartTimer(int minutes, int seconds)
{
  Min = minutes; // set to global vars
  Sec = seconds; // ------------------
  
  (minutes == NULL) ? M = 0 : (M = minutes - 1);
  (seconds == NULL) ? S = 60: (S = seconds);
  Stop = false;
}

int ShowMinutes()
{
  return M;
}

int ShowSeconds()
{
  return S;
}

boolean TimeHasChanged()
{
  return timeFlag;
}

boolean TimeCheck(int minutes, int seconds) // output is true if timer equals requested time
{
  return (minutes == ShowMinutes() && seconds == ShowSeconds()); 
}

Ok, so I gave you a working count down timer with some good options, now all you need to do is get a number from the IR controller and use the values from ShowMinutes() and ShowSeconds() to put on your 7 segment display(s).

What progress have you made with your code so far?