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

Thread: 24 Keys Matrix

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    5

    24 Keys Matrix

    I wrote this code to scan 24 keys matrix, it's work great with Arduino Uno but on teensy 3.2 and 3.6 I got something like "double pulsing".
    when I press on the 7th' key I got 6 and 7 reading , allmost same when I pressed the 16th' key then I got 14 and 16 reading.
    all the rest of the keys works fine.

    thanks for help ,



    the code :

    Code:
    int pressedKey[25]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    int k1_row[6]={1,2,3,4,5,6};
    int k1_col[4]={7,8,9,10};
    
    
    void setup() {
        Serial.begin(9600); 
        delay(1000);
         
      for(int i=0;i<=5;i++){
       pinMode(k1_row[i],OUTPUT);
      }
      
      for(int i=0;i<=3;i++){
        pinMode(k1_col[i],INPUT_PULLUP);
        }
      Serial.println("SETUP DONE");
    }
    
    void loop() {
    
          for(int i=0; i<=5; i++){
               digitalWriteFast(k1_row[0],HIGH);
               digitalWriteFast(k1_row[1],HIGH);
               digitalWriteFast(k1_row[2],HIGH);
               digitalWriteFast(k1_row[3],HIGH);
               digitalWriteFast(k1_row[4],HIGH);
               digitalWriteFast(k1_row[5],HIGH);
               digitalWriteFast(k1_row[i],LOW); 
              
              for(int j=0; j<=3; j++){ 
          
                int strokedKey=i*4+j+1;
                bool col_scan=digitalRead(k1_col[j]);
                     
                            
                    if(col_scan==LOW){
                        if (pressedKey[strokedKey]==0 ){
                                //playMusic(strokedKey);
                                pressedKey[strokedKey]=1 ;
                                Serial.println(strokedKey);
                        }        
                       delay(10);
                   } 
                   
                   else
                    pressedKey[strokedKey]=0;    
          
                }
                   delay(10);
    }
    }

    Click image for larger version. 

Name:	IMG-7107.jpg 
Views:	21 
Size:	137.1 KB 
ID:	26631
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG-7109.jpg 
Views:	17 
Size:	142.0 KB 
ID:	26630  
    Last edited by yinon; 11-22-2021 at 10:33 AM.

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    255
    Thinking about the differences between a UNO and a Teensy:

    The Teensy is faster and will fail when the program needs more time between steps. I think that is probably the issue and would suggest an added delay.

    Code:
           for(int i=0; i<=5; i++){
               digitalWriteFast(k1_row[0],HIGH);
               digitalWriteFast(k1_row[1],HIGH);
               digitalWriteFast(k1_row[2],HIGH);
               digitalWriteFast(k1_row[3],HIGH);
               digitalWriteFast(k1_row[4],HIGH);
               digitalWriteFast(k1_row[5],HIGH);
               digitalWriteFast(k1_row[i],LOW); 
              
    Add a delay here to allow the row signals to stabilize before reading the columns.
              for(int j=0; j<=3; j++){ 
          
                int strokedKey=i*4+j+1;
                bool col_scan=digitalRead(k1_col[j]);
    The UNO has 5 volt logic and the Teensy has 3 volt logic. The UNO has more margin for error in determining if the signal is high or low. You have your signals going through a long cable and that can cause issues. Short wires are best for speed.

    You are using the internal pullups and they may be different between a UNO and the Teensy. Generally internal pullups are high resistance and the signals will respond slower than if you used external pullups.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    5
    Hi Rcarr, thanks for the well-detailed answer.

    I do some extra tests and set the matrix with short wires and it worked well.
    so it's probably a wire length issue. I am trying to figure out if the solution is on the output side mean the matrix lines or maybe on the inputs pins used by the matrix Cullums.

    Practically I see two options :

    1. shift the logic level to 5V and raise the pulldown resistor
    2. stay with the pull-up system using external resistors and looking for the right value
    Last edited by yinon; 11-23-2021 at 09:30 AM.

  4. #4
    Junior Member
    Join Date
    Nov 2021
    Posts
    5
    last update :

    A 5.1k external pull-up resistor had solved the problem
    thanks for you're help.

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,990
    Quote Originally Posted by yinon View Post
    last update :

    A 5.1k external pull-up resistor had solved the problem
    thanks for you're help.
    Hopefully at least for the T3.6 you are using a PU that uses 3.3v as the T3.6 pins are not tolerant of 5v being applied to them.

  6. #6
    Senior Member houtson's Avatar
    Join Date
    Aug 2015
    Location
    Scotland
    Posts
    212
    Quote Originally Posted by KurtE View Post
    Hopefully at least for the T3.6 you are using a PU that uses 3.3v as the T3.6 pins are not tolerant of 5v being applied to them.
    also, the keypad library is very effective if you are looking for a ready baked solution

  7. #7
    Junior Member
    Join Date
    Nov 2021
    Posts
    5
    Quote Originally Posted by KurtE View Post
    Hopefully at least for the T3.6 you are using a PU that uses 3.3v as the T3.6 pins are not tolerant of 5v being applied to them.
    yes, of course.

    I am still dealing with the same issue. the external Pullup don't solve the problem ):

  8. #8
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    7,858
    You may want to check this post out:
    https://forum.pjrc.com/threads/62087...ght=key+matrix
    sounds like a similar problem. Solution may help.

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,990
    Like @houtson mentioned, you might try the keypad library solution.

    A long time ago I wrote my own code for a 20 button matrix... which does something similar to what you mentioned.
    But there were discussions somewhere (not sure here or Trossen or ...) about concern if multiple buttons were pressed, could I could I short two pins. One digitalWrite HIGH connected to another one digtialWrite LOW...
    So changed my digitalWrite(pin, HIGH), to pinMode(pin, INPUT_PULLUP)...

    I doubt that would help here...

    If it were me, I would try a quick and dirty debounce sort of logic... like:
    Code:
        if(col_scan==LOW){
            delay(5); // experiment on how long
            col_scan=digitalRead(k1_col[j]);
            if (col_scan==(LOW) {
                // only if still low process it...
    ...

    And see if that removes the issues.

Posting Permissions

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