Newbie need your help

Hi @ALL :slight_smile: ,

I´m a Newbie and trying to use the following sketch to build my own ButtonBox for PS4. With this modified Sketch from Kevin Peat and the used “Matrix Cabling” it isnt possible to use two buttons at the same time, if they are in the same row. Using multiple rows limits the number of usable pins to 12, I need about 18 and 4 digitalout pins. so, its no alternative. the video explains the problem.

can someone help me and find out how to optimize the code to keep the nice toggle switch and

  • maybe after sending the keyboard.press “6” immediately sending keyboard.release although the toogle switch is in on position.
  • if this wouldnt work and i have to use momentary switches, how to turn the led on and keep it on until the switch is pressed again. this would be the sad alternative, I would like to keep the illuminated buttons.

Maybe someone can help me, that would be very nice.

Selfexpaining Video:
ButtonBox Sketch Problem

/*
* -------------------------------------------------------------------------
* ButtonBox auf Basis des Arduino Leonardo
* -------------------------------------------------------------------------
* Original code provided by:
* kevin@kevinpeat.com
* http://www.kevinpeat.com/arduino_button_box.html
* &
* Dan Nixon on his blog
* Multiplexing Switches on Arduino without additional ICs
* http://www.dan-nixon.com/2012/04/multiplexing-switches-on-arduino.html
* -------------------------------------------------------------------------
* Modified by Markus Dörner, markus.doerner77@gmail.com
* -------------------------------------------------------------------------
* Requires an Arduino Leonardo or other similar device that is able to
* emulate a USB HID Keyboard
* -------------------------------------------------------------------------
* Hardware to be used with iRacing and other racing sims. The simple
* hardware design means that only one button can be pressed at a time
* which is not a problem for this type of hardware. Also there is no need
* for key modifiers so only simple key presses can be sent.
* -------------------------------------------------------------------------
*/


// Angabe der Pins in der vertikalen Anordnung (Spalte)
int cPins[] = {2, 3};

// Anzahl der vertikalen Pins (Spalte)
int cPinsNo = 2;

// Angabe der Pins in der horizontalen Anordnung (Reihe)
int rPins[] = {5, 6, 7, 8};

// Anzahl der horizontalen Pins (Reihe)
int rPinsNo = 4;

// Array für die zuletzt bekannten Schaltzustände [cPinsNo][rPinsNo]
int colPrev[2][4] = {0};

// Key codes für jeden einzelnen Button
// (Spalten und Reihen werden auf den Arduino übertragen)
uint8_t buttonCodes[2][4] = {
  {0x31, 0x32, 0x33, 0x34},
  {0x36, 0x37, 0x38, 0x39}
};

void setup()
{
  // Initialisierung des digitalen PINs 9 bis 13 als Output
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);


  //Serielle Ausgabe starten, um den Serial Monitor als Ausgabe und Überwachung zu nutzen
  Serial.begin(9600);
  Serial.println("Multiplexed ButtonBox Test");

  //Set the Column Pin Mode
  Serial.println("Setting Column Pins...");

  for (int cPin = 0; cPin < cPinsNo; cPin++)
  {
    pinMode(cPins[cPin], OUTPUT);
    digitalWrite(cPins[cPin], HIGH);
  }

  //Set the Row Pin Mode
  Serial.println("Setting Row Pins...");

  for (int rPin = 0; rPin < rPinsNo; rPin++)
  {
    pinMode(rPins[rPin], INPUT);
    digitalWrite(rPins[rPin], HIGH);
  }
  Serial.println("Ready!");


}

