Using Arcade Buttons and MEGA2560 to make keystokes?

Hey all... new to the forums and need some code advice...

I've built a controller with 27 buttons on it, and purchased an arduino mega ... and need to write code so each button will act like a letter (or in some cases ctrl+letter or number etc)

so far i've found this ( http://arduino.cc/forum/index.php?topic=99.0 ) which seems like it might have some good info... but i feel like i need to make it so each specific pin on the board (each button) is assigned a keystroke.

any advice is appreciated! thanks!

and a dumb question for the same project, just to be sure... i can link one side of each switch together and send them all to the one ground right?

I assume you’ve got a button on each of 26 input pins.

Then you can do something like this

char keys [] = {0,0,'A', 'B', 'C'....};

for (int i = 2; i < 28; i++) {
    if (digitalRead (i) == LOW))
      keypressed = keys[i];
}

This assumes that you are using consecutive IO pins and not using 0 and 1 as they are the serial port. Also some debouncing should be incorporated.

If the pins aren’t consecutive then it’s an easy matter to map them with another array.

i can link one side of each switch together and send them all to the one ground right?

Correct, and enable all the pull up resistors.


Rob

thanks for the quick reply, Rob

yea i guess i was thinking id send a button to each of the pins on the arduino (digital 22 through 53?)

and in that case, with your code, am i modifying it right:

char keys [] = {0,0,'A', 'B', 'C'....};

for (int i = 2; i < 28; i++) {
    if (digitalRead (22) == LOW))
      keypressed = keys[Q];
    if (digitalRead (23) == LOW))
      keypressed = keys[W];
    if (digitalRead (24) == LOW))
      keypressed = keys[E];
}

so then the button on 22 would be Q, 23 would be W, 24 E ?

(i had arduino experience a few years ago, but its been awhile and i never was great with the coding part, ha)

I don’t think your code will work, for starters you can’t index into an array like that, also the whole point is not to have 26 “if” statements.

(digital 22 through 53?)

That’s a span of 31 pins so there are some holes right?

If for any reason the pins aren’t contigous I’d map them with an array

int pin_map[] = {22,23,24,26,28,29,30....};

for (int i = 0; i < 26; i++) {
    if (digitalRead (pin_map[i]) == LOW))
      keypressed = i + 'A'
}

Note that the pin_map array determines the order of the pins read, so the first input is on 22, the second on 23, third on 24, but the fourth input is on pin 26, we’ve skipped 25.

This way it’s a simple matter to re map the pins into any order.


Rob

i was just trying to use your code and plug in pin numbers and the corresponding key (like i said… not so good at writing code)

on the mega there is a section of digital pins, numbered 22 through 53 (although thats still 2 less than i need… havent determined which to use yet - pwm or communication, mabe?)

i guess the confusion i have with the code is that i need the buttons to be certain letters, not just 1=A, 2=B, 3=C and so on

i was hoping to get
Z X C V,
Q W E R T,
P U I O Y,
B N M,
Apple+1 ctrl+opt+5 opt+S opt+D A,
Apple+0 ctrl+opt+6 opt+J opt+K L

what i'm trying to do is...

make this:

act like this:

(to be used in addition with vestax spin controller and algoriddim djay to mess with music)

So you are saying that the first button is not necessarily ‘A’, second not ‘B’ etc.

In that case combine the two approaches I’ve mentioned.

// this array linearises the input pins
int pin_map[] = {22,23,24,26,28,29,30....};

// this array maps input pins to characters
char key_map [] = {'Z', 'X', 'C, 'V', ....};


for (int i = 0; i < 26; i++) {
    if (digitalRead (pin_map[i]) == LOW))
      keypressed = key_map[i];
}

So pressing input 22 gives you a ‘Z’.


Rob

oh awesome!

that makes sense, and is there a certain way to write things like command+1 or opt+S for example?

thanks so much for your help! really appreciated XD

heres my code then so far:

// input pins
int pin_map[] = {22,23,24,26,28,29,30,31,32,33,34,35,36,37,38,39,40};

// input pins to characters
char key_map [] = {'X', 'C', 'V', 'Z', 'Q', 'W', 'E', 'R', 'T', 'P', 'U', 'I', 'O', 'Y', 'B', 'N', 'M'};


