Диванный пилот Posted November 30, 2022 Posted November 30, 2022 (edited) Hi all, I have a switch matrix, and I use it with 2pos switches like this. volatile unsigned char in_mat[6][6] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//The first number in the square brackets [4] is the number of rows, the second is number of columns [3]. The number of "0"s needs to match the multiple of these two, in this case 3 X 4 = 12. byte rowPins[6] = { 52, 50, 48, 46, 44, 42}; //These are the pin numbers being used for the rows, from 1-4 in this example, change to suit. byte colPins[6] = { 53, 51, 49, 47, 45, 43}; //These are the pin numbers being used for the columns, from 1-3 in this example, change to suit. int numRows = sizeof(rowPins);// this line obtains number of pins in the row array for later. DON'T CHANGE. int numCols = sizeof(colPins);//this line obtains number of pins in the column array for later. DON'T CHANGE. DcsBios::MatActionButtonSet dplDecDevToggle("DPL_DEC_DEV", &in_mat[4][3], LOW); DcsBios::MatActionButtonSet dplIncDevToggle("DPL_INC_DEV", &in_mat[5][3], LOW); DcsBios::MatActionButtonSet dplDecPathToggle("DPL_DEC_PATH", &in_mat[1][4], LOW); DcsBios::MatActionButtonSet dplIncPathToggle("DPL_INC_PATH", &in_mat[0][4], LOW); DcsBios::MatActionButtonSet dplDecAngleToggle("DPL_DEC_ANGLE", &in_mat[3][4], LOW); DcsBios::MatActionButtonSet dplIncAngleToggle("DPL_INC_ANGLE", &in_mat[2][4], LOW); DcsBios::MatActionButtonSet dplOffCoordToggle("DPL_OFF_COORD", &in_mat[1][5], LOW); DcsBios::MatActionButtonSet dplOnCoordToggle("DPL_ON_COORD", &in_mat[0][5], LOW); void setup() { DcsBios::setup(); //Button Matrix for (int y = 0; y < numRows; y++) //Iterate through all the rows in the matrix to set the pinmode to input { pinMode(rowPins[y], INPUT_PULLUP); //INPUT_PULLUP stops the pin floating and picking up noise. If using external Pull-up resistors, this could be set to just INPUT. }; for (int x = 0; x < numCols; x++) //Iterate through all the column pins in the matrix to set the pinmode to OUTPUT-HIGH { pinMode(colPins[x], OUTPUT); digitalWrite(colPins[x], HIGH); }; void loop() { for (int x = 0; x < numCols; x++) //Iterate through each Column pin { digitalWrite(colPins[x], LOW); //set the current column output to a low (current sink) for (int y = 0; y < numRows; y++) //Iterate through each row to detect which one is pressed (connected to the OUTPUT-LOW column) { in_mat[y][x] = digitalRead(rowPins[y]); //update the state of the button in the array, with array address is defined by the current row and column ([y] and [x]). } digitalWrite(colPins[x], HIGH); //set the current column output to a high +V source to so it is not recognised as a press when the other columns are LOW. } DcsBios::loop(); } This works good. Now I want to include an KY-040 encoder in my matrix. The function template looks like this: DcsBios::RotaryEncoder r828PrstChanSel("R828_PRST_CHAN_SEL", "DEC", "INC", PIN_A, PIN_B); So I modify code to this. DcsBios::RotaryEncoder 8TuziQg48TuziQg4("BAR_L_QFE", "-3200", "+3200", in_mat[5][1], in_mat[5][2]); Unfortunatly, when I use the code above, I get a continous switching / bounce in a virtual cockpit. When I connect the encoder right to pins on the arduino board, it works good with the code: DcsBios::RotaryEncoder 8TuziQg48TuziQg4("BAR_L_QFE", "-3200", "+3200", 8, 9); Can someone help me to make encoder work properly in the switch matrix? Edited November 30, 2022 by Диванный пилот
Vinc_Vega Posted December 1, 2022 Posted December 1, 2022 (edited) I have no idea on the used matrix nor the actionbuttonset class, but is it working if you put a "&" infront of the encoders' pin definitions? like &in_mat[5][1], &in_mat[5][2] Regards, Vinc Edited December 2, 2022 by Vinc_Vega Regards, Vinc real life: Royal Bavarian Airforce online: VJS-GermanKnights.de [sIGPIC][/sIGPIC]
bojack Posted December 2, 2022 Posted December 2, 2022 I don't think it works the way you are hoping it does. I had a quick look through the DCS bios source (https://github.com/DCSFlightpanels/dcs-bios-arduino-library/blob/master/src/internal/Encoders.h) The DcsBios::RotaryEncoder method is expecting the values to be passed as PIN numbers (it does the read of the pins itself) - you are passing it a zero (from your in_mat array) which it is interpreting as PIN0 - I think is shared with the serial input(?) hence the bouncing around. I can see that there is a method DcsBios::MatRotaryEncoder that accepts pointer to a value instead - I think this may be what your are looking for (though I'm not sure how you use a pointer to an array element in c++) 1
Диванный пилот Posted December 3, 2022 Author Posted December 3, 2022 В 01.12.2022 в 19:30, Vinc_Vega сказал: I have no idea on the used matrix nor the actionbuttonset class, but is it working if you put a "&" infront of the encoders' pin definitions? like &in_mat[5][1], &in_mat[5][2] Regards, Vinc No, with this It gives no response. 22 часа назад, bojack сказал: I don't think it works the way you are hoping it does. I had a quick look through the DCS bios source (https://github.com/DCSFlightpanels/dcs-bios-arduino-library/blob/master/src/internal/Encoders.h) The DcsBios::RotaryEncoder method is expecting the values to be passed as PIN numbers (it does the read of the pins itself) - you are passing it a zero (from your in_mat array) which it is interpreting as PIN0 - I think is shared with the serial input(?) hence the bouncing around. I can see that there is a method DcsBios::MatRotaryEncoder that accepts pointer to a value instead - I think this may be what your are looking for (though I'm not sure how you use a pointer to an array element in c++) I will try today and come back with result.
Диванный пилот Posted December 3, 2022 Author Posted December 3, 2022 В 02.12.2022 в 15:36, bojack сказал: I don't think it works the way you are hoping it does. I had a quick look through the DCS bios source (https://github.com/DCSFlightpanels/dcs-bios-arduino-library/blob/master/src/internal/Encoders.h) The DcsBios::RotaryEncoder method is expecting the values to be passed as PIN numbers (it does the read of the pins itself) - you are passing it a zero (from your in_mat array) which it is interpreting as PIN0 - I think is shared with the serial input(?) hence the bouncing around. I can see that there is a method DcsBios::MatRotaryEncoder that accepts pointer to a value instead - I think this may be what your are looking for (though I'm not sure how you use a pointer to an array element in c++) Ok. Thank you! Now it works! The strange thing is that the ehcoders.h library, included in the dcs-bios-arduino-library-0.3.7 package, differs from the file from your link. MatRotaryEncoderT class is missing from the package, and I had to replace the library with the library from your link. В 01.12.2022 в 19:30, Vinc_Vega сказал: I have no idea on the used matrix nor the actionbuttonset class, but is it working if you put a "&" infront of the encoders' pin definitions? like &in_mat[5][1], &in_mat[5][2] Regards, Vinc And of cource, "&" sign is needed. Vinc_Vega, bojack, thank you all again! 1
Recommended Posts