I am using the "CapacitiveSensor" library to make a capacitive push on push off switch.
I have managed to make it act as a momentary on off switch for an LED for the sake of testing but I am having trouble where it comes to toggling between states. I have spent some time looking at the "StateChangeDetection" example and trying to figure out how to blend the two sketches together but I am not using states like 1's and 0's to toggle between but a sensor with a set threshold.
In the sketch below I already have it working for momentary push on/off, I just need help configuring it like the "StateChangeDetection" example.
Please help me I am lost!
Sketch:
#include <CapacitiveSensor.h>
const int ledPin = 9;
const int threshold = 100;
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
void setup(){
pinMode(ledPin, OUTPUT);
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
}
void loop(){
long start = millis();
long sensor_value = cs_4_2.capacitiveSensor(30);
if (sensor_value > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
}