for (int i = 0; i < 26; i++) {
    if (digitalRead (pin_map[i]) == LOW))
      keypressed = key_map[i];
}

do i need to change that i < 26 to i < 40 … or 53 when i add more

sorry for being a big newbie :blush:

is there a certain way to write things like command+1 or opt+S for example?

Not AFIAK, you'll have to find out the binary value for these combinations.


Rob

i'll look into it... thanks again for all your help, Rob!

The number controlling the for loop is simply the number of keys you have, I used 26 because at the time I thought we were talking about the entire alphabet.

In the example you've got use 17. If you add one more key use 18 etc.


Rob

oh my bad… i was thinking it was the range of pins for some reason

so just trying to get something basic going (ignoring my buttons with modifiers)

void setup() {
  
  // input pins
int pin_map[] = {22,23,24,26,28,29,30,31,32,33,34,35,36,37,38,39,40};

// input pins to characters
char key_map [] = {'X', 'C', 'V', 'Z', 'Q', 'W', 'E', 'R', 'T', 'P', 'U', 'I', 'O', 'Y', 'B', 'N', 'M'};

}

void loop() {

for (int i = 0; i < 17; i++){ 
    if (digitalRead (pin_map[]) == LOW))
      keypressed = key_map[];
  
}

still getting errors when i try to verify tho… :~

    if (digitalRead (pin_map[]) == LOW))
      keypressed = key_map[];

where's the "i" gone?

    if (digitalRead (pin_map[i]) == LOW))
      keypressed = key_map[i];

Rob

oh yea i noticed that and tried it different ways without luck… so all 's need i’s in them?

your earlier code didnt have i’s before the arrays, but in the loop (?) it did?

Graynomad:

// this array linearises the input pins

int pin_map = {22,23,24,26,28,29,30…};

// this array maps input pins to characters
char key_map = {‘Z’, ‘X’, 'C, ‘V’, …};

for (int i = 0; i < 26; i++) {
   if (digitalRead (pin_map[i]) == LOW))
     keypressed = key_map[i];
}

or something like this: ?

void setup() {
  
  // input pins
int pin_map[i] = {22,23,24,26,28,29,30,31,32,33,34,35,36,37,38,39,40};

// input pins to characters
char key_map [i] = {'X', 'C', 'V', 'Z', 'Q', 'W', 'E', 'R', 'T', 'P', 'U', 'I', 'O', 'Y', 'B', 'N', 'M'};

}

void loop() {

for (int i = 0; i < 17; i++){ 
    if (digitalRead (pin_map[i]) == LOW))
      keypressed = key_map[i];
  
}

thanks for the patience, ha

says ‘i’ was not declared in this scope

I think there is an error on every line of that code :slight_smile:

The arrays should be declared outside any function.
The “i” is not used in the declaration.
keypressed was not defined
Loop had no closing brace

  // input pins
int pin_map[] = {22,23,24,26,28,29,30,31,32,33,34,35,36,37,38,39,40};

// input pins to characters
char key_map [] = {'X', 'C', 'V', 'Z', 'Q', 'W', 'E', 'R', 'T', 'P', 'U', 'I', 'O', 'Y', 'B', 'N', 'M'};

char keypressed;

void setup() {
  

}

void loop() {

  for (int i = 0; i < 17; i++){ 
    if (digitalRead (pin_map[i]) == LOW)
      keypressed = key_map[i];
  
  }
}

Rob

hahah... yea, again ...really not good with writing code

and again... THANKS!!! very much appreciated

working on the wiring as we speak... i'll share my progress on here when i can

still no luck... mabe i need to do something to make the computer recognize the arduino as a keyboard??

still no luck

Meaning?


Rob

well, its not going to do much of anything with the code posted. you have no code that sends any data to the computer. the arduino can only work with serial communication to the computer, unless you have a newer mega or uno where you can reflash the micro acting as the serial bridge to spit out HID data, but as far as i know theres no official guide for that, and you would lose the ability to program with the serial port. one option is to see if you can find a serial keyboard emulator (sometimes used for special input devices), or if you have a ps/2 port you can emulate a keyboard in code.