#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD (I2C address, width, height)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change I2C address if necessary
// Pin definitions
const int switchPinsSE[] = {2, 3, 4, 5, 6, 7, 8, 9}; // 8 switches for SE
const int switchPinsSP[] = {10, 11, 12, 13, 30, 31, 32, 33}; // 8 switches for SP
const int switchPinsSM[] = {34, 35, 36, 37, 38, 39, 40, 41}; // 8 switches for SM
const int switchPinsSW[] = {42, 43, 44, 45, 46, 47, 48, 49}; // 8 switches for SW
const int switchPinsPM[] = {50, 51, 52, 53}; // 4 switches for PM
const int switchPinsMS[] = {A0, A1, A2, A3, 22, 23, A6, A7}; // 8 switches for MS
const int switchPinsSS[] = {A8, A9, A10, A11, A12, A13, A14, A15}; // 8 switches for SS
// Track switch states
int switchStatesSE[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int switchStatesSP[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int switchStatesSM[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int switchStatesSW[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int switchStatesPM[4] = {HIGH, HIGH, HIGH, HIGH}; // Track PM states
int switchStatesMS[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int switchStatesSS[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
// Count variables
int countSE = 0;
int countSP = 0;
int countSM = 0;
int countSW = 0;
int countPM = 0;
int countMS = 0;
int countSS = 0;
void setup() {
lcd.begin(20, 4); // Initialize the I2C LCD with width and height
lcd.backlight(); // Turn on the backlight
lcd.print("Counts:"); // Initial message
// Initialize switch pins
for (int i = 0; i < 8; i++) {
pinMode(switchPinsSE[i], INPUT_PULLUP);
pinMode(switchPinsSP[i], INPUT_PULLUP);
pinMode(switchPinsSM[i], INPUT_PULLUP);
pinMode(switchPinsSW[i], INPUT_PULLUP);
pinMode(switchPinsMS[i], INPUT_PULLUP);
pinMode(switchPinsSS[i], INPUT_PULLUP);
if (i < 4) { // Only for the first 4 of PM
pinMode(switchPinsPM[i], INPUT_PULLUP);
}
}
}
void loop() {
// Check each switch for SE
for (int i = 0; i < 8; i++) {
int currentState = digitalRead(switchPinsSE[i]);
if (currentState == LOW && switchStatesSE[i] == HIGH) {
countSE++;
switchStatesSE[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesSE[i] == LOW) {
countSE--; // Decrement count on release
switchStatesSE[i] = HIGH; // Update state
}
}
// Check each switch for SP
for (int i = 0; i < 8; i++) {
int currentState = digitalRead(switchPinsSP[i]);
if (currentState == LOW && switchStatesSP[i] == HIGH) {
countSP++;
switchStatesSP[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesSP[i] == LOW) {
countSP--; // Decrement count on release
switchStatesSP[i] = HIGH; // Update state
}
}
// Check each switch for SM
for (int i = 0; i < 8; i++) {
int currentState = digitalRead(switchPinsSM[i]);
if (currentState == LOW && switchStatesSM[i] == HIGH) {
countSM++;
switchStatesSM[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesSM[i] == LOW) {
countSM--; // Decrement count on release
switchStatesSM[i] = HIGH; // Update state
}
}
// Check each switch for SW
for (int i = 0; i < 8; i++) {
int currentState = digitalRead(switchPinsSW[i]);
if (currentState == LOW && switchStatesSW[i] == HIGH) {
countSW++;
switchStatesSW[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesSW[i] == LOW) {
countSW--; // Decrement count on release
switchStatesSW[i] = HIGH; // Update state
}
}
// Check each switch for PM
for (int i = 0; i < 4; i++) { // Only 4 switches for PM
int currentState = digitalRead(switchPinsPM[i]);
if (currentState == LOW && switchStatesPM[i] == HIGH) {
countPM++;
switchStatesPM[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesPM[i] == LOW) {
countPM--; // Decrement count on release
switchStatesPM[i] = HIGH; // Update state
}
}
// Check each switch for MS
for (int i = 0; i < 8; i++) { // 8 switches for MS
int currentState = digitalRead(switchPinsMS[i]);
if (currentState == LOW && switchStatesMS[i] == HIGH) {
countMS++;
switchStatesMS[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesMS[i] == LOW) {
countMS--; // Decrement count on release
switchStatesMS[i] = HIGH; // Update state
}
}
// Check each switch for SS
for (int i = 0; i < 8; i++) {
int currentState = digitalRead(switchPinsSS[i]);
if (currentState == LOW && switchStatesSS[i] == HIGH) {
countSS++;
switchStatesSS[i] = LOW; // Update state
delay(200); // Debounce
} else if (currentState == HIGH && switchStatesSS[i] == LOW) {
countSS--; // Decrement count on release
switchStatesSS[i] = HIGH; // Update state
}
}
// Display counts on the LCD
lcd.setCursor(0, 1);
lcd.print("SE: "); lcd.print(countSE);
lcd.setCursor(0, 2);
lcd.print("SP: "); lcd.print(countSP);
lcd.setCursor(7, 1);
lcd.print("SM: "); lcd.print(countSM);
lcd.setCursor(7, 2);
lcd.print("SW: "); lcd.print(countSW);
lcd.setCursor(0, 3);
lcd.print("PM: "); lcd.print(countPM);
lcd.setCursor(7, 3);
lcd.print("MS: "); lcd.print(countMS);
lcd.setCursor(14, 1);
lcd.print("SS: "); lcd.print(countSS);
// Total count
int totalCount = countSE + countSP + countSM + countSW + countPM + countMS + countSS;
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("Total: ");
lcd.setCursor(7, 0);
lcd.print(totalCount < 10 ? "00" : (totalCount < 100 ? "0" : "")); // Leading zeros
lcd.print(totalCount); // Print total count
// Small delay to avoid flickering
delay(500); // Adjust delay as needed
}