Hi, I'm working on a gaming device that consists of two screens with 31 buttons, 1 rotary switch (3 positions) and two potentiometers each one. Essentially this: https://i0.wp.com/totalcontrols.eu/wp-content/uploads/2022/06/Total_Controls_1657webb.jpg?fit=1920%2C1009&ssl=1
My idea is to use only one Pro Micro board to control both, and for that, I wanted to use a PCF8575 for each screen in the following way: use 12 of the pins for a 6x6 matrix, and 3 of the rest of the inputs for the rotary switch.
I already have another working device that uses a PCF8575 with an 8x8 matrix. The simplified code for that other device (removing the parts that are not relevant) is:
#include "Joystick.h"
#include "Wire.h"
#define address 0x20
byte buttons[8][8] = {
{ 0, 1, 2, 3, 4, 5, 6, 7 },
{ 8, 9, 10, 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20, 21, 22, 23 },
{ 24, 25, 26, 27, 28, 29, 30, 31 },
{ 32, 33, 34, 35, 36, 37, 38, 39 },
{ 40, 41, 42, 43, 44, 45, 46, 47 }
{ 48, 49, 50, 51, 52, 53, 54, 55 },
{ 56, 57, 58, 59, 60, 61, 62, 63 },
};
bool buttonvalues[8][8] = {
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
};
const bool initAutoSendState = true;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 64, 0, false, false, true, true, true, true, false, false, false, false, false);
void setup() {
Wire.begin();
Wire.beginTransmission(address);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.endTransmission();
Joystick.begin();
}
void loop() {
byte read_value1;
byte read_value2;
byte write_value = 1;
for (int n = 0; n < 8; n++) {
byte temp = ~write_value;
Wire.beginTransmission(address);
Wire.write(temp);
Wire.write(0xFF);
Wire.endTransmission();
if (Wire.requestFrom(address, 2) == 2) {
read_value1 = (Wire.read());
read_value2 = (Wire.read());
}
for (int i = 0; i < 8; i++) {
if (bitRead(read_value2, i) != buttonvalues[i][n]) {
Joystick.setButton(buttons[i][n], !bitRead(read_value2, i));
buttonvalues[i][n] = bitRead(read_value2, i);
}
}
write_value <<= 1;
}
}
So essentially as you can see, is what a standard keypad does, setting rows (or cols, I can't remember which one was wich) as output highs by writing them 0xFF, and scanning row by row, which one is pulled down. With that in mind, I check the button ID in array buttons, and assign it a value in array buttonvalues.
Now for my project, I want to do the same, but I only need a 6x6 matrix, and I want to be able to use the other 4 pins (I only need 3), the above code won't work, because I'm writing all the pins to high at some point, and that may give a wrong reading of the other two bits.
So my question is, how can I modify the above code to use the leftover pins? If I could write bit by bit, instead of having to write a full byte, I think it would be easier, but I don't think that's the case.
Also, can you write a byte in binary instead of HEX? so wire.write(111111111) instead of wire.write(0xFF). Because if that's possible, then I could read the last two bits and just write whatever they read.
And since people in this forum is very fond of schematics, and although I don't think is really needed for this question, here it is: