#include "millisDelay.h"
const int BUTTON = 2;
const int LED = 3;
int BUTTONstate = 0;
//int counter = 0;
int pressed = 0;
//Debouncing code:
int prevBUTTONstate;
int LEDstate;
int prevLEDstate;
unsigned int counter;
unsigned int lastDebounce;
unsigned int debounceDelay;
void setup()
{
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
BUTTONstate = LOW; //Debouncing code
prevBUTTONstate = LOW; //Debouncing code
LEDstate = LOW; //Debouncing code
prevLEDstate = LOW; //Debouncing code
counter = 0; //Debouncing code
lastDebounce = 0; //Debouncing code
debounceDelay = 0; //Debouncing code
Serial.begin(9600);
}
void loop()
{
//Debouncing code
BUTTONstate = digitalRead(BUTTON);
if(BUTTONstate != prevBUTTONstate){
lastDebounce = millis();
// every time the button state changes, get the time of that change
}
if ((millis() - lastDebounce) > debounceDelay) {
LEDstate = BUTTONstate;
/**********************************************************************************************
if the difference between the last time the button changed is greater than the delay period
it is safe to say the button is in the final steady state
so set the LED state to button state.
***********************************************************************************************/
}
// verification code, and a button press counter
if ((prevLEDstate == HIGH) && (LEDstate == LOW)) {
//count rising edges
counter++;
Serial.println(counter);
}
// set current states to previous states
digitalWrite(LED, LEDstate);
prevBUTTONstate = BUTTONstate;
prevLEDstate = LEDstate;
}
//Old code:
/******************************************************
if (BUTTONstate == HIGH)
{
pressed = 1;
digitalWrite(LED, HIGH);
}
if (BUTTONstate == LOW && digitalRead(LED)== HIGH)
{
pressed = 0;
digitalWrite(LED, LOW);
}
if (digitalRead(BUTTON)==1 && BUTTONstate == LOW){
counter++;
Serial.println(counter,DEC);
}
*****************************************************/