//Declarations for button matrix
const byte RowPins[] = {11,12,13,14}; //Row pins
const byte NumRows = sizeof(RowPins) / sizeof(RowPins[0]);
const byte ColPins[] = {3,4,5,6,7,8,9,10}; //Column pins
const byte NumCols = sizeof(ColPins) / sizeof(ColPins[0]);
bool BtnStateTable[NumRows][NumCols];
//Declarations for 3-Pos Toggle Switch
class Toggle3Pos {
public:
int pinA;
int pinB;
int value;
Toggle3Pos(int a, int b) { //Contstructor (allow us to set parameters at instantiation, as done below for pot1 thru 4)
pinA = a;
pinB = b;
}
};
Toggle3Pos tgl3Pos1(16, 17);
Toggle3Pos tgl3Pos2(18, 19);
Toggle3Pos tgl3Pos3(20, 21);
Toggle3Pos toggles3Pos[] = {tgl3Pos1, tgl3Pos2, tgl3Pos3};
//Declarations for Poteniometers
class Potentiometer {
public:
int pin;
int lastValue;
Potentiometer(int p) { //Contstructor (allow us to set parameters at instantiation, as done below for pot1 thru 4)
pin = p;
lastValue = analogRead(p);
}
};
Potentiometer pot1(A0);
Potentiometer pot2(A1);
Potentiometer pot3(A2);
Potentiometer pot4(A3);
Potentiometer potentiometers[] = {pot1, pot2, pot3, pot4};
//Declarations for Encoders
class Encoder {
public:
int CLK; //Pin for the clock or 'CLK' output, sometimes simply referred to as 'A'
int DT; //Pin for 'DT' output, sometimes simply referred to as 'B'
int SW; //Pin for the push button switch
int clkLastState;
Encoder(int clkA, int dtB, int s) {
CLK = clkA;
DT = dtB;
SW = s;
clkLastState = digitalRead(clkA);
}
};
Encoder enc1(52, 50, 48);
Encoder enc2(53, 51, 49);
Encoder enc3(46, 44, 42);
Encoder enc4(47, 45, 43);
Encoder enc5(40, 38, 36);
Encoder enc6(41, 39, 37);
Encoder encoders[] = {enc1, enc2, enc3, enc4, enc5, enc6};
void checkBtnArray() {
for (int j = 0; j < NumCols; j++) {
digitalWrite(ColPins[j], LOW);
for (int i = 0; i < NumRows; i++) {
BtnStateTable[i][j] = digitalRead(RowPins[i]);
}
digitalWrite(ColPins[j], HIGH);
}
}
void checkToggles3Pos() {
for (Toggle3Pos tgl : toggles3Pos) {
if (digitalRead(tgl.pinA) == LOW) {
Serial.print("Toggle on Pin ");
Serial.print(tgl.pinA);
Serial.println(" is pressed.");
} else {
if (digitalRead(tgl.pinB) == LOW) {
Serial.print("Toggle on Pin ");
Serial.print(tgl.pinB);
Serial.println(" is pressed.");
}
}
}
}
void checkEncoders() {
for (Encoder e : encoders) {
bool clkCurrentState = digitalRead(e.CLK);
if (clkCurrentState != e.clkLastState) {
if (digitalRead(e.DT) != clkCurrentState && clkCurrentState == LOW) { //CW Rotation
Serial.print("Encoder on CLK pin ");
Serial.print(e.CLK);
Serial.print(", DT pin ");
Serial.print(e.DT);
Serial.println(" is turning CW.");
} else { //CCW Rotation
if (clkCurrentState == LOW) {
Serial.print("Encoder on CLK pin ");
Serial.print(e.CLK);
Serial.print(", DT pin ");
Serial.print(e.DT);
Serial.println(" is turning CCW.");
}
}
}
e.clkLastState = clkCurrentState;
}
}
unsigned long lastPotCheck = 0;
int potDelay = 100;
void checkPotentiometers() {
if (millis() - lastPotCheck > potDelay) {
for (Potentiometer pot : potentiometers) {
int currentValue = analogRead(pot.pin);
if (abs(currentValue - pot.lastValue) > 10) {
Serial.print("Potentiometer on pin ");
Serial.print(pot.pin);
Serial.print(" has a new value of ");
Serial.println(currentValue);
pot.lastValue = currentValue;
}
}
lastPotCheck = millis();
}
}
void printBtnStateTable() {
for (int j = 0; j < NumCols; j++) {
for (int i = 0; i < NumRows; i++) {
if (BtnStateTable[i][j] == LOW) {
Serial.print("Button at Row ");
Serial.print(i);
Serial.print(", Column ");
Serial.print(j);
Serial.println(" is pressed.");
}
}
}
}
void setup() {
/*
//BUTTON MATRIX PINS
for (int i = 0; i < NumRows; i++) {
pinMode(RowPins[i], INPUT_PULLUP);
}
for (int j = 0; j < NumCols; j++) {
pinMode(ColPins[j], OUTPUT);
digitalWrite(ColPins[j], HIGH);
}
//Toggles 3-Pos
for (Toggle3Pos t : toggles3Pos) {
pinMode(t.pinA, INPUT_PULLUP);
pinMode(t.pinB, INPUT_PULLUP);
}
//POTENTIOMETERS
for (Potentiometer p : potentiometers) {
pinMode(p.pin, INPUT);
}
//ENCODERS
for (Encoder e : encoders) {
pinMode(e.CLK, INPUT_PULLUP);
pinMode(e.DT, INPUT_PULLUP);
pinMode(e.SW, INPUT_PULLUP);
}
*/
Serial.begin(9600);
}
void loop()
{
checkBtnArray();
/*
checkToggles3Pos();
checkEncoders();
checkPotentiometers();
*/
printBtnStateTable();
}