#define button_pin 13
int nilai = 0;
//millis
unsigned long prevT = 0;
unsigned long hold_time = 3000;
unsigned long press_time = 1000;
//Flagging push button
int button = LOW;
int stateButton = LOW;
int stateButtonLast = LOW;
bool button_hold = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!\n");
pinMode(button_pin, INPUT);
}
void loop() {
int button = digitalRead(button_pin);
unsigned long currT = millis();
if(button != stateButtonLast){
prevT = millis();
}
if(currT - prevT > hold_time){
if(button != stateButton){
stateButton = button;
if(stateButton == HIGH){
Serial.println("HOLD PRESSED");
}
}
}
else if(currT - prevT <= press_time){
if(button != stateButton){
stateButton = button;
if(stateButton == HIGH){
Serial.println("PRESS");
}
}
}
stateButtonLast = button;
delay(10);
}