How would I go about creating a toggle with a Capacitive Touch sensor for a lamp

Hello I’m working on a project where I would like to incorporate a capacitive touch sensor to toggle between brightness(low,medium,high,off). I’m not sure how to start. Any recommendations?

Thank You
Tameim Amon

Have you got some code for the cap touch sensor to start with?
With that the “toggle” would be simple

enum BRIGHTNESSLEVEL 
{ brOFF = 0
, brLOW
, brMED
, brHIGH
};
BRIGHTNESSLEVEL br = brOFF;

...
BRIGHTNESSLEVEL levelUp() {
  br++;
  if (br > brHIGH) br = brOFF;
  return br;
}

Hi Tameim,
If I needed to do something like that, I would get some inspiration here:

and why not, here as well:

Cheers
Gustavo.

scroll down to the section WORKING WITH CAPACITIVE TOUCH

void loop()
{
  if (showRainbow)
  {
    rainbow(20);
  }
  else
  {
    for (int i = 0; i < CRICKIT_NUM_TOUCH; i++) 
    { // check each touch input 
      uint16_t val = crickit.touchRead(i); // read the touch input 
      
      if (val > CAPTOUCH_THRESH)
      { // if the value read is > the threshold
        switch (i)
        {
        case 0:
          setAllPixels(255, 0, 0); //RED
          break;
        case 1:
          setAllPixels(0, 255, 0); //GREEN
          break;
        case 2:
          setAllPixels(0, 0, 255); //BLUE
          break;
        case 3:
          setAllPixels(122, 123, 123); //WHITE
          break;
        }
      }
    }
    delay(100);
  }
}
1 Like