I managed to make a panel of 20 switches with a Leonardo and two MCP23017 IC on a breadboard. Every time I press a switch it will act as Im pressing a combination of keyboard keys.
That has been working fine. But now that Im trying to improve things for safety and stability of the project Im facing problems when I try to build it with CJMCU-2317 board.
Because when I had everything on the breadboard it was a delicate mess mixed with the resistors needed and every time the panel was moved, some cables would get lose so I had to spend time looking for which one was lose and so. Using this module would allow me to clear the interior of the panel and keep things tidy.
When I started this project, I considered the matrix to connect the switches, but at the time I thought that using the chips would allow me to increase the numebr of switches whenever it was needed in an easier way than having to re-design and solder a new matrix every time.
I guess if the buttons are mounted on a different panel to the Leonardo, having the MCP module mounted on the button panel and only 2 wires plus Vcc and GND going back to the Leonardo would make the wiring simpler.
I believe I have a Pro Micro somewhere. I could try to use it instead of the Leonardo. I remember I used the Leonardo because at that time, it was the one used in a project to simulate a keyboard, which it’s indeed what the panel does. The Arduino detects a switch being pressed and it sends a simulated key to the PC.
The resistors were pull-up resistors for the I2C bus like in the scheme. This is the actual schematic I followed to do the panel.
That is my idea. Set the Leonardo on the back of the panel, and only those 4 cables ( Vcc,GND,SDA and SCK ) going into the panel to each of the modules located on each secction.
Yes, but correct me if Im wrong please. As far as I know, implementing the matrix is more complex than having the buttons connected individually to each module,right? Of course, there are a lot more cables, but the schematics are more simple. Am I correct?
Yes it makes the code a little more complex, but there are Arduino libraries which will make the coding more simple.
Yes, and more components. More components makes the code more complex and the circuit more expensive.
No, because there are more components and more wires, the schematic is more complex.
My advice would be to use only the Leonardo/Pro Micro. You can read 100 buttons/switches with no extra chips, making the circuit simpler and cheaper, and allowing you to expand your 50-60 switches in the future.
Reviewing your code, I can't immediately see a problem. I would suggest:
Comment out the keyboard library code while you test the remaining code by simply using Serial.print().
I'm not familiar with that debouncer library, but I can forsee it becoming a performance and memory problem if you eventually want to use 60 debouncer objects.
Debouncing is easy, you really don't need a library. Don't read the switches continuously, read them once every 20ms, then bouncing won't be a problem.
Read all 16 switches/MCP pins with a single function, not individually pin-by-pin, otherwise you will get performance problems. Use bitRead() to extract the values for individual switches from the value returned by the function.
If you find yourself naming multiple variables with digits as part of their names, or copying & pasting parts of your code multiple times, making only minor changes, you have left the path of programming wisdom. The forum can help.