hello i am not a programmer and i am trying to build FA-18 UFC for Digital Combat Simulator and i built it and its ready, I tested the code for matrix for 7x7 for a total of 49 push buttons and i am trying to add 2 ON-ON toggle switches to the Arduino Pro Micro but i am afraid to upload the below code as it was modified by me to include the 2 on-on toggle switches.
kindly help me please.
#include <Keypad.h>
#include <Joystick.h>
#define ENABLE_PULLUPS
#define NUMBUTTONS 49
#define NUMROWS 7
#define NUMCOLS 7
#define joyButton49 A2
#define joyButton50 A3
byte buttons[NUMROWS][NUMCOLS] = {
{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}
};
byte rows[] = {2,3,4,5,6,7,8};
const int rowCount = sizeof(rows)/sizeof(rows[0]);
// JP2 and JP3 are outputs
byte cols[] = {9,10,16,14,15,A0,A1};
const int colCount = sizeof(cols)/sizeof(cols[0]);
byte keys[colCount][rowCount];
void setup() {
Serial.begin(115200);
for(int x=0; x<rowCount; x++) {
Serial.print(rows[x]); Serial.println(" as input");
pinMode(rows[x], INPUT);
}
for (int x=0; x<colCount; x++) {
Serial.print(cols[x]); Serial.println(" as input-pullup");
pinMode(cols[x], INPUT_PULLUP);
}
}
void readMatrix() {
// iterate the columns
for (int colIndex=0; colIndex < colCount; colIndex++) {
// col: set to output to low
byte curCol = cols[colIndex];
pinMode(curCol, OUTPUT);
digitalWrite(curCol, LOW);
// row: interate through the rows
for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
byte rowCol = rows[rowIndex];
pinMode(rowCol, INPUT_PULLUP);
keys[colIndex][rowIndex] = digitalRead(rowCol);
pinMode(rowCol, INPUT);
}
// disable the column
pinMode(curCol, INPUT);
}
}
void printMatrix() {
for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
if (rowIndex < 10)
Serial.print(F("0"));
Serial.print(rowIndex); Serial.print(F(": "));
for (int colIndex=0; colIndex < colCount; colIndex++) {
Serial.print(keys[colIndex][rowIndex]);
if (colIndex < colCount)
Serial.print(F(", "));
}
Serial.println("");
}
Serial.println("");
}
int lastButton49State = 0;
int lastButton50State = 0;
Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 2, 0,true,true,false,false,false,true,false,true,false,false,false);
void loop() {
readMatrix();
if (Serial.read()=='!')
printMatrix();
int currentButton49State = !digitalRead(joyButton49);
if (currentButton49State != lastButton49State){
Joystick.setButton(0, currentButton49State);
lastButton49State = currentButton49State;
}
int currentButton50State = !digitalRead(joyButton50);
if (currentButton50State != lastButton50State){
Joystick.setButton(1, currentButton50State);
lastButton50State = currentButton50State;
}
}