Skip to content

Instantly share code, notes, and snippets.

@Jolievw
Created September 20, 2017 10:03
Show Gist options
  • Save Jolievw/9dfde2c7049505469d5549e079e08db1 to your computer and use it in GitHub Desktop.
Save Jolievw/9dfde2c7049505469d5549e079e08db1 to your computer and use it in GitHub Desktop.
#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(A4, A2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int threshold;
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
long reading;
for(int i = 0; i < 4; i++) {
reading = reading = cs_4_2.capacitiveSensor(30);
delay(5);
}
reading = reading /4;
if(reading > THRESHOLD) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(reading);
delay(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment