Hi,
I am working on a project in which i need to read the state about 80 momentary and toggle switches.
I decided to organize them in a matrix.
So I designed a pcb where there are 80 molex 1x2 molex connectors in 8 cols and 10 rows to connect the switches. Each has a diode and connected, to prevent masking and ghosting.
And will also contain a 10x1 and 8x1 connector to connect the rows and cols to the Arduino.
For now I created the prototype on a perfboard just as a 2*8 matrix.
I need to check the switch states every loop and if the state of the switch has changed i need to send a message to the PC on serial. So I wrote the code that uses the internal pullup resistors of the arduino.
It works almost well, but sometimes I get the message twice, which for me means that the switches are bouncing...
The problem is that i have no idea how to correctly debounce a matrix.
And also have no idea if it is a good idea to put momentary and toggle switches in the same matrix.
I think the debounce should be done by checking the switch states out a bit later and if the state is still the same, than it is not because of bouncing, but i cannot use delay() because other inputs and outputs should not be blocked. (I need to add several leds and servos later to the project...)
My code is:
//Initialise the matrix for the switches and push buttons
const byte SWITCH_PANEL_ROWS = 2;
const byte SWITCH_PANEL_COLS = 8;
byte SWITCH_PANEL_ROW_PINS[SWITCH_PANEL_ROWS] = {2, 3};
byte SWITCH_PANEL_COL_PINS[SWITCH_PANEL_COLS] = {14,15,16,17,18,19,20,21};
//Create an array to store the states of the switches
bool switchPanelLastState[SWITCH_PANEL_ROWS][SWITCH_PANEL_COLS];
//Settings for serial
const int BAUD_RATE = 9600;
const int SERIAL_TIMEOUT = 50;
//Is pc connected?
bool isComputerConnected = false;
//Declare functions
void ScanSwitchPanel(bool initialScan = false);
//End of declare
void setup()
{
//First set the row pins
for(byte i=0; i < SWITCH_PANEL_ROWS; i++)
{
pinMode(SWITCH_PANEL_ROW_PINS[i], INPUT_PULLUP);
}
ScanSwitchPanel(true);
//Start serial for testing
Serial.begin(BAUD_RATE);
Serial.setTimeout(SERIAL_TIMEOUT);
}
void loop() {
//Read message from PC if available
if(Serial.available())
{
char commandBuffer = Serial.read();
int valueBuffer = Serial.parseInt();
//If we get C1 it means that the SimConnect client app
//on the PC wants to connect the Arduino
//C0 means, that the softaware on PC has quited
if(commandBuffer == 'C')
{
if(valueBuffer == 1)
{
isComputerConnected = true;
Serial.print('C');
Serial.print(1);
Serial.print('\n');
//Then we need to send the initial states of the switches
for (byte r = 0; r < SWITCH_PANEL_ROWS; r++)
{
for (byte c = 0; c < SWITCH_PANEL_COLS; c++)
{
Serial.print('s');
Serial.print(r);
Serial.print(',');
Serial.print(c);
Serial.print(',');
Serial.print(switchPanelLastState[r][c] ? 1 : 0);
Serial.print(';');
}
}
//Then indicate that we finished the transmission
Serial.print('I');
Serial.print(';');
}
else if (valueBuffer == 0)
{
isComputerConnected = false;
}
}
}
ScanSwitchPanel();
}
void ScanSwitchPanel(bool initialScan )
{
for (byte c = 0; c < SWITCH_PANEL_COLS; c++)
{
//We set the column value LOW
pinMode(SWITCH_PANEL_COL_PINS[c], OUTPUT);
digitalWrite(SWITCH_PANEL_COL_PINS[c], LOW);
//Read the row values
for (byte r = 0; r < SWITCH_PANEL_ROWS; r++)
{
if (initialScan)
{
//If it is the first scan of the switches we save all values
switchPanelLastState[r][c] = //We need to negate becuase we read
!((bool)digitalRead(SWITCH_PANEL_ROW_PINS[r])); //HIGH if switch is off and LOW when it is on :)
}
else
{
//Otherwise we only save if it has changed
if (switchPanelLastState[r][c] != !((bool)digitalRead(SWITCH_PANEL_ROW_PINS[r])))
{
//We save the value
switchPanelLastState[r][c] = !((bool)digitalRead(SWITCH_PANEL_ROW_PINS[r]));
//And then send the serial message if PC is connected
if (isComputerConnected)
{
Serial.print('S');
Serial.print(r);
Serial.print(',');
Serial.print(c);
Serial.print(';');
//Serial.flush();
}
}
}
}
//Set it back to HIGH
digitalWrite(SWITCH_PANEL_COL_PINS[c], HIGH);
pinMode(SWITCH_PANEL_COL_PINS[c], INPUT);
}
}
And I am enclosing the schematics of the board.
What are your suggestions for debouncing my matrix?
Thanks in advance for any help!