Example
Here is an example of a setup using both preset functions:
What I want to achieve:
The function multiFunction2Bit24() will use a 2-bit encoder and a 12-position switch (resistor ladder switch) together to make a 24-position complex. Basically the position of the 12-position switch determines the button numbers for the encoder, making it very valuable for setting up in-car adjustments and other things.
I want:
MultiFunction setup in presets 1-4
12-position switch and incremental encoder separated in presets 5-8
Both switches work as incremental encoders in presets 9-11
Preset 12 is for rally, so I want my thumb encoders inactive in this preset. I tend to hit them when the wheel goes wild and I dont want any stray button presses. I want the 12 position switch back to a MultiFunction function, but now with two buttons instead of the encoder.
The first thought when setting this up would be on the button numbering. Lets say I reserve button numbers 1-24 for the MultiFunction. If the MultiFunction is not used, the 12 position switch only needs the first 12 button numbers, 1-12, and the remaining 13-24 are free. So the 12-position switch can use buttons 13+14 in incremental mode, and the encoder can use button numbers 15+16. The two buttons used in MultiFunction in preset 12 needs to be able to use in presets 1-4 as well, when the encoder is used in MultiFunction - so these buttons need numbers higher than 24.
Lets say for this example that:
12-position switch is analog switch #1, wired to arduino pin A3
Encoder is wired to Row 1, Columns 4+5
The two buttons are on Row 3 Column 3 and Row 4 Column 2.
The 10_MatrixAndAnalog.ino might look a bit like this
//------------------------------
//-------MATRIX VARIABLES-------
//------------------------------
uint8_t row[] = {2, 3, 4, 6};
const uint8_t rowCount = sizeof(row) / sizeof(row[0]);
uint8_t col[] = {10, 15, 14, 16, 5};
const uint8_t colCount = sizeof(col) / sizeof(col[0]);
bool nonMatrixIncluded = false;
uint8_t nonMatrix[] = {0};
const uint8_t nonMatrixCount = sizeof(nonMatrix) / sizeof(nonMatrix[0]);
//---------------------------------------
//--------MATRIX DESCRIPTION-------------
//---------------------------------------
const uint8_t buttonNumber[rowCount][colCount] =
{
{26, 27, 28, 14, 0}, //ROW 1
{40, 35, 36, 37, 38}, //ROW 2
{39, 29, 24, 30, 31}, //ROW 3
{41, 25, 34, 32, 33} //ROW 4
};
//---------------------------------------
//--------ANALOG DESCRIPTION-------------
//---------------------------------------
const uint8_t analogButtonNumber[10] = //ANALOG BUTTONS 1
{ 0, 42, 0, 0, 0, 0, 0, 0, 0, 0 };
const uint8_t analogButtonNumberIncMode[10] = //ANALOG BUTTONS 2
{ 12, 54, 0, 0, 0, 0, 0, 0, 0, 0 };Now, for the setup in 30_Switches.ino, none of these 4 switches that will swap functions should be mentioned there. They should all be sorted out in 31_RunningPresets.ino, and each preset should handle these switches somehow. If the switches are also given a function in 30_Switches.ino, that will override anything written in 31_RunningPresets.ino.
31_RunningPresets.ino setup:
Note that to inactivate the encoder in preset 12, I just had to not call the encoder function.
Now that the correct switch functions are used in each presets, we'll have to use 32_Presets.ino to set the correct switch modes in each preset. When presets(int8_t presetNumber) is run, all switch modes are set to 0. So only the switches that needs to be in a mode other than 0 needs to be included. In this case that means only the 12-position switch in presets 9-11, where it will be in mode 1.
Last updated