Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active September 19, 2024 07:19
Show Gist options
  • Save todbot/27c34c55d36002c601b2c28ae8f1b8a4 to your computer and use it in GitHub Desktop.
Save todbot/27c34c55d36002c601b2c28ae8f1b8a4 to your computer and use it in GitHub Desktop.
// fakeycapsense_demo.ino -- implement a fakey capsense touch sensor using same technique as in CircuitPython
// 2 Aug 2022 - @todbot / Tod Kurt
///////////////
class FakeyTouch
{
public:
FakeyTouch( int apin, int athreshold = 200 ) {
pin = apin;
thold = athreshold;
}
void begin() {
baseline = readTouch();
}
int readTouch() {
pinMode(pin, OUTPUT);
digitalWrite(pin,HIGH);
pinMode(pin,INPUT);
int i = 0;
while( digitalRead(pin) ) { i++; }
return i;
}
bool isTouched() {
return (readTouch() > baseline + thold);
}
int baseline;
int thold;
int pin;
};
///////////////////
const int touchpin_F = 16; // GP16
FakeyTouch touchF = FakeyTouch( touchpin_F );
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
Serial.println("hello world\n");
}
void loop() {
if( touchF.isTouched() ) {
Serial.print("TOUCH!\n");
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment