Hackster is hosting Impact Spotlights: Industrial Automation. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Industrial Automation. Stream on Thursday!
rakshith bk
Published

ESP32 Touch Slider

Capacitive touch slider using ESP32 touch pins. Increase or decrease value based on swipe. Control volume, brightness, color, and much more.

BeginnerWork in progress30 minutes4,572
ESP32 Touch Slider

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1
aluminium foil
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Touch Slider

C/C++
Use ArduinoIDE with ESP32 Board configured.
#include <Arduino.h>
#include "RGBLED.h"


//prototype declaration
int level_inc_or_dec(int val);

int H =0;
RGBLED L1(25, 33, 32, COMMON_CATHODE);

void setup()
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Swipe Test");
  
  L1.writeHSV(H, 1, 1);
}

int prev_val;
  
void loop()
{
  int val = touchRead(T5);
  int latest_val;
  if ( val < 70 && (val > prev_val+5 || val < prev_val-5) )
  {
    delay(10);
    latest_val = touchRead(T5);
    if ( latest_val < val+5 && latest_val > val-5)
    {
      prev_val = latest_val;
      Serial.println(latest_val);
      int inc_dec = level_inc_or_dec(latest_val);
      Serial.println(inc_dec);
      inc_dec > 0 ? H+=30 : (inc_dec ==0 ? 0:H -=30);
      H < 0 ? H=0:H;
      H > 360 ? H=360: H; 
      L1.writeHSV(H, 1, 1);
    }
  }
  delay(100);
}

int level_inc_or_dec(int val)
{
  int prev_val=val;
  int count = 0;
  while(val < 70)
  {
    delay(100);
    val = touchRead(T5);
    if ( val < 70 && (val > prev_val+5 || val < prev_val-5) )
    {
      val < prev_val ? count-- : count++;
      prev_val = val;
    }
  }
  return count;
}

Credits

rakshith bk
2 projects • 4 followers
Engineer in making; Tinkerer; Hobby photographer;
Contact

Comments

Please log in or sign up to comment.