Aiutino per settare libreria Keypad a tastiera matrice qwerty

Ciao a tutti,
ho due problemini con l'utilizzo della libreria keypad.h , ho connesso una tastiera a matrice standard Qwerty 16x8 ricavando dalla scheda di interfaccia USB originale la composizione righe colonne e relativi caratteri. Ho cablato il tutto ad un Arduino Mega rispettando la matrice originale.

Lo scopo è inviare su seriale il carattere premuto, il tasto Funzione premuto (F1,F2 ESC etc ), e nel caso di Shift e Blocco Maiuscolo i relativi caratteri maiuscoli.

Utilizzando un singolo tasto alla volta il tutto funziona perfettamente. Ricevo sulla seriale il carattere premuto ed è coerente alla matrice.

Il primo problema è come gestire i tasti funzione, tipo F1 ,F2 ALT etc
La marice è un array di Char e facendo la comparazione per qualche ragione mi interpreta alcune funzioni come caratteri e viceversa, probabilmente sbaglio qualcosa nel codice o la matrice non è adatta allo scopo.

Il secondo problema è che vorrei poter utilizzare lo Shift e il Blocco Maiuscolo come trigger per inviare i relativi caratteri maiuscoli, nella libreria ho trovato la funzione HOLD che mi dice se un tasto è mantenuto premuto ma creando la seconda matrice con i caratteri "alti" il codice si blocca e finchè non rilascio lo Shift non mi restituisce il carattere del secondo tasto premuto

Se qualcuno ha già utilizzato la libreria per qualcosa di analogo e potesse dare un occhio al codice per darmi qualche dritta sarebbe fantastico.

Grazie in anticipo

Flavio

/* 
 TEST SCHEDA DI INTERFACCIA TASTIERA QWERTY -> SERIALE
*/

#include <Keypad.h>

char customKey;


bool shift_lock = false;

char F1 = "F1";
char F2 = "F2";
char F3 = "F3";
char F4 = "F4";
char F5 = "F5";
char F6 = "F6";
char F7 = "F7";
char F8 = "F8";
char F9 = "F9";
char F10 = "F10";
char F11 = "F11";
char F12 = "F12";
char F13 = "F13";

char ESC = "ESC";
char TAB = "TAB";
char BM = "B_CAPS";
char BN = "B_NUM";
char SHF1 = "SHIFT1";
char SHF2 = "SHIFT2";
char CTRL1 = "CTRL1";
char CTRL2 = "CTRL2";
char WIN = "WIN";
char CR = "ENTER";

char UP = "UP";
char DW = "DW";
char DX = "DX";
char SX = "SX";
char BKS = "BKSP";
char A_GR = "AGR";
char ALT1 = "ALT1";
char ALT2 = "ALT2";
char FN = "FN";
char DEL = "DEL";
char SLH = "SLH";
const byte ROWS = 8; //four rows
const byte COLS = 16; //four columns
//define the symbols on the buttons of the keypads

char normalKeys[ROWS][COLS] = {
 // C1    C2    C3    C4    C5    C6    C7    C8    C9    C10   C11   C12   C13   C14   C15   C16   
  
{   '1'  ,'2'  ,'3'  ,'4'  ,'7'  ,'8'  ,'9'  ,'@'  ,'@'  ,'@'  ,'@'   ,F10  ,'0'  ,'@'  ,F5    ,'@'  },
{   SLH  ,F1   ,F2   ,'5'  ,'6'  ,'ì'  ,F8  ,'@'   ,'@'  ,DEL  ,'@'   ,F9   ,'@'  ,'@'  ,CTRL1 ,'@'  },
{   '@'  ,'@'  ,'@'  ,'b'  ,'n'  ,'@'  ,'@'  ,SX   , DX  , DW  ,'@'   ,'@'  ,'-'  ,A_GR ,'@'   ,'@'  },
{   'z'  ,'x'  ,'c'  ,'v'  ,'m'  ,','  ,'.'  ,'@'  ,'@'  , BN  ,'@'   ,CR   ,'@'  ,'@'  ,CTRL2 ,'@'  },
{   ESC  ,'@'  ,F4   ,'g'  ,'h'  ,F6   ,'@'  ,UP   ,'@'  , ' ' ,'@'   ,'@'  ,'à'  ,'@'  ,'@'   ,'@'  },
{   'a'  ,'s'  ,'d'  ,'f'  ,'j'  ,'k'  ,'l'  ,'@'  ,'@'  ,'@'  , F11  ,F12  ,'ò'  ,FN   ,'@'   ,ALT2  },
{   TAB  ,BM   ,F3  ,'t'  ,'y'  ,'+'   ,F7   ,'@'  ,'@'  ,'@'  , '*' ,BKS  ,'è'  ,'@'  ,'@'   ,WIN  },
{   'q'  ,'w'  ,'e'  ,'r'  ,'u'  ,'i'  ,'o'  ,'@'  ,'@'  ,'@'  ,'@'   ,'@'  ,'p'  ,'@'  ,F13   ,'@'  }
};