void loop()
{
  // Loop through the columns
  for (int cPin = 0; cPin < cPinsNo; cPin++)
  {
    digitalWrite(cPins[cPin], LOW);

    // Loop through the rows
    for (int rPin = 0; rPin < rPinsNo; rPin++)
    {
      //Check if each switch is pressed
      if (digitalRead(rPins[rPin]) == LOW)
      {
        // Check to see if the state has changed since last time
        if (colPrev[cPin][rPin] == 0)
        {
          // Do action here, switch is on
          Serial.print(cPins[cPin]);
          Serial.print(", ");
          Serial.print(rPins[rPin]);
          Serial.println(" ON");


          Keyboard.press(buttonCodes[cPin][rPin]);
          //An dieser Stelle könnte man eine Quittungs-LED (0) aktivieren, für jeden Taster der gedrückt wird
          //digitalWrite(0, HIGH);
          delay(150);
          //digitalWrite(0, LOW);
          Keyboard.release(buttonCodes[cPin][rPin]);



          // Update last known state of this switch
          colPrev[cPin][rPin] = 1;
        }

        //Ab hier werden die insg. 5 verbliebenen PINS, die im Setup als Output initialisiert wurden, über
        //insg. 5 verschiedene Buttons aktiviert.


        {
          if (buttonCodes[cPin][rPin] == 0x31)
          {
            //Kein Keyboardbefehl an dieser Stelle, hier nur angeben,
            //bei welcher Taste eine LED eingeschaltet werden soll.
            //Dauerhaft solange gedrückt wird, da kein LOW Befehl.

            digitalWrite(9, HIGH);
            //delay(150);
            //digitalWrite(9, LOW);

            // Update last known state of this switch
            colPrev[cPin][rPin] = 1;
          }
        }
        {
          if (buttonCodes[cPin][rPin] == 0x32)
          {
            //Kein Keyboardbefehl an dieser Stelle, hier nur angeben,
            //bei welcher Taste eine LED eingeschaltet werden soll.
            //Dauerhaft solange gedrückt wird, da kein LOW Befehl.


            digitalWrite(10, HIGH);
            //delay(150);
            //digitalWrite(10, LOW);

            // Update last known state of this switch
            colPrev[cPin][rPin] = 1;
          }
          {
            if (buttonCodes[cPin][rPin] == 0x36)
            {
              //Kein Keyboardbefehl an dieser Stelle, hier nur angeben,
              //bei welcher Taste eine LED eingeschaltet werden soll.
              //Dauerhaft solange gedrückt wird, da kein LOW Befehl.


              digitalWrite(11, HIGH);
              //delay(150);
              //digitalWrite(11, LOW);

              // Update last known state of this switch
              colPrev[cPin][rPin] = 1;
            }
          }
          {
            if (buttonCodes[cPin][rPin] == 0x37)
            {
              //Kein Keyboardbefehl an dieser Stelle, hier nur angeben,
              //bei welcher Taste eine LED eingeschaltet werden soll.
              //Dauerhaft solange gedrückt wird, da kein LOW Befehl.


              digitalWrite(12, HIGH);
              //delay(150);
              //digitalWrite(12, LOW);

              // Update last known state of this switch
              colPrev[cPin][rPin] = 1;
            }
          }
          {
            if (buttonCodes[cPin][rPin] == 0x38)
            {
              //Kein Keyboardbefehl an dieser Stelle, hier nur angeben,
              //bei welcher Taste eine LED eingeschaltet werden soll.
              //Dauerhaft solange gedrückt wird, da kein LOW Befehl.


              digitalWrite(13, HIGH);
              //delay(150);
              //digitalWrite(12, LOW);

              // Update last known state of this switch
              colPrev[cPin][rPin] = 1;
            }
          }
        }

      }
      else {
        // Check to see if the state has changed since last time
        if (colPrev[cPin][rPin] == 1)
        {
          // Do action here, switch is off
          Serial.print(cPins[cPin]);
          Serial.print(", ");
          Serial.print(rPins[rPin]);
          Serial.println(" OFF");

          digitalWrite(9, LOW);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);

          // Update last known state of this switch
          colPrev[cPin][rPin] = 0;
        }
      }
    }
    digitalWrite(cPins[cPin], HIGH);
  }
}

Can you provide a diagram or link to show us how the buttons are wired up? I have a feeling your problem is in the wiring and has nothing to do with the code.

memoric77:
I´m a Newbie and ...

I'm an oldie and always surprised, how many newbies like to create a meaningless topic which is telling absolutely nothing about the actual problem.

Is it really increasing response rate or quality of responses when the topic doesn't tell about the problem that's going to be solved?

I'm really clueless about all the "Newbie needs help!!", "Newbie need your help" and "Need help" topics in this forum.

