How to Toggle LEDs with keypad pushbuttons?

Hi all,

I have a 9 button panel I made for flight simulator using 9 momentary push buttons wired using the keypad code using 3rows x 3 columns.

I want to know if it’s possible to edit the code and wire up individual LEDS as to when I push one of the push buttons an LED is toggled on and stays on until toggles off again.

I’m not that experienced with coding. All help appreciated thank you.

Using Arduino Pro Micro board.

[code]
#include <Keypad.h>
#include <Joystick.h>

//Potentiometer Setup
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK,
  9, 0,                  // Button Count, Hat Switch Count
  true, true, true,     // X, Y, and Z Axis
  true, false, false,     // Rx, but no Ry or Rz (Rx represents throttle)
  false, false,          // No rudder or throttle (represented as Rx)
  false, false, false);  // No accelerator, brake, or steering; 
int throttle = 0;
int fuel = 0;

int lastButtonState = 0;

const byte ROWS = 3; //four rows
const byte COLS = 3; //four columns

char buttons[ROWS][COLS] = {
  {0, 1, 2},
  {3, 4, 5},
  {6, 7 ,8},
};
byte rowPins[ROWS] = {4, 5, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 8, 9}; //connect to the column pinouts of the keypad

Keypad throttleButtons = Keypad(makeKeymap(buttons), rowPins, colPins, ROWS, COLS); 

void setup(){
  Joystick.begin();
}
  
void loop(){

  checkButtons();
  
  throttle = analogRead(A0);
  throttle = map(throttle,0,1023,0,255);
  Joystick.setRxAxis(throttle);
  
  fuel = analogRead(A1);
  fuel = map(fuel,0,1023,0,255);
  Joystick.setXAxis(fuel);
 
  delay(30);
}

void checkButtons(void) {
  if (throttleButtons.getKeys())
  {
    for (int i=0; i<LIST_MAX; i++)   
        {
           if (throttleButtons.key[i].stateChanged )   
            {
            switch (throttleButtons.key[i].kstate) {  
                    case PRESSED:
                    case HOLD:
                              Joystick.setButton(throttleButtons.key[i].kchar, 1);
                              break;
                    case RELEASED:
                    case IDLE:
                              Joystick.setButton(throttleButtons.key[i].kchar, 0);
                              break;
            }
            }
        }
  }
}

[/code]

“ I want to know if it's possible . . .”

Yes

Thank you.

Could you point me in the right direction as to how if it’s not too much to ask? Thank you

Do you have 6 pins to read the momentary pins using Keypad.h?
Or 9 pins reading each button discretely?

One method leaves 14 pins free on a Promini or Nano to drive LEDs with current limit resistors.
5 pins left for D0, D1, to the PC, and 3 more for other uses.

The other method leaves 11 pins free for LEDs.
Then leave D0, D1 for USB/P comms, and all your IO is taken.

When a valid key is recognized, light up the appropriated LED.

No idea how to change your code, as you haven't posted any.

CrossRoads:
Do you have 6 pins to read the momentary pins using Keypad.h?
Or 9 pins reading each button discretely?

One method leaves 14 pins free on a Promini or Nano to drive LEDs with current limit resistors.
5 pins left for D0, D1, to the PC, and 3 more for other uses.

The other method leaves 11 pins free for LEDs.
Then leave D0, D1 for USB/P comms, and all your IO is taken.

When a valid key is recognized, light up the appropriated LED.

Yes, I have 6 pins using Keypad.h to read the pushbuttons, (4,5,6,7,8,9) to be exact. Yeah i have a pro micro board and i've got the resistors and leds im just a bit lost as how to go about it.
Ill attach code in original post, Thank you for assistance, I appreciate it.

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can as you now see, easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Why do we think this is more important than just having your question answered? Because it is really difficult to write code properly - as with any other task requiring care - if everything is disorganised!

Paul__B:
OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can as you now see, easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Why do we think this is more important than just having your question answered? Because it is really difficult to write code properly - as with any other task requiring care - if everything is disorganised!

Hi, I'm new to all of this so thanks for the advice. I've since attached the code to my original post in the correct format.

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