char shiftedKeys[ROWS][COLS] = {
 // C1    C2    C3    C4    C5    C6    C7    C8    C9    C10   C11   C12   C13   C14   C15   C16   
  
{   '1'  ,'2'  ,'3'  ,'4'  ,'7'  ,'8'  ,'9'  ,'@'  ,'@'  ,'@'  ,'@'   ,F10  ,'0'  ,'@'  ,F5    ,'@'  },
{   SLH  ,F1   ,F2   ,'5'  ,'6'  ,'ì'  ,F8  ,'@'   ,'@'  ,DEL  ,'@'   ,F9   ,'@'  ,'@'  ,CTRL1 ,'@'  },
{   '@'  ,'@'  ,'@'  ,'B'  ,'N'  ,'@'  ,'@'  ,SX   , DX  , DW  ,'@'   ,'@'  ,'-'  ,A_GR ,'@'   ,'@'  },
{   'Z'  ,'X'  ,'C'  ,'V'  ,'M'  ,','  ,'.'  ,'@'  ,'@'  , BN  ,'@'   ,CR   ,'@'  ,'@'  ,CTRL2 ,'@'  },
{   ESC  ,'@'  ,F4   ,'G'  ,'H'  ,F6   ,'@'  ,UP   ,'@'  , ' ' ,'@'   ,'@'  ,'à'  ,'@'  ,'@'   ,'@'  },
{   'A'  ,'S'  ,'D'  ,'F'  ,'J'  ,'K'  ,'L'  ,'@'  ,'@'  ,'@'  , F11  ,F12  ,'ò'  ,FN   ,'@'   ,ALT2  },
{   TAB  ,BM   ,F3  ,'t'  ,'y'  ,'+'   ,F7   ,'@'  ,'@'  ,'@'  , '*' ,BKS  ,'è'  ,'@'  ,'@'   ,WIN  },
{   'Q'  ,'W'  ,'E'  ,'R'  ,'U'  ,'I'  ,'O'  ,'@'  ,'@'  ,'@'  ,'@'   ,'@'  ,'P'  ,'@'  ,F13   ,'@'  }
};

byte rowPins[ROWS] = {6, 7, 8, 9, 10, 11, 12, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22 }; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad normalKeypad = Keypad( makeKeymap(normalKeys), rowPins, colPins, ROWS, COLS); 
Keypad shiftedKeypad = Keypad( makeKeymap(shiftedKeys), rowPins, colPins, ROWS, COLS);


void setup(){
  Serial.begin(57600);
  normalKeypad.addEventListener(keypadEvent); // Add an event listener for this keypad
  normalKeypad.setDebounceTime(50);
  shiftedKeypad.setDebounceTime(50);
}
  
