digamesystems / esp32_capacitive_touch_test

Experimenting with the Capacitive Touch Inputs on the ESP32
MIT License
1 stars 0 forks source link

How to use this data? #1

Open Gomezzzzz opened 3 days ago

Gomezzzzz commented 3 days ago

Hello! Can you add an example not only for experiments, but also for signaling touch based on this data (raw - baseline etc...)? Like “Touch registered!!!” :)

digamesystems commented 3 days ago

Hi, @Gomezzzzz

It would be helpful to know some more details about what you are trying to do.

It sounds like you are looking for some kind of detector for 'touch' and 'release' events.

The way I've structured the example starts off in the setup routine by establishing a baseline which is updated as the loop runs. A simple approach might be to compare the smoothed value to the baseline and see if it crosses a threshold. I'm typing this on the fly, but you might consider something like:

//Somewhere at the top of the file define a couple of state variables...
const int TOUCHED = 1;
const int RELEASED = 0;
int old_state = RELEASED;
int state = RELEASED;

...

void loop()
{
... 

// inside of your loop function...
if ((smoothed - baseline) < threshold) {
    state = TOUCHED;
}
else {
    state = RELEASED;
}

// see if the state has changed since the last time around the loop.
if (old_state != state) {  // There has been a change
    // do your Touch Registered! logic
    if (old_state == RELEASED) { // RELEASED -> TOUCHED
        // Touch Registered
    } else { // TOUCHED -> RELEASED
        // Release Registered
    }
} 

old_state = state; //set the old_state for comparison the next time around the loop

}

The correct value for the threshold might require some experimentation based on your setup.

I hope this helps. Regards, John