#include "esp_mac.h" // required - exposes esp_mac_type_t values
#include "common.h"
////////////////////////////////////////////////
typedef struct Buttons {
const uint8_t pin = MyBUTTON_2;
const uint8_t debounce = 50;
unsigned long counter = 0;
bool prevState = HIGH;
bool currentState;
} Button;
// create a Button variable type
Button button;
////////////////////////////////////////////////
//=======================================================================
void setup() {
Serial.begin(115200);
delay(250);
DEBUG_F("\r\n------ Booting ESP ------------------------------------\r\n");
DEBUG_F("Arduino IDE : %d.%d.%d \r\n", ARDUINO / 10000, ARDUINO % 10000 / 100,
ARDUINO % 100 / 10 ? ARDUINO % 100 : ARDUINO % 10);
DEBUG_F("Compiler ver. : %s \r\n", __VERSION__); // gcc version
DEBUG_F("On Board : %s \r\n", ARDUINO_BOARD);
DEBUG_F("Sketch name : %s \r\n", SKETCH_NAME.c_str());
DEBUG_F("Sketch ver. : %s \r\n", VERSION);
DEBUG_F("Build Date : %s \r\n", (__DATE__ ", " __TIME__)); // compilation date, time
DEBUG_F("Author : Kernel Panic\n\n");
PRINT_DASHED_LINES;
pinMode(My_BUTTON, INPUT_PULLUP); //
pinMode(MyBUTTON_2, INPUT_PULLUP); //
pinMode(MY_LED, OUTPUT);
rgbLedWrite(MY_LED, 1, 1, 200);
// #if defined(DebugLevel == 0)
// DEBUG_F("\t #if defined DebugLevel = %d\n", DebugLevel);
// #endif
DEBUG_F("✅🆗 All systems initialized successfully!\n");
DEBUG_F(" - Time waiting for Boot : %.2f sec. -\r\n", (millis() / 1000.0));
PRINT_DASHED_LINES;
}
//=======================================================================
void loop() {
CheckButton();
CheckButton2();
}
/* === CheckButton ======================================================= */
void CheckButton() {
static bool button_state = false; // false = released | true = pressed
static uint32_t button_time_stamp = 0; // debouncing control
const uint16_t debouceTime = 200; // button debouncing time (ms)
bool currentStateMy_BUTTON = digitalRead(My_BUTTON);
// Check if the button has been pressed
if (currentStateMy_BUTTON == LOW && !button_state) {
button_time_stamp = millis(); // record the time while the button is pressed.
button_state = true; // pressed.
}
if (button_state && millis() - button_time_stamp > debouceTime) {
button_state = false; // released
DebugLevel = (DebugLevel + 1) % 6;
DEBUG_F("\tDebugLevel = %d\n", DebugLevel);
}
}
//=======================================================================
void CheckButton2() {
static int MyCounter = 0;
// check the button
button.currentState = digitalRead(button.pin);
// has it changed?
if (button.currentState != button.prevState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == LOW) {
// a new press event occured
// record when button went down
button.counter = millis();
}
if (button.currentState == HIGH) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
if ((currentMillis - button.counter >= 100) && !(currentMillis - button.counter >= 900)) {
// short press detected.
MyCounter++;
DEBUG_F("\tShort MyCounter : %d\n", MyCounter);
}
if ((currentMillis - button.counter >= 900)) {
// the long press was detected
MyCounter = 10 + MyCounter;
DEBUG_F("\tLong MyCounter : %d\n", MyCounter);
}
}
// used to detect when state changes
button.prevState = button.currentState;
}
}
/*********************************
end of this page
**********************************/