Oi, this website is currently on the move to https://turboflip.de - party on there!

Basic touch helpers for ESP32

Here is an example of basic usage for the helper functions.

// ----------------------------------
// -------MAIN LOOP -----------------
// ----------------------------------

// initalize the aTouch objects : aTouch namewahtevet(yourpinnumber);
aTouch touch1(2);
aTouch touch2(4);
aTouch touch3(12);

void setup() {
  
  Serial.begin(9600);

}

void loop() {
 
  // simply read input and do some calc
  touch1.readAndProcessInput();
  touch2.readAndProcessInput();
  touch3.readAndProcessInput();

  // just call the touch object properties for further logic :)
  if(touch2.on_pressed){ Serial.println("Touch2 pressed"); }
  if(touch2.on_released){ Serial.println("Touch2 released");}
  if( touch2.is_holded){ Serial.println("Touch2 hold very long"); }
  
  delay(40);

}

This is the main code for the helpers. This code needs to be put prior to your mainloop code.


// ----------------------------------
// ------- TRXYS TOUCH HELPERS ------
// ----------------------------------
// ( put this code PRIOR to your mainloop code or you will get nice errors :) )

  // simple lerp helper function

float return_lerp(float _s, int _target,int _time){
 
   _s = _s + (( float(_target) - _s)/float(_time));
   return _s;
   
}

class aTouch {
  private:
    bool prev_touch_state = false;
    byte pin;
    int smooth_time = 4;
    int trigger_threshold = 15;
    long ts = 0;
  public:
    int current_val = 0;
    int smoothed_val = 0;
    int diff_val = 0;
    bool is_triggered = false;
    bool on_pressed = false;
    bool on_released = false;
     bool is_holded = false;
    aTouch(byte pin) {
     this->pin = pin;
    }
    

    void readAndProcessInput() {

        // reset interaction states
        on_pressed = false;
        on_released = false;
        
         // directly read out values TWICE = BUGFIX for debouncing 
         current_val = touchRead(pin);
         delayMicroseconds(10);
         current_val = touchRead(pin);

        //calculate smoothed input values 
         smoothed_val = return_lerp(smoothed_val,current_val,smooth_time);

         // calc current differential sum of button
          diff_val =   smoothed_val - current_val;

          // check if there is a noticable difference input values
          if(  diff_val  > trigger_threshold){

              if(prev_touch_state == false){
                is_triggered = true;
                prev_touch_state = is_triggered;
                on_pressed = true;
                ts = millis(); // set timestamp for hold routine
             }
             
          }else if( diff_val < trigger_threshold*-.4){

              if(prev_touch_state == true){
                is_triggered = false;
                 prev_touch_state = is_triggered;
                 on_released = true;
              }
                
          }


          // calculate timed holding function
          
          if( ts + 2500 < millis() && is_triggered){

               
              is_holded = true;
          }else{

              is_holded = false;
           }
      
     }


     void printAllMetadata() {

           // only use this on one touchinput only - it will skrew up else
           Serial.print (on_pressed);
           Serial.print(","); 
           Serial.print (on_released);
           Serial.print(","); 
           Serial.print (is_triggered);
           Serial.print(","); 
           Serial.print (current_val);
           Serial.print(","); 
           Serial.println(smoothed_val);
           

      
      
     }
};