#include <BfButtonManager.h>
byte clockpin[] = { 40, 44, 48, 52}; // "clock" pin array
byte datapin[] = { 38, 42, 46, 50}; // "data" pin array
byte pushpin[] = { 39, 43, 47, 51}; // "push" pin array
const byte arraysize = sizeof(clockpin) / sizeof(clockpin[0]); // number of encoders
byte pushstate[arraysize]; // state of each button
byte clockstate[arraysize], lastclockstate[arraysize], datastate[arraysize]; // states
byte encocount[arraysize]; // encoders count 0 to 255
byte pushcount[arraysize]; // pushbuttons count 0 to 255
//PUSH BUTTONS
int numbuts = 32;
int bts[32]; //
boolean btgs[32]; //sends one message, not continuous data
unsigned long pushtimer[arraysize], timeout = 350; // adjust timeout for lowest value
#define BAUD 9600
void setup() {
Serial.begin(BAUD); // initialize Serial Monitor
for (byte i = 0; i < arraysize; i++) { // configure DIO pins
pinMode(clockpin[i], INPUT_PULLUP); //
pinMode(datapin[i], INPUT_PULLUP); //
pinMode(pushpin[i], INPUT_PULLUP); //
lastclockstate[i] = digitalRead(clockpin[i]); // first state reading
//pinMode(bt1, INPUT_PULLUP); //pushbutton #1 pin 6, single button code
//bt1g = false; //single button code
for(int b=0; b<numbuts; b++) bts[b] = b+6;
for(int b=0; b<numbuts; b++) btgs[b] = false;
for(int b=0; b<numbuts; b++) pinMode(bts[b], INPUT_PULLUP);
}
}
//PUSH BUTTONS
int buttonStatus = 0;
void loop() {
for (byte i = 0; i < arraysize; i++) {
// pushbutton ENCODER
pushstate[i] = digitalRead(pushpin[i]); // read each pushbotton
if ((pushstate[i] == LOW) && (millis() - pushtimer[i] > timeout)) { // debounce pushbutton
pushtimer[i] = millis(); // set new timer
++pushcount[i]; // increment pushbutton count
showstates("Button : ", i, " | Presscount: ", pushcount[i]); // send data to formatter
}
// clockstate
clockstate[i] = digitalRead(clockpin[i]); // read clock state for each encoder
if (clockstate[i] != lastclockstate[i]) { // if clock state changed
lastclockstate[i] = clockstate[i]; // store state
// datastate
datastate[i] = digitalRead(datapin[i]); // datastate for each encoder
if (datastate[i] == 1 && clockstate[i] == 0) { // states for CC rotation
encocount[i]++; // increment counter
showstates("Encoder: ", i, " | CW | Count: ", encocount[i]); // send data to formatter
}
if (datastate[i] == 0 && clockstate[i] == 0) { // states for CW rotation
encocount[i]--; // decrement counter
showstates("Encoder: ", i, " | CC | Count: ", encocount[i]); // send data to formatter
}
}
//PUSH BUTTONS
for(int b=0; b<numbuts; b++){
if(!btgs[b]) {
if(digitalRead(bts[b])==LOW){
Serial.print("bt" + String(b) + ":");
Serial.println(1);
btgs[b] = true;
}
}
else {
if(digitalRead(bts[b])==HIGH) {
Serial.print("bt" + String(b) + ":");
Serial.println(0);
btgs[b] = false;
}
}
}
delay(5);
}
}
void showstates(char *name, int number, char *action, int count) { // Serial Monitor formatting
Serial.print(name);
Serial.print(number);
Serial.print(action);
Serial.print(count);
Serial.println();
}