Must be hundreds of them. Or thousands.

And you seem to get on top of the list quickly, as you managed to post TWO "Newbie need your Help" postings into this forum within 20 minutes.

BTW: Cross-Posting in this forum is BAD!

Meaningless headline topic with each posting AND cross-posting with just 2 posts, you are surely on your way to become a notorious forums user!

Sorry for double Post, my fault. but 1. the Post belongs imho in this two threads and 2. how should i name the headline? buttonbox led Matrix Problem? it is meaningless too, the Information you need is in the Post itself. I hoped, "need your help" looks important to have a deeper look and help me out. of i would be no newbie, I could choose a better headline but it was strongly enough to try to explaining the Problem in 5 sentences.

memoric77:
buttonbox led Matrix Problem?

That would tell me a lot more than newbie needs help. And would get you considerably more views.

Are you going to bitch about this or answer the question in reply #1 so we can start helping you with your problem?

I only wanted to explain me.

I am on the way to respond and made a pic from the wiring. have to say it again, I am a “beginner”.

moderator:

  • crosspost removed
  • added code tags to original post
  • used CTRL-T in the IDE to auto indent the code to make it more readable

Thank you

memoric77:
I am on the way to respond and made a pic from the wiring. have to say it again, I am a "beginner".

Are you able to install and use third-party libraries in the Arduino-IDE?

If so, I'd recommend to install and use the "keypad libary" in your IDE:
http://playground.arduino.cc/Main/KeypadTutorial
http://playground.arduino.cc/Code/Keypad

I'd recommend to use the "getKey()" method of the Keypad object then.

This is commonly used library by many Arduino users.
I'm not sure about the arduino-button-box code.

jurs:
Are you able to install and use third-party libraries in the Arduino-IDE?

If so, I'd recommend to install and use the "keypad libary" in your IDE:
http://playground.arduino.cc/Main/KeypadTutorial
http://playground.arduino.cc/Code/Keypad
GitHub - Chris--A/Keypad: A version of the keypad library found in Wiring. This is just a copy made compatible with the Arduino IDE library manager.

I'd recommend to use the "getKey()" method of the Keypad object then.

This is commonly used library by many Arduino users.
I'm not sure about the arduino-button-box code.

it looks a little bit the same to me as my used code, isnt it? and if not, it looks difficult to me too ;).

If someone would know how to sending keyboard.release although the toogle switch is in on position, wouldnt it be the answer?

memoric77:
it looks a little bit the same to me as my used code, isnt it? and if not, it looks difficult to me too ;).

So WHAT does look so difficult to you with the keypad library?

a) Installing the third-party “Keypad” library in the IDE looks difficult?
or
b) Stripping down the example program from a 4x3 (12 buttons) keypad to a 2x4 (8 buttons) keypad looks difficult?

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

I stripped it down, I think so, but installing the third-party "Keypad" library in the IDE is new to me, have to figure it out

got the library installed (easy). Stripped it down loke this but the problem is, while the toggle switch is on, no another key could be pressed. I have to turn him off, LED is still on, and then press button 1 to turn him off. this would mean, this could only work with a momentary switch (on ->5) off (on->1) ?!

#include <Keypad.h>

const byte ROWS = 2; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’, ‘4’},
{‘5’,‘6’, ‘7’, ‘8’}

};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 2, 3 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 6, 7, 8 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 11

void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘1’:
digitalWrite(ledpin, LOW);
Keyboard.press(0x31);
Keyboard.release(0x31);
break;
case ‘5’:
digitalWrite(ledpin, HIGH);
Keyboard.press(0x35);
Keyboard.release(0x35);
break;
default:
Serial.println(key);
}
}
}

memoric77:

   Keyboard.press(0x31);

Keyboard.release(0x31);

OK, now I think I see what this thread is about:

How to use a matrix keypad with LEONARDO to emulate a HID keyboard

Sorry, I cannot tell you much about that.

Except perhaps that: When doing wrong programming you can easily brick your LEONARDO board, and then you will have to upload a working sketch (possibly "blink") with "manual reset" in the right moment, or you never will be able to upload any sketch after bricking the board.