// by NMDW COTMD/
int input_pin = 5; // input pin number
int switch_bounce_time = 10; // adjust bounce time depend on the switch (mechanical vibration)
unsigned long time1 =0;
unsigned long time2 =0;
unsigned long bouncechecktime = 0;
bool input_Status = false;
bool Prvious_input_Status = false;
bool debounce_Status = false;
bool prv_debounce_Status = false;
bool debounce_status_changed = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(input_pin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
input_Status = digitalRead(input_pin);
if( not Prvious_input_Status && input_Status) // Previous low and current high i.e. rising edge
{
//Serial.println("Rising Edge");
if(millis() - bouncechecktime > switch_bounce_time)
{
debounce_Status = input_Status;
bouncechecktime = millis();
}
}
if(Prvious_input_Status && not input_Status) // Previous low and current high i.e. rising edge
{
//Serial.println("Falling Edge");
if(millis() - bouncechecktime > switch_bounce_time)
{
debounce_Status = input_Status;
bouncechecktime = millis();
}
}
debounce_status_changed = (prv_debounce_Status != debounce_Status);
if(debounce_status_changed)
{
//Serial.print("Debounced Status : ");
//Serial.println(debounce_Status);
if (debounce_Status)
{
Serial.println("Debounce Rising Edge");
time1 = millis();
Serial.print("Debounced off time : ");
Serial.println(millis() - time2);
}
else
{
Serial.println("Debounce falling Edge");
time2 = millis();
Serial.print("Debounced on time : ");
Serial.println(millis() - time1);
}
}
prv_debounce_Status = debounce_Status;
Prvious_input_Status = input_Status;
}