Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Keyboard matrix help (Teensy 3.0; constant output when nothing is pressed)

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4

    Keyboard matrix help (Teensy 3.0; constant output when nothing is pressed)

    Hello everyone,

    First of I want to share that I have experience with programming but zero of it with microcontrollers, so I apologise beforehand for any stupid mistakes.

    I want to make a simple keyboard matrix of 6 buttons to work, but the output I am getting from the teensy seems random to me.

    I am using pins 3-7. I was using the ones from 2-6 but I changed them in an attempt to solve the issue.
    Pins 3-5 are columns. Pins 6 and 7 are rows.

    This is the code. I also tried with Keyboard.send_now(). I am not using Keyboard.print() because later on I would like to use the keys for media controlls via HID codes.
    Code:
    #include <usb_keyboard.h>
    int keyArray[] = {0x1E /*1*/, 0x1F /*2*/, 0x20 /*3*/, 0x21 /*4*/, 0x22 /*5*/, 0x23 /*6*/};
    
    void setup()
    {
       //pinMode(13, OUTPUT);
       pinMode(3, OUTPUT);
       pinMode(4, OUTPUT);
       pinMode(5, OUTPUT);
       pinMode(6, INPUT);
       pinMode(7, INPUT);
    }
    
    void loop()
    {
        /* Testing code
        digitalWrite(13, HIGH);
        delay(200);
        digitalWrite(13, LOW);
        delay(200);
        */
        for(int i = 3; i <= 5; i++)
        {
          digitalWrite(i, HIGH);
          for(int j = 6; j <= 7; j++)
          {
                      
            if(digitalRead(j))
            {
              int index = (i - 3) * 2 + (j - 6);
              //press
              keyboard_keys[0] = keyArray[index];;
              usb_keyboard_send();
    
              delay(200);
            }
            else
            {
              //release
              keyboard_keys[0] = 0;
              usb_keyboard_send();
            }
          }
          digitalWrite(i, LOW);
        }
    }
    When I tried seeing what was idling with the below code it always returns the same button when I'm not pressing anything. i - 5, j - 6 - which corresponds to the bottom left button on the image.
    Code:
    Keyboard.println(digitalRead(j));
    Keyboard.println(i);
    Keyboard.println(j);

    Here's an image of my soldering. Red numbers are pin numbers. White numbers are what is supposed to be output from the keys.
    Click image for larger version. 

Name:	IMAG0151.jpg 
Views:	574 
Size:	84.6 KB 
ID:	5032

    What is actually being output is this:

    Expected output: --- Actual output:
    1 --------------------- 13
    2 --------------------- 24(5)
    3 --------------------- 35
    4 --------------------- 4(5)6
    5 --------------------- 51
    6 --------------------- 62(5)
    The 5 in the brackets sometimes is there, sometimes it's not....
    When leaving it idle while testing the actual output it sometimes starts spamming 5s, "as it should" since it 's the key output from the debug println above, but not before scrambling through other numbers.
    In other cases it continues outputting random numbers. I have observed that it is relevant with the position of the teensy but I have not reached a concrete scenario and I couldn't find any potential shortages in my solders.
    I also tried playing around with the pinmodes - result was either the same or nothing was returned.


    Thanks in advance. Any help is highly appreciated.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMAG0151.jpg 
Views:	183 
Size:	83.5 KB 
ID:	5031  

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    28,460
    Use the Bounce library for detecting button changes. It's much easier than digitalRead() and it automatically handles mechanical chatter in the buttons.

    Start with File > Examples > Teensy > USB_Keyboard > Buttons

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks for the answer but I am trying to accomplish this with a matrix. I have 3 columns and 2 rows and as far as I understand Bounce requires each button to be individually connected to a teensy pin and the GND pin.

    I tried to define the rows as buttons and check with fallingEdge() for key presses. I also tried with the Keypad example but nothing is being printed in both cases.

    Also I don't think this is a problem with mechanical chatter since it's not a single button repeating. The idle spam is endless, it doesn't stop shortly after key presses.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •