00001 #ifndef _BUTTON_H_ 00002 #define _BUTTON_H_ 00003 00004 #include <WProgram.h> 00005 00006 class Button{ 00007 public: 00008 00009 Button(int pin, bool invert =false , int debounce_delay = 50){ 00010 pin_ = pin; 00011 debounce_delay= debounce_delay; 00012 prior_value = digitalRead(pin_); 00013 last_debounce_time = millis(); 00014 invert_ = invert; 00015 changed_= false; 00016 } 00017 00018 bool pressed(){ 00019 return invert_ && digitalRead(pin_); 00020 } 00021 00022 bool changed(){ 00023 bool reading = pressed(); 00024 if (prior_value!= reading){ 00025 prior_value = reading; 00026 last_debounce_time = millis(); 00027 changed_ = true; 00028 } 00029 //if the button value has not changed for the debounce delay, we know its stable 00030 if ( changed_ && ( (millis() - last_debounce_time) > debouce_delay_ ) ) { 00031 00032 changed_ = false; 00033 return true; 00034 } 00035 return false; 00036 } 00037 00038 private: 00039 int pin_; 00040 bool prior_value; 00041 unsigned long last_debounce_time; 00042 int debouce_delay_; 00043 bool invert_; 00044 bool changed_; 00045 00046 }; 00047 00048 #endif