#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <EEPROM.h> // Include the EEPROM library
const char* ssid = "YourSSID";
const char* password = "YourPassword";
#define BTN_push_1 26
#define BTN_push_2 27
#define BTN_push_3 14
#define BTN_push_4 12
#define BTN_NAVIGATE 33
#define BTN_increase_count 32
#define BTN_decrease_count 34
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int counts[4] = {0, 0, 0, 0}; // Array to store counts for each counter (initialized to 0)
int counter = 1; // Start with the first row selected
bool dataFromROM = false; // Flag to check if data was loaded from ROM
const unsigned long longPressDuration = 5000; // 5 seconds for long press
// Variable to track button press start time
unsigned long buttonPressStartTime = 0;
// Variable to track if BTN_NAVIGATE is being long-pressed
bool isLongPress = false;
// Button debounce variables
int buttonStateInc = HIGH;
int lastButtonStateInc = HIGH;
unsigned long lastDebounceTimeInc = 0;
int buttonStateDec = HIGH;
int lastButtonStateDec = HIGH;
unsigned long lastDebounceTimeDec = 0;
int buttonStateNav = HIGH;
int lastButtonStateNav = HIGH;
unsigned long lastDebounceTimeNav = 0;
unsigned long debounceDelay = 50;
void saveCountsToEEPROM() {
int addr = 0; // Start address in EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.put(addr, counts[i]); // Write each count to EEPROM
addr += sizeof(int); // Move to the next address for the next count
}
EEPROM.commit(); // Commit the changes to EEPROM
}
void loadCountsFromEEPROM() {
int addr = 0; // Start address in EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.get(addr, counts[i]); // Read each count from EEPROM
if (counts[i] == -1) { // If uninitialized or corrupted data, set to 0
counts[i] = 0;
}
addr += sizeof(int); // Move to the next address for the next count
}
}
void clearCountsAndEEPROM() {
for (int i = 0; i < 4; i++) {
counts[i] = 0; // Clear counts
}
EEPROM.begin(512); // Initialize EEPROM
for (int i = 0; i < EEPROM.length(); i++) {
EEPROM.write(i, 0); // Erase EEPROM data by writing zeros
}
EEPROM.commit(); // Commit the changes to EEPROM
EEPROM.end(); // Release EEPROM
}
void setup() {
EEPROM.begin(512);
// Load counts from EEPROM on startup
loadCountsFromEEPROM();
// Check if data was loaded from EEPROM
for (int i = 0; i < 4; i++) {
if (counts[i] != 0) {
dataFromROM = true;
break;
}
}
// If no data was loaded from EEPROM, set counts to 0
if (!dataFromROM) {
for (int i = 0; i < 4; i++) {
counts[i] = 0;
}
}
// Release EEPROM
EEPROM.end();
pinMode(BTN_push_1, INPUT_PULLUP);
pinMode(BTN_push_2, INPUT_PULLUP);
pinMode(BTN_push_3, INPUT_PULLUP);
pinMode(BTN_push_4, INPUT_PULLUP);
pinMode(BTN_NAVIGATE, INPUT_PULLUP);
pinMode(BTN_increase_count, INPUT_PULLUP);
pinMode(BTN_decrease_count, INPUT_PULLUP);
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.fillScreen(ILI9341_BLACK);
// Display the initial menu
displayMenu();
}
void loop() {
// ... (Previous loop code remains unchanged)
// Debounce increase count button
int readingInc = digitalRead(BTN_increase_count);
if (readingInc != lastButtonStateInc) {
lastDebounceTimeInc = millis();
}
if ((millis() - lastDebounceTimeInc) > debounceDelay) {
if (readingInc != buttonStateInc) {
buttonStateInc = readingInc;
if (buttonStateInc == LOW) {
counts[counter - 1]++; // Increase count for the selected counter
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
}
}
}
lastButtonStateInc = readingInc;
// Debounce decrease count button
int readingDec = digitalRead(BTN_decrease_count);
if (readingDec != lastButtonStateDec) {
lastDebounceTimeDec = millis();
}
if ((millis() - lastDebounceTimeDec) > debounceDelay) {
if (readingDec != buttonStateDec) {
buttonStateDec = readingDec;
if (buttonStateDec == LOW && counts[counter - 1] > 0) {
counts[counter - 1]--; // Decrease count for the selected counter (if greater than zero)
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
}
}
}
lastButtonStateDec = readingDec;
// Debounce navigate button
int readingNav = digitalRead(BTN_NAVIGATE);
if (readingNav != lastButtonStateNav) {
lastDebounceTimeNav = millis();
}
if ((millis() - lastDebounceTimeNav) > debounceDelay) {
if (readingNav != buttonStateNav) {
buttonStateNav = readingNav;
if (buttonStateNav == LOW) {
// Button is pressed
buttonPressStartTime = millis(); // Record the button press start time
} else {
// Button is released
unsigned long buttonPressDuration = millis() - buttonPressStartTime;
if (buttonPressDuration >= longPressDuration) {
// Long press detected
isLongPress = true;
}
// Reset button press start time
buttonPressStartTime = 0;
}
if (isLongPress) {
clearCountsAndEEPROM(); // Clear counts and values in EEPROM
displayMenu();
isLongPress = false; // Reset long-press flag after clearing
} else {
counter = (counter % 4) + 1; // Increment counter to move to the next row
displayMenu();
}
}
}
lastButtonStateNav = readingNav;
if (digitalRead(BTN_push_1) == LOW) {
counts[0]++; // Increase count for Counter 1
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
delay(250); // Add a small delay to avoid rapid counting on button hold
}
if (digitalRead(BTN_push_2) == LOW) {
counts[1]++; // Increase count for Counter 2
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
delay(250);
}
if (digitalRead(BTN_push_3) == LOW) {
counts[2]++; // Increase count for Counter 3
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
delay(250);
}
if (digitalRead(BTN_push_4) == LOW) {
counts[3]++; // Increase count for Counter 4
EEPROM.begin(512); // Initialize EEPROM
saveCountsToEEPROM();
EEPROM.end(); // Release EEPROM
displayMenu();
delay(250);
}
}
void displayMenu() {
tft.fillScreen(ILI9341_BLACK); // Clear screen
int textSize = 3; // Text size for counters
int lineHeight = textSize * 10; // Line height based on text size
// Calculate total height of menu area
int totalHeight = 4 * lineHeight;
int startY = (tft.height() - totalHeight) / 2; // Start Y position for the menu
for (int i = 1; i <= 4; i++) {
int yPos = startY + ((i - 1) * lineHeight);
tft.setCursor(10, yPos);
if (i == counter) {
tft.setTextColor(ILI9341_BLACK);
tft.fillRect(0, yPos, tft.width(), lineHeight, ILI9341_GREEN); // Highlight selected row
} else {
tft.setTextColor(ILI9341_WHITE);
}
tft.setTextSize(textSize); // Set text size for counters
tft.print("Sensor[");
tft.print(i);
tft.print("]: ");
tft.print(counts[i - 1]); // Display count for each counter
}
int bottomTextSize = 2; // Text size for bottom text
tft.setTextSize(bottomTextSize);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor((tft.width() - 320) / 2, tft.height() - 20); // Position for bottom text
tft.println("Push to select menu");
tft.setTextColor(ILI9341_WHITE);
}