/*
Button Pressing Testing
*/
#include <avr/wdt.h>
#define BUTTON_PIN 7 // Button pin
#define LED_PIN 13 // Led pin
#define BUTTONS_SAMPLES 6000 // Affect the sensitivity of the button
#define BUTTON_PRESSED LOW // The state of the pin when button pressed
unsigned
int
o_prell = 0;
// counter for button pressing detection
boolean button_state =
false
;
unsigned
int
led_state = LOW;
// Led off at the beginning
void
setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// set initial LED state
digitalWrite(LED_PIN, led_state);
wdt_enable(WDTO_1S);
// enable the watchdog timer : 1 second timer
}
void
loop()
{
wdt_reset();
// feed the dog
check_button();
digitalWrite(LED_PIN, led_state);
if
(Serial.available()>0)
{
while
(1) ;
}
}
void
check_button()
{
int
button_input = digitalRead(BUTTON_PIN);
if
((button_input == BUTTON_PRESSED) && (o_prell < BUTTONS_SAMPLES))
{
o_prell++;
// counting for button pressing
}
else
if
((button_input == BUTTON_PRESSED) && (o_prell == BUTTONS_SAMPLES) && !button_state)
{
button_state =
true
;
// button pressed
//led_state = HIGH;
led_state = !led_state;
}
else
if
((button_input != BUTTON_PRESSED) && (o_prell > 0))
{
o_prell--;
// counting for button releasing, or debouncing / immunity
}
else
if
((button_input != BUTTON_PRESSED) && (o_prell == 0) && button_state)
{
button_state =
false
;
//led_state = LOW;
}
}