Help with keyboard matrix and arduino

Hello,
I am not very confortable with coding but I have been given an accordion left hand keyboard. I have successfully managed to make the matrix work, I can display which row and columns are keyd but I cannont access to the array I have set up.
If someone could help, I can feel it is simple.

I use this library to interact with the matrix
https://github.com/ReneRichterDE/ButtonMatrix

here is the arduino sketch :

/**
  *****************************************************************************
  Module        ButtonMatrix
  @file         Example02_fell_rose.ino
  -----------------------------------------------------------------------------
  @brief        Example showing basic button matrix usage with
                the rose(), fell() and isLongPress(..) methods of a button
  -----------------------------------------------------------------------------
  @author       Rene Richter
  @date         21.01.2024
  @modified     -
  @copyright    (c) 2023-2024 Rene Richter
  @license      This library is free software; you can redistribute it and/or
                modify it under the terms of the GNU Lesser General Public
                License as published by the Free Software Foundation; version
                2.1 of the License.

                This library is distributed in the hope that it will be useful,
                but WITHOUT ANY WARRANTY; without even the implied warranty of
                MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                See the GNU Lesser General Public License for more details.
  *****************************************************************************
*/

/**
 * What you need to do to work with ButtonMatrix:
 *
 * 1. Add the library to your project
 * 2. Include the header file in main.ino/main.cpp (or wherever you need it)
 * 3. Either add "using namespace RSys;" or just prefix all ButtonMatrix types with "RSys::" (i.e. "RSys::Button")
 * 4. Define the column pins
 * 5. Define the row pins
 * 6. Define your buttons
 * 7. Create an instance of the ButtonMatrix passing the information of steps 4. to 6.
 * 8. Make sure to call the init() method in setup()
 * 9. Place a call to the update() method in loop() always before dealing with the state of the buttons
 */


#include <Arduino.h>
#include "ButtonMatrix.h" /** Include this header in order to work with the button matrix */



/** Everything in the ButtonMatrix library is within this namespace */
using namespace RSys;


static const uint32_t c_uiMonitorBaud = 115200; // USB monitoring baud rate

// -------------
// Button matrix
// -------------


const uint16_t longPressDuration = 1000; /** Minimum duration of a long press */

const uint8_t COLS = 4; /** Number of button matrix columns */
const uint8_t ROWS = 6; /** Number of button matrix rows */

uint8_t colPins[COLS] = {8,9,10,11}; /** Button matrix column pins */
uint8_t rowPins[ROWS] = {2,3,4,5,6,7}; /** Button matrix row pins */

/** Button matrix button definitons */
Button buttons[ROWS][COLS] = {
    { ("mi"), ("sib"), ("MI"), ("SIb") },
    { ("la"), ("mib"), ("LA"), ("MIb") },
    { ("re"), ("lab"), ("RE"), ("LAb") },
    { ("sol"), ("dod"), ("SOL"), ("DOd") },
    { ("do"), ("fad"), ("DO"), ("FAd") },
    { ("fa"), ("si"), ("FA"), ("SI") }
};


ButtonMatrix matrix((Button*)buttons, rowPins, colPins, ROWS, COLS);


void setup()
{
    Serial.begin(c_uiMonitorBaud);

    matrix.init();  /** Initialize the ButtonMatrix*/
    //matrix.setInvertInput(); /** Uncomment if you get a pressed signal while button is released and vice versa */
}


void loop(){

//-----------------------------------------------------------------------------

      Button* pButton = NULL;


   if (matrix.update())
    {

  
    for (uint8_t row = 0; row < ROWS; row++)
    {
        for (uint8_t col = 0; col < COLS; col++)
        {


 pButton = matrix.getButton(row,col);
            if (pButton->fell())
            {
               Serial.print ("row");
               Serial.print (row);
               Serial.print ("     ");
               Serial.print ("col");
               Serial.println (col);
           }
        }

        }
    }
}

and the keyboard

1 Like

BTW, the github link above is broken.

Accessing it with this link: https://github.com/ReneRichterDE/ButtonMatrix, it looks like the buttons two dimensional array should contain integers, not strings like you did.

And then, when a button is pressed you can access the buttons number with the
Serial.println(pButton->getNumber()); method.

So replace the Button declaration with something like:

Button buttons[ROWS][COLS] = {
{ 0, 1, 2, 3},
{ 4, 5, 6, 7},
{ 8, 9,10,11},
{12,13,14,15},
{16,17,18,19},
{20,21,22,23}
};

char *names[24] = {
 "mi" , "sib", "MI" , "SIb",
 "la" , "mib", "LA" , "MIb",
 "re" , "lab", "RE" , "LAb",
 "sol", "dod", "SOL", "DOd",
 "do" , "fad", "DO" , "FAd",
 "fa" , "si" , "FA" ,  "SI" 
};

And then, if you want to print the note name, you could just add the following line after Serial.println(col);

Serial.println(names[pButton->getNumber()]);

Sorry if there are syntax errors, I did not run this by a compiler.
I don’t think the parenthesis do anything in the Button array, but there might be some class syntax that I have forgotten, so you may want to add them back in to match the examples in the github.

Hope this helps.

1 Like

Hello,
I am sorry to reply so late. This was a quote problem. I managed to make it work in the end. I will try to make it with a rpi Pico soon.
thanks a lot anyway
have a nice day