~fuzzgun/buttonui-ros-pkg/trunk

« back to all changes in this revision

Viewing changes to arduino/buttonui/buttonui.ino

  • Committer: Bob Mottram
  • Date: 2012-07-24 22:29:31 UTC
  • Revision ID: fuzzgun@gmail.com-20120724222931-uratfyqtby5z3b55
DebouncedĀ E-stop

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 */
12
12
 
13
13
//#define USE_PRESSURE_SENSORS
14
 
//#define USE_ESTOP
 
14
#define USE_ESTOP
15
15
 
16
16
const int no_of_buttons = 5;
17
17
const int buttonPin[] = {8,9,10,11,12};  // the pin that the pushbutton is attached to
125
125
 
126
126
const int EstopPin = 7;   // pin used for emergency stop, normally held high
127
127
int EstopState = HIGH;
 
128
int prevEstopState = HIGH;
128
129
int lastEstopState = HIGH;
 
130
int lastEstopTime=0;
 
131
const int debounceDelay = 50;
129
132
 
130
133
void setup_Estop() {
131
134
  pinMode(EstopPin,INPUT);
 
135
  prevEstopState = digitalRead(EstopPin);
 
136
  lastEstopState = prevEstopState;
 
137
  lastEstopTime = millis();
132
138
}
133
139
 
134
140
void update_Estop() {
135
 
  EstopState = digitalRead(EstopPin);
136
 
  if (EstopState != lastEstopState) {
137
 
    if (EstopState == LOW) {
 
141
  int reading = digitalRead(EstopPin);
 
142
  if (reading != lastEstopState) {
 
143
    // reset the debouncing timer
 
144
    lastEstopTime = millis();
 
145
  }
 
146
 
 
147
  if ((millis() - lastEstopTime) > debounceDelay) {
 
148
    EstopState = reading;
 
149
  }
 
150
  
 
151
  if (EstopState!=prevEstopState) {
 
152
    if (reading == LOW) {
138
153
      // Emergency stop active
139
154
      Serial.println('E');
140
155
    }
143
158
      Serial.println('e');
144
159
      digitalWrite(ledPin, LOW);
145
160
    }
146
 
    lastEstopState = EstopState;
 
161
    prevEstopState = EstopState;
147
162
  }
148
 
  // if E-stop is active then turn the LED on
 
163
 
149
164
  if (EstopState == LOW) {
150
165
    digitalWrite(ledPin, HIGH);
151
166
  }
 
167
  
 
168
  lastEstopState = reading;  
152
169
}
153
170
 
154
171
#endif