1

I'm a beginner using Arduino with a Teensy 3.2 board and programming it as a usb keyboard. I have two 4 button membrane switches. Their button contacts are on pins 1-8, and the 9th pin holds a soldered together wire of both membrane switches' "ground" line or whatever it's true name is; the line that completes the circuit.

Basically when you press the buttons they are supposed to simply type "a, b, c..." respectively. I've been told I need to use a matrix for this.

I'm looking for an example of how to code a keyboard matrix that effectively supports a one row/9 column line (or vice versa?) I've been unable to find that solution online.

All I have so far is this code which, when the button on the second pin is pressed, sends tons of "AAAAAAAAAAAAAAAA" keystrokes.

void setup() {
  // make pin 2 an input and turn on the 
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if(digitalRead(2)==LOW){
    //Send an ASCII 'A', 
    Keyboard.write(65);
  }
}

Would anyone be able to help?

1
  • How long do you hold the button down for? Half a second? How many "A" do you think you can print in that time. You need to detect a transition from button-not-down to button-down and then send the "A".
    – Nick Gammon
    Oct 29, 2015 at 7:00

1 Answer 1

1

By your description, you don't have a matrix. You have them wired as just 8 straight switches.

A matrix would be if you had the 4 switch pins of one keypad wired to the same switch pins of the other keypad, and the two common connections from the keypads wired to two separate IO pins.

At the moment your code is responding whenever a button is in the low state. Instead it needs to respond only when the button transitions from the high state to the low state. To do that you need to know what state all the buttons were in the previous pass through loop to compare them to what they are in this pass through loop - and if any have changed then they have either been pressed or released.

You should also implement debouncing to that you don't get false presses.

If you don't want to learn how to do all that yourself I have a handy library that will do it all for you:

If you want to convert your setup to an actual matrix to save on IO pins you want to read the Arduino Keypad tutorial:

2
  • I'm using 2 of these: s1250.photobucket.com/user/09016/media/1_zps7b32fbcc.jpg.html Everyone I spoke to on forums (and my testing) concluded that I couldn't get the buttons to function without taking those 5th connections and merging them. I'm too new to get much done with what you've said. Compiling your program can't find the DebouncedInput.h include and while it's in there I'm not sure where to copy it. (tried many) I've read the Matrix tut thrice and tried it with no success when I press the buttons. The only thing that has displayed anything was my example code and it was a mess~
    – sylcat
    Oct 28, 2015 at 19:21
  • Follow this guide to install a library: arduino.cc/en/Guide/Libraries I know those keypads well - I have one stuck to the top of my home-made keyboard (old Amiga A500). They are just 4 buttons with one common connection, not a matrix.
    – Majenko
    Oct 28, 2015 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.