Buttonmatrix.h how can I display the content of the array?

Hello.
I am making a small midi controller from a salvaged organ accordion left hand keyboard.
I have managed to make the button matrix working with this library : GitHub - ReneRichterDE/ButtonMatrix: The library allows easy interfacing with a keypad. Not only keypads connected directly to the IO pins of the microcontroller are supported, but also via I2C. Implements software debouncing

I am really bad a programming so I had a long time figuring out how to display column and row on the serial ouput (and it was really simple in the end)

Now what I would like to do is ti parse the array I declared in the arduino sketch to be able to send the coresponding midi notes.

I tried a lot of combination and none are working.
thanks for your help

here is the code for now :

/**
  *****************************************************************************
  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;
      const uint16_t numButtons = matrix.getNumButtons();


   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);
           }
        }

        }
    }
}

Have a nice day

In your buttons array, you have strings parameters, but the library (unless you modified it) only accept numbers. The compiler will give warnings about it, you should never ignore warnings, and fix them.

  • Put the strings in a separate array

  • Fix your buttons array by using numbers instead of strings, each number being the position (starting at 0) of the corresponding string in the strings array.

  • Then you can use method pButton->getNumber() to lookup the corresponding string in the strings array. Alternatively, you can use row and col to calculate the position in the strings array.

For example

char const * const notes_strings[] =
{
	"mi",
	"sib",
	"MI",
	"SIb",
	...
};

Button buttons[ROWS][COLS] = {
    { (0), (1), (2), (3) },
    ...
};

...

if (pButton->fell())
{
    Serial.println( notes_strings[ pButton->getNumber() ] );
}

The above array is a THREE dimension array... (1) rows, (2) cols, (3) chars.

Here is how I would print it out in a "table"...

const byte ROWS = 6;
const byte COLS = 4;

char buttons[][ROWS][COLS] = { // THREE dimensions: col, row, chars
  { ("mi "), ("sib"), ("MI "), ("SIb") },
  { ("la "), ("mib"), ("LA "), ("MIb") },
  { ("re "), ("lab"), ("RE "), ("LAb") },
  { ("sol"), ("dod"), ("SOL"), ("DOd") },
  { ("do "), ("fad"), ("DO "), ("FAd") },
  { ("fa "), ("si "), ("FA "), ("SI ") }
};

void setup() {
  Serial.begin(115200);
  Serial.println("     COL0  COL1  COL2  COL3");

  for (int row = 0; row < ROWS; row++) {
    Serial.print("ROW"); Serial.print(row); Serial.print(" ");
    for (int col = 0; col < COLS; col++) {
      Serial.print(buttons[row][col]);
      Serial.print("   ");
    }
    Serial.println();
  }
  Serial.println();
}
void loop() {}

Result:

     COL0  COL1  COL2  COL3
ROW0 mi    sib   MI    SIb   
ROW1 la    mib   LA    MIb   
ROW2 re    lab   RE    LAb   
ROW3 sol   dod   SOL   DOd   
ROW4 do    fad   DO    FAd   
ROW5 fa    si    FA    SI  

I do not like multi-dimension arrays because I am not skilled enough to pass them from function to function without major trouble in my programs. I prefer two dimension arraays or less. The following is a TWO dimension "flat" array (notice the lack of braces)... (1) row (2) chars

I prefer using "flat" arrays and some row/col math to make tables.

#define ROWS 6
#define COLS 4

char buttons[][4] = {
  // "\0" is a line terminator needed in flat arrays
  "mi \0", "sib\0", "MI \0", "SIb\0", 
  "la \0", "mib\0", "LA \0", "MIb\0",
  "re \0", "lab\0", "RE \0", "LAb\0",
  "sol\0", "dod\0", "SOL\0", "DOd\0",
  "do \0", "fad\0", "DO \0", "FAd\0",
  "fa \0", "si \0", "FA \0", "SI \0"
};

void setup() {
  Serial.begin(115200); // start serial communications on USB/tx,rx/D0,D1
  Serial.println("      COL0 COL1 COL2 COL3");
  for (uint8_t row = 0; row < ROWS; row++) {
    Serial.print("ROW");
    Serial.print(row);
    Serial.print("  ");
    for (uint8_t col = 0; col < COLS; col++) {
      Serial.print(buttons[(row * COLS) + col]); // FIX see post #4
      // Serial.print(buttons[row + col]); // row/col math
      Serial.print("  ");
    }
    Serial.println(); // end of row
  }
  Serial.println(); // end of table
}

void loop() {}

Result:

      COL0 COL1 COL2 COL3
ROW0  mi   sib  MI   SIb  
ROW1  sib  MI   SIb  la   
ROW2  MI   SIb  la   mib  
ROW3  SIb  la   mib  LA   
ROW4  la   mib  LA   MIb  
ROW5  mib  LA   MIb  re 

Is this what you were talking about?

That should be (col * COLS) + row I mean.. (row * COLS) + col)

1 Like

Hmm. I agree with (x + y * z) but the result is still the same... or so I thought... the result here is from the corrected table math...

      COL0 COL1 COL2 COL3
ROW0  mi   sib  MI   SIb  
ROW1  la   mib  LA   MIb  
ROW2  re   lab  RE   LAb  
ROW3  sol  dod  SOL  DOd  
ROW4  do   fad  DO   FAd  
ROW5  fa   si   FA   SI   

Thanks you both.
I feel a little stupid as the probleme is what guix said...
everything is working fine without notes names and quotation marks.
And thanks for the explanation about the three dimensions. I understand very well but I would never had thought about that.