Button State save, 2 buttons?

So my issue is i have no idea how to get these 2 buttons (momentary, Normally open) to be memorized before the actions start.

So button1 (pin5) gets pressed then say 5 seconds later button2 (pin3) gets pressed. Then the keystrokes are ran.

if that was a bad explanation: Hey button one got pressed, but i need to wait for button 2 to get pressed. they both got pressed, now run the program.

Thanks everyone for your help!!!!

#include "Keyboard.h" 

 
void setup() {
  pinMode(9, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
  digitalWrite(9, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(3, HIGH);
}

void loop() {
  //if the button is pressed
 if(digitalRead(5 && 3)==LOW){
    Keyboard.write('B');
    delay(150);
    Keyboard.release('B');
    delay(9000);
    Keyboard.write('H');
    Keyboard.write('S');
    delay(200);
    Keyboard.releaseAll();
    delay(100);
 }
  if(digitalRead(9)==LOW){
   
    Keyboard.write('H');
    Keyboard.write('S');
    delay(200);
    Keyboard.releaseAll();
    delay(100);
 } 
}

The several things at a time post and this post on using millis() may help with getting rid of those deleys.

if(digitalRead(5 && 3)==LOW){

That evaluates to,

if(digitalRead(1) == LOW);

You must separate the 2 digital reads.
Should be

if(digitalRead(5) == LOW && digitalRead(3) == LOW);
  digitalWrite(9, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(3, HIGH);

You already set the pinModes tro INPUT_PULLUP. No reason to write those pins to HIGH.

 if(digitalRead(5 && 3)==LOW){

Do you know what 5 && 3 is? That is NOT how to read two pins. There are NO shortcuts. You have to read each of them separately. If the order matters, then you also need to keep track of when each pin BECOMES pressed (not IS pressed).

The Order does not matter. So either one could be pressed first.

I know that the && doesn't work. that's why i asked here :slight_smile:

How would I write it correctly?

Does the first button have to remain pressed until the second one becomes pressed or can the first one be released but be "forgotten" 5 seconds later ?

It does not have to remain pressed.
but i would like it to remember(either of the buttons) for at least 15 Seconds

Here is a program that does what you describe, albeit full of flaws, but it should give you some ideas

const byte buttonPin1 = A1;
const byte buttonPin2 = A2;
unsigned long waitStart;
unsigned long currentMillis;
unsigned long period = 15000;

const byte waitingForEitherButton = 0;
const byte waitingForButton2 = 1;
const byte waitingForButton1 = 2;
const byte bothButtonsPressed = 3;

byte currentState = waitingForEitherButton;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  Serial.println("Waiting for a button press");
}

void loop()
{
  currentMillis = millis();
  switch (currentState)
  {
    case waitingForEitherButton:
      if (digitalRead(buttonPin1) == LOW)
      {
        currentState =  waitingForButton2;
        waitStart = currentMillis;
        Serial.println("Waiting for button 2");
      }
      if (digitalRead(buttonPin2) == LOW)
      {
        currentState =  waitingForButton1;
        waitStart = currentMillis;
        Serial.println("Waiting for button 1");
      }
      break;
      //
    case waitingForButton2:
      if (currentMillis - waitStart >= period)
      {
        currentState =  waitingForEitherButton;
        Serial.println("Waiting for either button");
      }
      if (digitalRead(buttonPin2) == LOW)
      {
        currentState = bothButtonsPressed;
      }
      break;
      //
    case waitingForButton1:
      if (currentMillis - waitStart >= period)
      {
        currentState =  waitingForEitherButton;
        Serial.println("Waiting for either button");
      }
      if (digitalRead(buttonPin1) == LOW)
      {
        currentState = bothButtonsPressed;
      }
      break;
      //
    case bothButtonsPressed:
      Serial.println("Both buttons pressed in time");
      delay(2000);    //naughty, but easy
      Serial.println("Waiting for either button");
      currentState = waitingForEitherButton;
      break;
  }
}

The program uses switch/case to move between various states and only executes the code for the current state. It uses millis() for timing so that code execution is not blocked apart from when both buttons have been pressed. It should really detect when buttons become pressed rather than when they are pressed and I fudged this by using delay() to avoid the second button press being detected as the first button press next time round. The program also wastes space by having the same constant text printed in more than one place, which is not good practice.

Look at how the program works in general then improve upon it

DO NOT take it as an example of good or efficient programming.

So i have done some editing to that one now where do I Put my Keystrokes in at?
I am still very new to programming. Thanks!!

const byte 3 = A1;
const byte 5 = A2;
unsigned long waitStart;
unsigned long currentMillis;
unsigned long period = 15000;

const byte waitingForEitherButton = 0;
const byte waitingForButton2 = 1;
const byte waitingForButton1 = 2;
const byte bothButtonsPressed = 3;

byte currentState = waitingForEitherButton;

void setup()
{
  pinMode(9, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
  digitalWrite(9, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(3, HIGH);
  Serial.begin(115200);
  pinMode(3, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  
}

void loop()
{
  currentMillis = millis();
  switch (currentState)
  {
    case waitingForEitherButton:
      if (digitalRead(3) == LOW)
      {
        currentState =  waitingForButton2;
        waitStart = currentMillis;
        
      }
      if (digitalRead(5) == LOW)
      {
        currentState =  waitingForButton1;
        waitStart = currentMillis;
        
      }
      break;
      //
    case waitingForButton2:
      if (currentMillis - waitStart >= period)
      {
        currentState =  waitingForEitherButton;
        
      }
      if (digitalRead(5) == LOW)
      {
        currentState = bothButtonsPressed;
      }
      break;
      //
    case waitingForButton1:
      if (currentMillis - waitStart >= period)
      {
        currentState =  waitingForEitherButton; //waiting
        
      }
      if (digitalRead(3) == LOW)
      {
        currentState = bothButtonsPressed;
      }
      break;
      //
    case bothButtonsPressed:
     
      delay(2000);    //naughty, but easy
      
      currentState = waitingForEitherButton;
      break;
  }
}
const byte 3 = A1;
const byte 5 = A2;

Variable names can not start with or be numbers.

now where do I Put my Keystrokes in at?

Did you even read the case value names?

where do I Put my Keystrokes in at?

To try the program you need to put the pin numbers of your inputs into the variable initialisations instead of mine and wire the switches to take the inputs LOW when the buttons are pressed.

const byte buttonPin1 = A1;
const byte buttonPin2 = A2;

My inputs use pins A1 and A2

Which pins do yours use ?

I used Digital pins 3 and 5

What about using boolean?

What about using boolean?

What about it ?

like make booleon False for both
then true

StrikeforcePC:
like make booleon False for both
then true

OK. Then what ?

How are you going to know which button was pressed first and manage the timing ?