-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
38 lines (33 loc) · 886 Bytes
/
Button.cpp
File metadata and controls
38 lines (33 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* Button.cpp
*
* Created on: Sep 10, 2012
* Author: sfeltner
*/
#include "Button.h"
Button::Button(uint8_t pin) {
_lastMillis = 0;
_lastState = 0;
_pin = pin;
pinMode(_pin, INPUT);
}
Button::~Button() {
// TODO Auto-generated destructor stub
}
bool Button::IsPressed(){
int pinread; // MUST BE int to do AnalogRead
if((pinread = analogRead(_pin)) > 900){
if(_lastState == 0){ // is this the first time it was pressed?
_lastState++; // show the button is already pressed
}
else if ((_lastState) && ((millis() - _lastMillis) > BUTTON_BOUNCE)){
_lastMillis = millis(); // capture the millis for the next go around
return true;
}
}
else if (_lastState){
_lastState = 0; // otherwise the button was just released
_lastMillis = 0;
}
return false;
}