Button pressed whilst another double pressed - Is this possible?

Hi, Ive been trying to work on a project for the past few weeks and previously asked some questions Is it possible to have a simultaneous button press to turn on a LED - Programming Questions - Arduino Forum, before realising the solution that I was creating did not work as I thought it would and going back to the drawing board.

However despite restarting I've now become a little bit stuck again and would like to ask some for some advice and also particualarly whether its feasible to do something I would like to do or whether I need to find a new solution.

I am still really new to this, and have to apologies if my current code (below) is not as good as it should be:

const int push_white=2;     
const int push_green=3;     
const int push_red=4;       
const int push_blue=5;       
const int led_green=9;      
const int led_red=10;        
const int led_blue=11;       
const int led_white=12;

const long BLINK_INTERVAL = 1000; 

int state_green=false;         
int state_red=false;           
int state_blue=false;          
int state_white=false;

unsigned long previousMillis = 0; 

void setup() {

  pinMode(push_white, INPUT);
  pinMode(push_green, INPUT);
  pinMode(push_red, INPUT);
  pinMode(push_blue, INPUT);
  pinMode(led_green, OUTPUT);
  pinMode(led_red, OUTPUT);
  pinMode(led_blue, OUTPUT);
  pinMode(led_white, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  clean();
  
  state_green=digitalRead(push_green);
  state_red=digitalRead(push_red);
  state_blue=digitalRead(push_blue);
  state_white=digitalRead(push_white);
  
  if(state_white==true)
      {
          clean();
          digitalWrite(led_white, HIGH);
          Serial.print("White");
          Serial.print("\n");
        }  
  if(state_green==true)
      {
          clean();      
          digitalWrite(led_green, LOW);
    	  digitalWrite(led_blue, LOW);
          digitalWrite(led_red, LOW);    
          Serial.print("ALL OFF");
          Serial.print("\n");
        }
   
  if(state_red==true)
      {
          clean();
          digitalWrite(led_green, LOW);
          digitalWrite(led_red, HIGH);
          Serial.print("Red");
          Serial.print("\n");
        }
   
  if(state_blue==true)
      {
          clean();
          digitalWrite(led_red, HIGH);
          digitalWrite(led_green, HIGH);
          Serial.print("Orange On");
          Serial.print("\n");
        delay(750);
          digitalWrite(led_red, LOW);
          digitalWrite(led_green, LOW);
          Serial.print("Orange Off");
          Serial.print("\n");
        }
   
  if(state_white==true && state_green==true)
      {
          clean();
          digitalWrite(led_white, HIGH);
          digitalWrite(led_green, HIGH);
          Serial.print("Green");
          Serial.print("\n");
        }
   
}

void clean(){
    digitalWrite(led_white, LOW);
}

I've been racking my brain and failing miserably, so I thought I'd ask for some advice in case its not feasible to do what I had hope and I should try and find an alternative.

Where I have been struggling is in an attempt to add a double press to my code. Where this differs from other examples I had tried to work from is that they seem to focus on making a single button multi functional.

What I would like to actually implement involves a state of two buttons rather than one, this would be similar to the last state above (White and Green).

The new action will still involve the white button (be held down), but I would like to activate a blue led if the green button was be double pressed/tapped in quick succession, rather than just a single press (which should activate the green light). On its own the Green button currently stops all leds.

Having gone round in circles can I please ask whether if this is something that is actually possible to implement? If so, please can you advise of any suggestions, examples or topics I should explore. Alternatively I am happy to take suggestions of alternative solutions.

(Please note, theres some code within my code that I am still working and its not yet - i.e. blink, as I would also like to make the 'state_red" flash the LED rather than stay on), but have not yet got that working either, as my focus has primarily been on the above issue).

lots of words, not much defining the need. is paragraph 7 the question ?

press a button, twice, within 1 second and something happens.

not a big problem. the example of button allows you to change state each time a button is pressed.

press a button once and flag#1 changed state. the blue LED lights
a timer for flag#1 occurs.

if flag#1 is set and the button is pressed again, the green led lights.
if 1 second passes and the green button is not pressed, then flag#1 change back.

BTW, this does not need to be two lines

          Serial.print("White");
          Serial.print("\n");

Serial.println("White") ; // prints the word White and does a new line

Yes, it's possible, but you need to detect when buttons are pressed and released. Take a look at the state change example in the IDE to start with. You'll need to use millis to record the time of each event too.

Do you have pullup or pulldown resistors on input pins to keep the pins from "floating" and causing false HIGHs or LOWs when the button is not pressed? Best way is like S3 in the diagram, no external resistor needed, logic is reversed though (pin is LOW when button is pressed).

There is a Button2 library by Lennart Hennigs that can handle single presses, double presses, triple presses, and long presses. Look for it in "Tools->Library Manager..." under the name "Button2". Install it and look at the examples in "File->Examples->Button2"

Please see this thread For beginners: The simple way to program for multiple buttons [code example] - Introductory Tutorials - Arduino Forum to learn how to simplify the button code

jayjayuk80s:
The new action will still involve the white button (be held down), but I would like to activate a blue led if the green button was be double pressed/tapped in quick succession, rather than just a single press (which should activate the green light). On its own the Green button currently stops all leds.

You could certainly program an Arduino to work like that but I suspect it will be very confusing for the user and prone to lots of frustrating mistakes.

...R

Firstly, thank you for your replies. I do apologies for the length of my post/question, I just wanted to give as much information as possible. May be it was too much.

Attached is also an image of my original circuit.

I did looked at debouncing the buttons and also a state change originally, but I found that meant each button would in effect need to be pressed and released, where as at present my current code allowed for the white button to be pressed once and held, whilst another button (ie green) is pressed.

At the sametime, I guess they are needed for the double press requirement.

Thanks again, I will review the comments and suggestions and hopefully update you shortly and possibly ask for some additional questions if needed