Created
June 19, 2018 02:21
-
-
Save honnet/3a931645d2673c004b8e4a4c96b237a7 to your computer and use it in GitHub Desktop.
Capacitive Touch Toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ADCTouch.h> | |
// ADCTouch can be installed in the library manager | |
// Procedure explained in https://www.arduino.cc/en/Guide/Libraries#toc3 | |
int ref0; // reference values to remove offset | |
bool toggle = 0; | |
bool old_touch_state = 0; | |
void setup() { | |
Serial.begin(115200*2); | |
ref0 = ADCTouch.read(A0, 500); // reference values | |
} | |
void loop() { | |
int value0 = ADCTouch.read(A0); // no second parameter | |
value0 -= ref0; // remove offset | |
bool new_touch_state = 0; // default value (bool can only be 0 or 1) | |
if (value0 > 40) // threshold to decide if a touch event happened | |
new_touch_state = 1; // 1 == true | |
if (old_touch_state == 0 && new_touch_state == 1) { | |
toggle = !toggle; // '!' is an inverting operator | |
} | |
Serial.println(toggle); //send the boolean: pressed or not pressed | |
old_touch_state = new_touch_state; // backup | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS: Serial.begin(115200*2); is not mandatory but it helps displaying faster. You just need to pick the right baud rate in the serial plotter: 230400 (=115200*2).