Skip to content

Instantly share code, notes, and snippets.

@wybiral
Created October 19, 2018 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wybiral/7a1c0dfcdc5a70624c64056132f9c978 to your computer and use it in GitHub Desktop.
Save wybiral/7a1c0dfcdc5a70624c64056132f9c978 to your computer and use it in GitHub Desktop.
// Touch pin cutoff value
#define CUTOFF 20
// Amount of time held before touch fires event (ms)
#define DURATION 100
// Touch state (0=none, 1=touched+waiting, 2=touched+sent)
int state;
int start;
void setup() {
Serial.begin(115200);
state = 0;
}
void loop() {
const int t = millis();
const int value = touchRead(T0);
const bool touched = (value < CUTOFF);
if (state > 0) {
if (state == 1 && t - start > DURATION) {
state = 2;
Serial.println("touch");
} else if (!touched) {
state = 0;
}
} else {
if (touched) {
state = 1;
start = t;
}
}
delay(25);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment