Need arduino leonardo code for Two Btns different keystroke

i am trying to get pin 7 to trigger Keyboards "ALT+F2 and Pin 5 to Trigger "ALT+F3, pin 7 has a red momentary button and pin5 has a green momentary Button all buttons are opened until pressed. I also need this to only do it once then stop. i am very new to this type of coding. if anyone could help it would be greatly appreciated. i have tried numerous code and as of right now pin 5 will trigger ALT+F2 and then if i do pin 5 again it will trigger ALT+F3 and pin 7 is doing nothing
here is my current code

const int buttonPin7 = 7;    // the number of the pushbutton pin
const int buttonPin5 = 5;    // the number of the pushbutton pin
// Variables will change:
int buttonState7;             // the current reading from input 7 pin
int lastButtonState7 = LOW;   // the previous reading from input 7 pin
int buttonState5;             // the current reading from input 5 pin
int lastButtonState5 = LOW;   // the previous reading from input 5 pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime5 = 0;  // the last time the output pin was toggled
long debounceDelay5 = 50;    // the debounce time; increase if the output flickers
long lastDebounceTime7 = 0;  // the last time the output pin was toggled
long debounceDelay7 = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin7, INPUT_PULLUP);  //sets pin 7 to use pullup resistor to 5v supply
  pinMode(buttonPin5, INPUT_PULLUP);  //sets pin 5 to use pullup resistor to 5v supply

}

void loop() {
  // read the state of the switch into a local variable:
  int reading7 = digitalRead(buttonPin7);
  int reading5 = digitalRead(buttonPin5);


  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
    if (reading5 != lastButtonState5) {
    // reset the debouncing timer
    lastDebounceTime5 = millis();
  }

    if (reading7 != lastButtonState7) {
    // reset the debouncing timer
    lastDebounceTime7 = millis();
  }

  if ((millis() - lastDebounceTime5) > debounceDelay5) {
  if ((millis() - lastDebounceTime7) > debounceDelay7) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading5 != buttonState5) {
      buttonState5 = buttonState5; 

        if (buttonState5 == LOW) {
     }
    }  
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F3);
    Keyboard.releaseAll();
    delay(600);
   buttonState5 = reading5;
      // save the reading.  Next time through the loop,
      // it'll be the lastButtonState:
      lastButtonState5 = reading5;
  }

    if (reading7 != buttonState7) {
      buttonState7 = reading7;

        if (buttonState7 == LOW) {
     }
    } 
   
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F2);
    Keyboard.releaseAll();
    delay(600);
   buttonState7 = reading7;
      // save the reading.  Next time through the loop,
      // it'll be the lastButtonState:
      lastButtonState7 = reading7;
}


  else {

  }

}

Welcome to the forum

If you Auto Format the code in the IDE then the code blocks become more obvious. Here is a portion of your code with some comments added


            // if the button state has changed:
            if (reading5 != buttonState5)
            {
                buttonState5 = buttonState5;        //why copy the variable to itself ?

                if (buttonState5 == LOW)
                {       //the state is LOW so the button is pressed but no code is executed ?
                }
            }
            Keyboard.press(KEY_LEFT_ALT);       //this code does not depend on the button being pressed
            Keyboard.press(KEY_F3);
            Keyboard.releaseAll();
            delay(600);
            buttonState5 = reading5;        //why do this ?
            // save the reading.  Next time through the loop,
            // it'll be the lastButtonState:
            lastButtonState5 = reading5;
        }

1 Like

Honestly i am not a code writer so while i can do some thing this is greek here to me i know many will say just read and learn the code, however i was just trying my luck to see if someone more experienced here could show me what needs to be changed so i could do that as this is for a slot car program.
how do i make that code work or do i need to have better new code?

@UKHeliBob already pointed out what goes wrong; your sending of <ALT><F3> does not depend the status of the button.

If you move

            Keyboard.press(KEY_LEFT_ALT);       //this code does not depend on the button being pressed
            Keyboard.press(KEY_F3);
            Keyboard.releaseAll();

(and maybe more) inside

                if (buttonState5 == LOW)
                {       //the state is LOW so the button is pressed but no code is executed ?
                }
            }

You will get closer to your goal.

The below also does not make much sense (no offense intended)

  if ((millis() - lastDebounceTime5) > debounceDelay5)
  {
    if ((millis() - lastDebounceTime7) > debounceDelay7)
    {

Why would you make the button7 debounce debending on the button5 debounce. You should have something like

  if ((millis() - lastDebounceTime5) > debounceDelay5)
  {
    do your button5 stuff here
  }

  if ((millis() - lastDebounceTime7) > debounceDelay7)
  {
    do your button7 stuff here
  }
1 Like

Thank You For your help, i am sure it does not make much sense because honestly i do not know what i am doing.
I was trying to do this quickly to get a trigger code set up for a computer that manages a slot car race track. I was hoping that people would chime in and show me what was wrong as that would be much faster right now than taking the time to read and do all the tutorials to learn everything.
Eventually i am going to sit down and actually learn how to do Arduino coding as i think this can be a valuable tool for other projects in the future.
Thanks, everyone for your help it is greatly welcomed and appreciated!

That never works. Don't take this wrong but if you need it quickly you will probably need to pay.

We did :wink: We just did not fix it. I suggest that you start with one button and get that working. If you're stuck with that, we can help you further.

Thanks, i have one button working, then i utilized a hardwire for the second wiring directly into the unit so now i have everything working, Actually easier than i thought. When i am done with this entire project i am going to take the time to learn the structure and coding for Arduino, also i just found out that a friend of mine codes Arduino boards for various projects at Roush, he said he can help me with various ideas and projects, so that is a nice plus.
All the info that has been provided in response to my post helped me greatly actually more than you know. It actually helped me to get an understanding of the basic structure and layout of the code.
Thank You again!

Great :+1:

Our goals achieved :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.