#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "SdFat.h"
// For the Adafruit screen, these are the pin outlets for Hardware SPI (FAST).
#define TFT_CS 22 //22
#define TFT_DC 21 //21
#define TFT_RST 14 //17
// Define SD card CS pin
#define SD_CS 5 // External SD card reader Chip Select
// Define button pins
#define BUTTON_INC 32 // Button to increase counter
#define BUTTON_DEC 33 // Button to decrease counter
#define BUTTON_RST 34 // Button to toggle screen on and off
// Define button hold time for reset
const unsigned long RESET_HOLD_TIME = 5000; // 5 seconds
// Define SD
SdFat SD;
// Create an instance of the ILI9341 library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Set button counters to 0
int counter = 0;
bool isScreenOn = true;
// Variables to track button states and timing
bool lastButtonStateInc = HIGH;
bool lastButtonStateDec = HIGH;
bool lastButtonStateRst = HIGH;
unsigned long lastDebounceTimeInc = 0;
unsigned long lastDebounceTimeDec = 0;
unsigned long lastDebounceTimeRst = 0;
const unsigned long DEBOUNCE_DELAY = 50;
unsigned long buttonDownTime = 0;
void setup() {
Serial.begin(115200);
Serial.println();
tft.begin();
tft.setRotation(1); // Rotate display 90 degrees
delay(100);
// Initialize the SD card reader
if (!SD.begin(SD_CS, SD_SCK_MHZ(25))) {
Serial.println("SD card initialization failed!");
while (true); // Halt the program if SD card initialization fails
}
Serial.println("SD card initialized successfully!");
// Set initial button states
lastButtonStateInc = digitalRead(BUTTON_INC);
lastButtonStateDec = digitalRead(BUTTON_DEC);
lastButtonStateRst = digitalRead(BUTTON_RST); // Update initial state
}
void loop() {
// Read button states with debounce
bool currentButtonStateInc = digitalRead(BUTTON_INC);
bool currentButtonStateDec = digitalRead(BUTTON_DEC);
bool currentButtonStateRst = digitalRead(BUTTON_RST);
// Check if the increase button is pressed
if (buttonPressed(currentButtonStateInc, &lastButtonStateInc)) {
counter++;
if (counter < 0) {
counter = 0; // Prevent negative values
}
updateDisplay();
delay(100); // Debounce delay
Serial.println(counter);
}
// Check if the decrease button is pressed
if (buttonPressed(currentButtonStateDec, &lastButtonStateDec)) {
counter--;
if (counter < 0) {
counter = 0; // Prevent negative values
}
updateDisplay();
delay(100); // Debounce delay
Serial.println(counter);
}
// Check if the toggle screen button is pressed
if (buttonPressed(currentButtonStateRst, &lastButtonStateRst)) {
isScreenOn = !isScreenOn;
if (!isScreenOn) {
tft.fillScreen(ILI9341_BLACK); // Turn off screen
} else {
updateDisplay();
}
delay(100); // Debounce delay
}
}
bool buttonPressed(bool currentButtonState, bool *lastButtonState) {
if (currentButtonState != *lastButtonState) {
*lastButtonState = currentButtonState;
// If the button is pressed (LOW), return true
if (currentButtonState == LOW) {
buttonDownTime = millis();
return true;
}
}
return false;
}
void updateDisplay() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(50, 100);
tft.print(counter);
}