// STM32 Nucleo-C031C6 SPI LCD Example
// Simulation: https://wokwi.com/projects/365549388158011393

// #include "SPI.h"
// #include "Adafruit_GFX.h"
// #include "Adafruit_ILI9341.h"

// #define TFT_DC 2
// #define TFT_CS 3
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// #include <Keypad.h>

// const uint8_t ROWS = 4;
// const uint8_t COLS = 4;
// char keys[ROWS][COLS] = {
//   { '1', '2', '3', 'A' },
//   { '4', '5', '6', 'B' },
//   { '7', '8', '9', 'C' },
//   { '*', '0', '#', 'D' }
// };

// uint8_t colPins[COLS] = { 9, 10, 14, 15 }; // Pins connected to C1, C2, C3, C4
// uint8_t rowPins[ROWS] = { 5, 6, 7, 8 }; // Pins connected to R1, R2, R3, R4

// Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// void setup() {
//   Serial.begin(115200);
//   tft.begin();

//   tft.setTextColor(ILI9341_RED);
//   tft.setTextSize(3);
// }

// void loop() {
//   char key = keypad.getKey();

//   if (key != NO_KEY) {
//     Serial.println(key);
//     tft.println(key);
//   }
// }


const int buttonPin = 4;
const unsigned long debounceDelay = 50;
const unsigned long pressWindow = 3000;

int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;

// Array to store timestamps of the last 3 presses
unsigned long pressTimes[3] = {0, 0, 0};

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // Use internal pull-up resistor
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(buttonPin);

  // Debounce handling
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // Only trigger on button press (falling edge since we're using INPUT_PULLUP)
    if (reading == LOW && lastButtonState == HIGH) {
      handleButtonPress();
    }
  }

  lastButtonState = reading;
}

void handleButtonPress() {
  unsigned long now = millis();
  
  // Shift previous press times
  pressTimes[0] = pressTimes[1];
  pressTimes[1] = pressTimes[2];
  pressTimes[2] = now;

  Serial.println("Button Pressed");

  // Check if 3 presses occurred within the pressWindow
  if (pressTimes[0] != 0 && (pressTimes[2] - pressTimes[0] <= pressWindow)) {
    clearDisplay();
    
    // Reset press times after trigger
    pressTimes[0] = pressTimes[1] = pressTimes[2] = 0;
  }
}

void clearDisplay() {
  Serial.println(">>> Display Cleared! <<<");
  // Put your actual display-clearing code here
}
Loading
st-nucleo-c031c6