Auto switch condition using timer

Hello, i need help for my code. In my program code, it will bring up "green" or "red" when one button is pressed. I want to know, How can the program change automatically when it exceeds the specified time. Example: when I press button1, the word "green" appears and repeats. then it will change conditions where "red" will appear after 10 minutes. The time will start when the button is pressed. Can anyone help me provide this solution. Thank you for your help.

This is my program code :

int Pressed1 = 0;
int Pressed2 = 0;

/* BUTTON */
int buttonPin1 = 2;
int buttonPin2 = 3;

void setup() {
  // whatever goes here
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

void loop() {
  tombol1();
  tombol2();
}
void tombol1() {
  if(Pressed1 == 0) { // button has never been pressed
    // detect button press
    if (digitalRead(buttonPin1) == HIGH) {
      Pressed1 = 1;
      Pressed2 = 0;
    }
  } else { // button has been pressed, do this instead
    // your loop
    Serial.println("Green");
    delay(1000);
   
  }
}
void tombol2() {
   if(Pressed2 == 0) { // button has never been pressed
    // detect button press
    if (digitalRead(buttonPin2) == HIGH) {
      Pressed2 = 1;
      Pressed1 = 0;
    }
  } else { // button has been pressed, do this instead
    // your loop
    Serial.println("Red");
    delay(1000);
  }
}

This the output what i want :

<Button1 Pressed>
Green
Green
Green
<after 10 minutes>
Red
Red
Red

Thank you.

since you are using delay can you impements a counter. the code below will should print out "RED" only once if there is a timeout following button1 pressed.

a better methold probably would be to use millis() however.

int TIMEOUT_CNT = 15000 // 10min/(20ms*2) 
int DELAY_T = 20; //20ms

int Pressed1 = 0;
int Pressed2 = 0;
int cnt=0;

/* BUTTON */
int buttonPin1 = 2;
int buttonPin2 = 3;

void setup() {
  // whatever goes here
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

void loop() {
  tombol1();
  tombol2();
  timeout_cnt();
}
void tombol1() {
  if(Pressed1 == 0) { // button has never been pressed
    // detect button press
    if (digitalRead(buttonPin1) == HIGH) {
      Pressed1 = 1;
      Pressed2 = 0;
	  cnt=0; //reset timeout counter
    }
  } else { // button has been pressed, do this instead
    // your loop
    Serial.println("Green");
    delay(DELAY_T);
   
  }
}
void tombol2() {
   if(Pressed2 == 0) { // button has never been pressed
    // detect button press
    if (digitalRead(buttonPin2) == HIGH) {
      Pressed2 = 1;
      Pressed1 = 0;
    }
  } else { // button has been pressed, do this instead
    // your loop
    Serial.println("Red");
    delay(DELAY_T);
  }
}
void timeout_cnt() {
  if(Pressed1 == 1) { // button1 has been pressed
	if(cnt>=TIMEOUT_CNT){
	  Serial.println("Red");
	  Pressed1 = 0;
	}
	else{
	  ++cnt;
	}
  } 
}

hope that helps

If you want repeated printout of the current state whilst timing then you cannot use delay() because it blocks program execution. Instead use millis() for the timing.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

sherzaad:
since you are using delay can you impements a counter. the code below will should print out "RED" only once if there is a timeout following button1 pressed.

a better methold probably would be to use millis() however.

hope that helps

how about using millis() and printout repeatedly ? Can you give example to my code ? Thank you

UKHeliBob:
If you want repeated printout of the current state whilst timing then you cannot use delay() because it blocks program execution. Instead use millis() for the timing.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

i was trying using millis, but i don't know how to implemented millis to my code. can you give solution for my code using millis ? I'm sorry I was beginning to learn about arduino. Thank you

Have you looked at the links and understood the code ?

UKHeliBob:
If you want repeated printout of the current state whilst timing then you cannot use delay() because it blocks program execution.

The way I see it, there is nothing wrong with using a very short delay (say, 10 milliseconds) in order to allow Serial.println() to finish its job.

But, in the case of projects such as this one, it is probably better to simply wire up the LEDs and do away with Serial.println() entirely. It is entirely reasonable to want debug output, but I see no reason not to allow the LEDs themselves provide that output.

UKHeliBob:
Have you looked at the links and understood the code ?

I'm trying out that link, and trying to implement millis to mycode

UKHeliBob:
See Using millis() for timing. A beginners guide

Why the wall of text?

odometer:
Why the wall of text?

The "wall of text" as you call it is the explanation of why you would want/need to use millis() for timing and the descriptions of how the examples work. When you read it, remember that you already know how to use millis() for timing so the text, nor the thread as a whole, is really not relevant to you.

Different people learn in different ways. For some people looking at BlinkWithoutDelay is enough to understand the principle and to see how it can be applied but for others more hand holding is needed. Horses for courses as usual.

now my code runs well as desired. thanks for @sherzaad, @UKHeliBob, and other friends.

Can millis be used for 24 hour intervals ?

rpm070:
Can millis be used for 24 hour intervals ?

Yes, it can be used for periods up to 49 and a bit days but it will not be very accurate or repeatable. A Real Time Clock (RTC) would be better if accuracy is required.

UKHeliBob:
The "wall of text" as you call it is the explanation of why you would want/need to use millis() for timing and the descriptions of how the examples work. When you read it, remember that you already know how to use millis() for timing so the text, nor the thread as a whole, is really not relevant to you.

Different people learn in different ways. For some people looking at BlinkWithoutDelay is enough to understand the principle and to see how it can be applied but for others more hand holding is needed. Horses for courses as usual.

I remember those old Forrest M. Mims III books from when I was in my early teens. This was way, way before Arduino, and also way before the Internet as we know it. Those books had plenty of diagrams and showed you how to do things. You actually understood (or, at least, felt you understood) what was going on.

I have a feeling that one reason people have so much trouble using millis() is that they can't see millis().

I have a feeling that one reason people have so much trouble using millis() is that they can't see millis().

I think that you are right, hence the many threads where analogies of using millis() such as cooking and making coffee at the same time are used to explain their use.

But I think that the real problem is that having used delay() you can't simply change the code to use millis(), rather you have to start again and use a completely different methodology of measuring time passing. If you start writing a program knowing that you will be using millis() for timing then you (hopefully) write it in a way where millis() can be used more easily.

Changing a program that uses delay() to use millis() is like the story of the local inhabitant of (fill in name of place) who, when asked how to get to (fill in name of another place) replied "Well, I wouldn't start from here"

rpm070:
Can millis be used for 24 hour intervals ?

Why do you ask?
What is the end goal of your project?

odometer:
Why do you ask?
What is the end goal of your project?

I just want to try switching at 24-hour intervals and if I can use millis for 24-hour intervals

odometer:
I remember those old Forrest M. Mims III books from when I was in my early teens. This was way, way before Arduino, and also way before the Internet as we know it. Those books had plenty of diagrams and showed you how to do things. You actually understood (or, at least, felt you understood) what was going on.

I have a feeling that one reason people have so much trouble using millis() is that they can't see millis().

I have all those Mims books 8).

rpm070:
I just want to try switching at 24-hour intervals and if I can use millis for 24-hour intervals

You can do that, yes. Keep in mind that 24 hours is 86400000 milliseconds, and that that number (86400000, eighty-six million four hundred thousand) is much too big to fit in an int or even an unsigned int, and so you will need a long or unsigned long. I would recommend that you use unsigned long.

Either that or you could use reasoning similar to Blink Without Delay to make a clock. Instead of blinking an LED, you would increment (add 1 to) a counter. Something like this:

int dayNumber = 1;
int hourCount = 0;
int minuteCount = 0;
int secondCount = 0;

unsigned long lastTickMillis = 0;

const unsigned long ONE_SECOND = 1000; 

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  unsigned long currentMillis = millis();

  if (currentMillis - lastTickMillis >= ONE_SECOND) { 
    Serial.print("Tick-tock! ");
    lastTickMillis += ONE_SECOND;
    Serial.print("What o\'clock? ");
    secondCount++;
    if (secondCount >= 60) {
      secondCount -= 60;
      minuteCount++;
    }
    if (minuteCount >= 60) {
      minuteCount -= 60;
      hourCount++;
    }
    if (hourCount >= 24) {
      hourCount -= 24;
      dayNumber++;
    }
    Serial.print("It\'s ");
    Serial.print(hourCount);
    Serial.print(" h ");
    Serial.print(minuteCount);
    Serial.print(" min ");
    Serial.print(secondCount);
    Serial.print(" s into Day #");
    Serial.print(dayNumber);
    Serial.println(".");
  } 
}

(WARNING: I haven't actually tested that code, but I think it will work.)