Hi @ALL ,
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);
}
}