void loop(){

char shiftedKey = sciftedKeypad.getKey();
char normalKey = normalKeypad.getKey();

if (shiftedKey){ Serial.println(sciftedKey);}
if (normalKey){ Serial.println(normalKey);}  
      
if (shift_lock == false ){customKey = normalKey; } 
if (shift_lock == true ){customKey = sciftedKey; } 
  
  if (customKey){
    Serial.println(customKey);
    if        ( customKey == F1 ){ Serial.println("F1"); }
    else if   ( customKey == '.' ){ Serial.println("."); }
    else if   ( customKey == F2 ){ Serial.println("F2"); }
    else if   ( customKey == F3 ){ Serial.println("F3"); }
    else if   ( customKey == F4 ){ Serial.println("F4"); }
    else if   ( customKey == F5 ){ Serial.println("F5"); }
    else if   ( customKey == F6 ){ Serial.println("F6"); }
    else if   ( customKey == F7 ){ Serial.println("F7"); }
    else if   ( customKey == F8 ){ Serial.println("F8"); }
    else if   ( customKey == F9 ){ Serial.println("F9"); }
    else if   ( customKey == F10 ){ Serial.println("F10"); }
    else if   ( customKey == F11 ){ Serial.println("F11"); }
    else if   ( customKey == F12 ){ Serial.println("F12"); }
    else if   ( customKey == F13 ){ Serial.println("F13"); }
    else if   ( customKey == ESC ){ Serial.println("ESC"); }
    else if   ( customKey == TAB ){ Serial.println("TAB"); }
    else if   ( customKey == BM ){ Serial.println("B_CAPS"); }
    else if   ( customKey == BN ){ Serial.println("B_NUM"); }
    else if   ( customKey == SHF1 ){ Serial.println("SHIFT1"); }
    else if   ( customKey == SHF2 ){ Serial.println("SHIFT2"); }
    else if   ( customKey == CTRL1 ){ Serial.println("CTRL1"); }
    else if   ( customKey == CTRL2 ){ Serial.println("CTRL2"); }
    else if   ( customKey == WIN ){ Serial.println("WIN"); }
    else if   ( customKey == CR ){ Serial.println("ENTER"); }
    else if   ( customKey == UP ){ Serial.println("UP"); }
    else if   ( customKey == DW ){ Serial.println("DW"); }
    else if   ( customKey == DX ){ Serial.println("DX"); }
    else if   ( customKey == SX ){ Serial.println("SX"); }
    else if   ( customKey == BKS ){ Serial.println("BKSP"); }
    else if   ( customKey == A_GR ){ Serial.println("AGR"); }
    else if   ( customKey == ALT1 ){ Serial.println("ALT1"); }
    else if   ( customKey == ALT2 ){ Serial.println("ALT2"); }
    else if   ( customKey == FN ){ Serial.println("FN"); }
    else if   ( customKey == DEL ){ Serial.println("DEL"); }
    else if   ( customKey == SLH ){ Serial.println("SLH"); }
    
    Serial.println(customKey);

    }
   }

       void keypadEvent(KeypadEvent key){
    switch (normalKeypad.getState()){
    case RELEASED:
        if (key == '*') {
          
            shift_lock = false;
            Serial.println("SHIFT OFF");
            
        }
        break;
    case HOLD:
        if (key == '*') {
            shift_lock = true;
            Serial.println("SHIFT ON");
                    }
        break;
    }
       }

Un primo problema potrebbe essere che assegni delle stringhe a variabili char.

La libreria non funziona sul arduino MEGA ma solo su Arduino con il ATmega32U4 o la DUE.
Per riuscire a leggere l'attivazione di 2 o piú pulsanti contemporaneamente devi mettere un diodo in serie a ogni pulsante.

Ciao Uwe

uwefed:
La libreria non funziona sul arduino MEGA ma solo su Arduino con il ATmega32U4 o la DUE.
Per riuscire a leggere l'attivazione di 2 o piú pulsanti contemporaneamente devi mettere un diodo in serie a ogni pulsante.

Ciao Uwe

Ciao Uwe, il lato hardware è esattamente come l'hai rappresentato, pulsante e diodo per ogni Nodo, sai qual'è il motivo per cui non dovrebbe funziona sull'Atmega 2560 ?
A parte il problema software per cui non riesco a "usare" una matrice diversa da quella dichiarata all'avvio se premo due tasti in contemporanea li legge correttamente entrambi , ad esempio se tengo premuto Shift + lettera "a" e commento la routine che dovrebbe passare alla matrice con le maiuscole sulla seriale ricevo sia lo schift sia la lettera "a" correttamente.
Essendo questo un prototipo con il Mega che una volta sistemato a livello di codice diventerà una schedina standalone potrei usare anche un micro diverso come il 32U4, ho previsto il 2560 perchè ne ho parecchi in casa.

uwefed:
La libreria non funziona sul arduino MEGA ma solo su Arduino con il ATmega32U4 o la DUE.

Occhio Uwe, NON sta parlando della libreria "Keyboard", ma della libreria "Keypad" ... immagino QUESTA (visto che non ha messo il link) ... ::slight_smile:

Guglielmo

ups.

UPDATE

Ho risolto il problema riscrivendo il codice che esegue la scansione delle righe e colonne , la libreria Keypad.h ufficiale ha dei limiti/bug nella gestione dei tasti contemporanei e nell'uso di matrici di caratteri multiple.
Ora funziona tutto a meraviglia e posso usare matrici di "String" per passare anche le funzioni speciali.