Code for sensing second button active


I'm new to the Arduino and would like to know what the best way to code the following situation.

Project will have 2 push buttons and when PB1 is pressed I'd like the program to loop or a case statement for 5 seconds to check if
PB2 is pressed during this time period.

Any input would be greatly appreciated.

Thank You

Look at this

Then put another one when you detect a press limited by time for five seconds.
Use the mills() function to see when that happens.
Look at the blink without delay example already loaded on your system under the examples menu.
Or do you want some one to do it for you?

Hi Mike,

Thanks for getting back to me.
I'm just having a hard time grasping how to write the code that waits to detect if the second button is activated and
how to include the code in the loop.



So you do want me to do it for you.
Just this one time then:-

// code to wait for two push button events within 5 seconds

#define button1 3  // pins used for buttons
#define button2 4

boolean oldButton1, button2State;
long int time;

void setup(){
  pinMode(button1, OUTPUT); 
  pinMode(button2, OUTPUT); 
  oldButton1 = digitalRead(button1);   // get initial state of button

void loop(){
  boolean button1State = digitalRead(button1);
  if( button1State == LOW && oldButton1 == HIGH) { // we have detected a push on button 1
   time = millis() + 5000; // get the time when we should stop looking for the second button
   button2State = digitalRead(button2);   // get the value of btton 2
   while(time > millis() && button2State == HIGH) { // spend the next 5 seconds looking for button 2
        button2State = digitalRead(button2); 
    // now we have exited the while loop because button 2 has been pressed or 5 seconds have elapsed
    // lets find out which
    if(button2State == LOW) {  // it was a button press
    // do what we want to do if the second event follows the first within 5 seconds
  oldButton1 = button1State; // remember the state of button 1 for comparison next time round the loop
  delay(10); // just a bit of debounce time

It compiles but I have not tested it.

 pinMode(button1, OUTPUT); 
  pinMode(button2, OUTPUT);

It compiles... 8)

Sorry brain fart

 pinMode(button1, INPUT); 
  pinMode(button2, INPUT);

I am here sipping some rather fine ale:- Vital Spark from Fine Ales Argyll

Marston's Pedigree - nothing finer. Cheers!

Agreed a grand brew.


Your the best, I really appreciate it.