Skip to content

Instantly share code, notes, and snippets.

@evanrinehart
Created June 21, 2017 22:41
Show Gist options
  • Save evanrinehart/da1c6cb7509a359c87f1db8b35340acb to your computer and use it in GitHub Desktop.
Save evanrinehart/da1c6cb7509a359c87f1db8b35340acb to your computer and use it in GitHub Desktop.
/* The cord switch is attached to CORD_IN, and we're using internal pullups.
so normally the value reads HIGH. when cord is pulled and connects to ground, reads LOW. */
/* In so many words, we're using the debounce protocol
described at university of utah "debouncing.pdf" section "software debouncers" */
/* return 1 if cord pull is detected, else 0 */
/* If 1 is returned, it will only be returned once (until cord is released and cycled). */
#define DEBOUNCE_TIME 100
int debounceState = HIGH;
int debounceTimer = DEBOUNCE_TIME;
int checkCordIn() {
int cord = digitalRead(CORD_IN);
if (cord == LOW) { // cord might be being pulled
if (debounceState == HIGH) { // we care, we're waiting to detect a real pull
debounceTimer--;
if (debounceTimer == 0) { // it's been down for DEBOUNCE_TIME, release the hounds
debounceState = LOW; // we don't care anymore (until release)
return 1;
}
}
}
else { // cord not fully pulled, reset the detector
debounceState = HIGH;
debounceTimer = DEBOUNCE_TIME;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment