I am a non-programmer NOOB. What I think I am doing (based on a prior NES controller example) is mapping each pin to a usb key command bia two arrays. Then I am checking to see if a button is pressed. (rinse and repeat) putting the requested key (key) corresponding to the appropriate pin. Keyboard.set_key1(keys); into keyboard.set_key1.
Here is the thing (I tried a bunch of case statements but have not go there yet); I want to allow multiple keys to be pressed and then shuttled into the next available keyboard.setkey ... eg keyboard.setkey1 or keyboard.setkey2.
could anyone suggest an approach or provide a code snippit that I could either use or learn from. (PS Paul.. the board is great; lots of fun.. I am mating the board with 3d printing, my first pcb design! such a noob am I.) PS my last programming was on a PDP 11 decades ago.
Here is the thing (I tried a bunch of case statements but have not go there yet); I want to allow multiple keys to be pressed and then shuttled into the next available keyboard.setkey ... eg keyboard.setkey1 or keyboard.setkey2.
could anyone suggest an approach or provide a code snippit that I could either use or learn from. (PS Paul.. the board is great; lots of fun.. I am mating the board with 3d printing, my first pcb design! such a noob am I.) PS my last programming was on a PDP 11 decades ago.
Code:
const int pinzero = 0;
const int pinone = 1;
const int pintwo = 2;
const int pinthree = 3;
const int pinfour = 4;
const int pinfive = 5;
const int pinsix = 6;
const int pinseven = 7;
const int pineight = 8;
const int pinnine = 9;
const int pinten = 10;
const int pineleven = 11;
const int pintwelve = 12;
const int pinthirteen = 13;
const int pinfourteen = 14;
const int pinfifteen = 15;
const int pinsixteen = 16;
const int pinseventeen = 17;
const int pineighteen = 18;
const int pinnineteen = 19;
const int pintwenty = 20;
const int pintwentyone = 21;
//CHANGE NEEDED - USE PIN 20 AND 21 FOR MODIFYIER KEYS ALT AND CONTROL
//Variables for the states of the pins
byte numpins[] = {pinzero, pinone, pintwo, pinthree, pinfour, pinfive, pinsix, pinseven, pineight, pinnine, pinten, pineleven, pintwelve, pinthirteen, pinfourteen, pinfifteen, pinsixteen,
pinseventeen, pineighteen, pinnineteen};
byte keys[] = {KEY_W, KEY_A, KEY_S, KEY_D, KEY_SPACE, KEY_Q, KEY_R, KEY_Z, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_T, KEY_Y, KEY_O};
#define NUMBUTTONS sizeof(numpins)
void setup()
{
//Special for the Teensy is the INPUT_PULLUP
//It enables a pullup resitor on the pin.
for (byte i=0; i< NUMBUTTONS; i++) {
pinMode(numpins[i], INPUT_PULLUP);
}
}
void loop()
{
// //debugging the start button...
//digitalWrite ( pinLEDOutput, digitalRead(pinBtnStart));
//Progess the SNES controller buttons to send keystrokes.
fcnProcessButtons();
}
//Process buttons
void fcnProcessButtons()
{
static long currentkey = 0;
byte nothingpressed = 1;
// run through all the buttons
for (byte i = 0; i < NUMBUTTONS; i++) {
// are any of them pressed?
if (! digitalRead(numpins[i])) {
nothingpressed = 0; // at least one button is pressed!
// THIS SECTION NEEDS TO CHANGE TO ALLOW NKRO EG WE CAN HAVE Keyboard.set_key1 to Keyboard.set_key6 and then send
// if its a new button, release the old one, and press the new one
if (currentkey != keys[i]) {
Keyboard.set_key1(0);
Keyboard.send_now();
Keyboard.set_key1(keys[i]);
currentkey = keys[i];
Keyboard.send_now();
} else {
// the same button is pressed, so repeat!
Keyboard.set_key1(keys[i]);
Keyboard.send_now();
delay(100);
}
}
}
if (nothingpressed) {
// release all keys
Keyboard.set_key1(0);
Keyboard.send_now();
}
}
Last edited: