Hi,
I'm working on a project that uses 3 PCF8575 16 I/O expander boards. These return the value in two bytes each, and what I do is read each board's two bytes, save them in a variable, and than store each individual bit in a 3x16 array.
I haven't been able to test the code yet, but it really feels like this part could be coded more efficiently:
byte read_value;
if (Wire.requestFrom(pcf8575_1, 2) == 2) {
read_value1_1 = (Wire.read());
read_value1_2 = (Wire.read());
}
byte read_value;
if (Wire.requestFrom(pcf8575_2, 2) == 2) {
read_value2_1 = (Wire.read());
read_value2_2 = (Wire.read());
}
byte read_value;
if (Wire.requestFrom(pcf8575_3, 2) == 2) {
read_value3_1 = (Wire.read());
read_value3_2 = (Wire.read());
}
for (int i = 0; i < 8; i++) {
buttonvalues[0][i] = bitRead(read_value1_1, i)
}
for (int i = 8; i < 16; i++) {
buttonvalues[0][i] = bitRead(read_value1_2, i)
}
for (int i = 0; i < 8; i++) {
buttonvalues[1][i] = bitRead(read_value2_1, i)
}
for (int i = 8; i < 16; i++) {
buttonvalues[1][i] = bitRead(read_value2_2, i)
}
for (int i = 0; i < 8; i++) {
buttonvalues[2][i] = bitRead(read_value3_1, i)
}
for (int i = 8; i < 16; i++) {
buttonvalues[2][i] = bitRead(read_value3_2, i)
}
I've always wondered how would you concatenate a variable name with a for. Let's say I have, for instance, var_1, var_2, and var_3, and I want to perform the same action for all three. In my mind would be something like:
for (int i = 1; i <= 3; i++) {
"var_" + i = "whatever";
}
So in the above example, all 3 variables would have a value of "whatever". But how would the actual code be?
Thank you