Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Support Us

Top Posters

Small Flight Sim Control Panel

This is using an arduino leonardo 4x4 matrix keypad 7 10k resistors and 4 switches
it is possible to add more matrix keypads by setting it up to use one pin but that isnt shown here.

picture of the project cant be seen here: https://www.linuxdistrocommunity.com/u/dragonkeeper/m/flightcontroller/

And here is the code:
, if you want to use the code, dont forget to change the keyboard output ,


#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
// Keymap
char keys[ROWS][COLS] = {
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'#','0','*','D' }
};
byte rowPins[ROWS] = {
11, 10, A0, A1 };
byte colPins[COLS] = {
A2, A3, A4, A5 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int buttonPin = 2; // the number of the switch pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;
const int buttonPin7 = 8;
const int ledpin = 13; // the number of the LED pin
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;

int lastbuttonState = 0;
int lastbuttonState2 = 0;
int lastbuttonState3 = 0;
int lastbuttonState4 = 0;
int lastbuttonState5 = 0;
int lastbuttonState6 = 0;
int lastbuttonState7 = 0;

void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, HIGH);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buttonPin7, INPUT);
Serial.begin(9600);
Keyboard.begin();
lastbuttonState = 1;
lastbuttonState2 = 1;
lastbuttonState3 = 1;
lastbuttonState4 = 1;
lastbuttonState5 = 1;
lastbuttonState6 = 1;
lastbuttonState7 = 1;
}

void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (lastbuttonState != buttonState){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('1');
Keyboard.releaseAll();
lastbuttonState = buttonState;
}
else {
}
}
else {
lastbuttonState = 1;
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == LOW) {
if (lastbuttonState2 != buttonState2){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('2');
Keyboard.releaseAll();
lastbuttonState2 = buttonState2;
}
else {
}
}
else {
lastbuttonState2 = 1;
}
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == LOW) {
if (lastbuttonState3 != buttonState3){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('3');
Keyboard.releaseAll();
lastbuttonState3 = buttonState3;
}
else {
}
}
else {
lastbuttonState3 = 1;
}
buttonState4 = digitalRead(buttonPin4);
if (buttonState4 == LOW) {
if (lastbuttonState4 != buttonState4){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('4');
Keyboard.releaseAll();
lastbuttonState4 = buttonState4;
}
else {
}
}
else {
lastbuttonState4 = 1;
}
buttonState5 = digitalRead(buttonPin5);
if (buttonState5 == LOW) {
if (lastbuttonState5 != buttonState5){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('5');
Keyboard.releaseAll();
lastbuttonState5 = buttonState5;
}
else {
}
}
else {
lastbuttonState5 = 1;
}
buttonState6 = digitalRead(buttonPin6);
if (buttonState6 == LOW) {
if (lastbuttonState6 != buttonState6){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('6');
Keyboard.releaseAll();
lastbuttonState6 = buttonState6;
}
else {
}
}
else {
lastbuttonState6 = 1;
}
buttonState7 = digitalRead(buttonPin7);
if (buttonState7 == LOW) {
if (lastbuttonState7 != buttonState7){
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('7');
Keyboard.releaseAll();
lastbuttonState7 = buttonState7;
}
else {
}
}
else {
lastbuttonState7 = 1;
}

/* Setting all switch keys ^^ */
/*==================================*/
/* Setting all keypad keys VV */
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '1':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('K');
Keyboard.releaseAll();
break;
case '2':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('L');
Keyboard.releaseAll();
break;
case '3':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('P');
Keyboard.releaseAll();
break;
case '4':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('O');
Keyboard.releaseAll();
break;
case '5':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('I');
Keyboard.releaseAll();
break;
case '6':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('U');
Keyboard.releaseAll();
break;
case '7':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('Y');
Keyboard.releaseAll();
break;
case '8':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('T');
Keyboard.releaseAll();
break;
case '9':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('R');
Keyboard.releaseAll();
break;
case '*':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('Q');
Keyboard.releaseAll();
break;
case '0':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('E');
Keyboard.releaseAll();
break;
case '#':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('W');
Keyboard.releaseAll();
break;
case 'A':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('A');
Keyboard.releaseAll();
break;
case 'B':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('B');
Keyboard.releaseAll();
break;
case 'C':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('C');
Keyboard.releaseAll();
break;
case 'D':
digitalWrite(ledpin, HIGH);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('D');
Keyboard.releaseAll();
break;
}
}
digitalWrite(ledpin, LOW);
delay(300);
}

Comments

  • Updated Code

    #include <Keypad.h>
    const byte ROWS = 4;
    const byte COLS = 4;
    // Keymap
    char keys[ROWS][COLS] = {
    {
    '1','2','3','A' }
    ,
    {
    '4','5','6','B' }
    ,
    {
    '7','8','9','C' }
    ,
    {
    '#','0','*','D' }
    };
    byte rowPins[ROWS] = {
    11, 10, A0, A1 };
    byte colPins[COLS] = {
    A2, A3, A4, A5 };

    // Create the Keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    const int buttonPin = 2; // the number of the switch pin
    const int buttonPin2 = 3;
    const int buttonPin3 = 4;
    const int buttonPin4 = 5;
    const int buttonPin5 = 6;
    const int buttonPin6 = 7;
    const int buttonPin7 = 8;
    const int ledpin = 13;
    const int ledpin2 = 9;// the number of the LED pin
    int buttonState = 0;
    int buttonState2 = 0;
    int buttonState3 = 0;
    int buttonState4 = 0;
    int buttonState5 = 0;
    int buttonState6 = 0;
    int buttonState7 = 0;

    int lastbuttonState = 0;
    int lastbuttonState2 = 0;
    int lastbuttonState3 = 0;
    int lastbuttonState4 = 0;
    int lastbuttonState5 = 0;
    int lastbuttonState6 = 0;
    int lastbuttonState7 = 0;


    void setup()
    {
    pinMode(ledpin,OUTPUT);
    digitalWrite(ledpin, HIGH);
    pinMode(ledpin2,OUTPUT);
    digitalWrite(ledpin2, LOW);
    pinMode(buttonPin, INPUT);
    pinMode(buttonPin2, INPUT);
    pinMode(buttonPin3, INPUT);
    pinMode(buttonPin4, INPUT);
    pinMode(buttonPin5, INPUT);
    pinMode(buttonPin6, INPUT);
    pinMode(buttonPin7, INPUT);
    Serial.begin(9600);
    lastbuttonState = 1;
    lastbuttonState2 = 1;
    lastbuttonState3 = 1;
    lastbuttonState4 = 1;
    lastbuttonState5 = 1;
    lastbuttonState6 = 1;
    lastbuttonState7 = 1;
    }

    void loop()
    { Keyboard.begin();
    buttonState = digitalRead(buttonPin);
    if (buttonState == LOW) {
    digitalWrite(ledpin2, HIGH);
    Keyboard.press(KEY_LEFT_SHIFT);
    }
    else {
    delay(100);
    digitalWrite(ledpin2, LOW);
    lastbuttonState = 2;
    Keyboard.releaseAll();
    }
    buttonState2 = digitalRead(buttonPin2);
    if (buttonState2 == LOW) {
    if (lastbuttonState2 != buttonState2){
    delay(500);
    digitalWrite(ledpin2, LOW);
    Keyboard.releaseAll();
    Keyboard.press('2');
    Keyboard.releaseAll();
    lastbuttonState2 = buttonState2;
    }
    else {
    Keyboard.releaseAll();
    }
    }
    else {
    lastbuttonState2 = 1;
    }
    buttonState3 = digitalRead(buttonPin3);
    if (buttonState3 == LOW) {
    if (lastbuttonState3 != buttonState3){
    Keyboard.releaseAll();
    Keyboard.press('3');
    Keyboard.releaseAll();
    lastbuttonState3 = buttonState3;
    }
    else {
    }
    }
    else {
    lastbuttonState3 = 1;
    }
    buttonState4 = digitalRead(buttonPin4);
    if (buttonState4 == LOW) {
    if (lastbuttonState4 != buttonState4){
    Keyboard.press('4');
    Keyboard.releaseAll();
    lastbuttonState4 = buttonState4;
    }
    else {
    }
    }
    else {
    lastbuttonState4 = 1;
    }
    buttonState5 = digitalRead(buttonPin5);
    if (buttonState5 == LOW) {
    if (lastbuttonState5 != buttonState5){
    Keyboard.press('5');
    Keyboard.releaseAll();
    lastbuttonState5 = buttonState5;
    }
    else {
    }
    }
    else {
    lastbuttonState5 = 1;
    }
    //removed for dublicating button press//
    /* buttonState6 = digitalRead(buttonPin6);
    if (buttonState6 == LOW) {
    if (lastbuttonState6 != buttonState6){
    Keyboard.press('6');
    Keyboard.releaseAll();
    lastbuttonState6 = buttonState6;
    }
    else {
    }
    }
    else {
    lastbuttonState6 = 1;
    } */
    buttonState7 = digitalRead(buttonPin7);
    if (buttonState7 == LOW) {
    if (lastbuttonState7 != buttonState7){
    Keyboard.press('7');
    Keyboard.releaseAll();
    lastbuttonState7 = buttonState7;
    }
    else {
    }
    }
    else {
    lastbuttonState7 = 1;
    }

    /* Setting all switch keys ^^ */
    /*==================================*/
    /* Setting all keypad keys VV */
    char key = kpd.getKey();
    if(key) // Check for a valid key.
    {
    switch (key)
    {
    case '1':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('[');
    Keyboard.releaseAll();
    break;
    case '2':
    digitalWrite(ledpin, HIGH);
    Keyboard.press(']');
    Keyboard.releaseAll();
    break;
    case '3':
    digitalWrite(ledpin, HIGH);
    Keyboard.press(';');
    Keyboard.releaseAll();
    break;
    case '4':
    digitalWrite(ledpin, HIGH);
    Keyboard.press(',');
    Keyboard.releaseAll();
    break;
    case '5':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('.');
    Keyboard.releaseAll();
    break;
    case '6':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('/');
    Keyboard.releaseAll();
    break;
    case '7':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('g');
    Keyboard.releaseAll();
    break;
    case '8':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('H');
    Keyboard.releaseAll();
    break;
    case '9':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('I');
    Keyboard.releaseAll();
    break;
    case '*':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('J');
    Keyboard.releaseAll();
    break;
    case '0':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('K');
    Keyboard.releaseAll();
    break;
    case '#':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('L');
    Keyboard.releaseAll();
    break;
    case 'A':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('M');
    Keyboard.releaseAll();
    break;
    case 'B':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('N');
    Keyboard.releaseAll();
    break;
    case 'C':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('O');
    Keyboard.releaseAll();
    break;
    case 'D':
    digitalWrite(ledpin, HIGH);
    Keyboard.press('P');
    Keyboard.releaseAll();
    break;
    }
    }
    digitalWrite(ledpin, LOW);
    Keyboard.end();
    delay(10);
    }
  • morning_starmorning_star DonatorRegistered, Donator
    Donator
    that's awesome DK

    Yesterday I went on a walk and saw a little chipmunk sitting on a log.   I went over and asked the chipmunk what he was doing there.  He looked up at me with his little eyes and now i only have one leg.

    Sourceforge
    Github

  • setkehsetkeh Administrator
    Nice dude keep the awesome projects a flowing :)



    Wisdom:

    One does not Simply Deploy into Production.

  • plustwoplustwo Registered
    :cool: keep up the good work on projects like these mate ...
Sign In or Register to comment.