Capacitive Touch Lamp That Ramps Up In Brightness Every Time You Touch It

)As the title states, I'm working on a capacitive touch lamp that will cycle brightness setting as you touch the capacitive sensor (in my case, a copper plate). I am using an Arduino Uno powered by a 12V 5A switching power supply (http://https://www.adafruit.com/products/352). The LED strip that I am using is Adafruit's 1M Cool White LED Weatherproof Flexi-Strip 60 LED (https://www.adafruit.com/products/887) along with a N-channel power MOSFET - 30V/ 60A (N-channel power MOSFET [30V / 60A] : ID 355 : $2.25 : Adafruit Industries, Unique & fun DIY electronics and kits) to dim the LEDs.
I am having trouble with the code since I am an architecture student with no background/experience with code so bare with me, I'll try to add a lot of detail so people can follow along. I have been closely following another forum by timbit1985 where he asked a similar question and I am trying to alter his code to work for my lamp. I used the wiring diagram from Dansl's Github but changed it for a Mono-color LED. My problem is when I use the code below, the LEDs are constantly cycling brightness levels without me even touching the copper plate but when I do, the Serial Monitor says it detects my touch. I'm pretty sure this has something to do with the code (maybe the touch threshold?) Here is the code I am using from timbit that I have slightly altered to use with the Arduino Uno pins that I am using:

int LEDPin = 11;       //PWM Output pin for LED 
int capSensePin = 12;   //Pin to attach to capacitive sensor
int mode = 0;         //Determines LED brightness. 0 is off. Varies between 0 and 255.
int touchThreshold = 254; //Minimum capacitive touch value  in order to trigger next  mode

void setup(){
 Serial.begin(9600);
 pinMode(LEDPin, OUTPUT);  //Set LEDPin to output mode
}

void loop(){

 if (readCapacitivePin(capSensePin) > touchThreshold)   //If the value of capSensePin exceeds touchThreshold then do...
 {
 delay(250);                            //Button Debounce. How would I remove this break using millis()??
 mode++; //increase value of mode by 1

//This next section outlines the different LED brightness levels
   if (mode == 0) analogWrite(LEDPin, 0);
 if (mode == 1) analogWrite(LEDPin, 100);
 if (mode == 2) analogWrite(LEDPin, 150);
 if (mode == 3) analogWrite(LEDPin, 255);
 if (mode > 3) {mode = 0;
analogWrite(LEDPin,0);
}
 //If the value for mode is equal to 4 then set value of mode to 0. 
 Serial.print("The current mode is..."); //Serial monitor to bebug mode increases
 Serial.println(mode);  //print value of mode to seial monitor

 }

 // THIS POINT ONWARD I DIDNT WRITE.
 // Every 500 ms, print the value of the capacitive sensor
 if ( (millis() % 500) == 0){
   Serial.print("Capacitive Sensor on Pin 12 reads: ");
   Serial.println(readCapacitivePin(capSensePin));
 }
}

// readCapacitivePin
//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
 // This is how you declare a variable which
 //  will hold the PORT, PIN, and DDR registers
 //  on an AVR
 volatile uint8_t* port;
 volatile uint8_t* ddr;
 volatile uint8_t* pin;
 // Here we translate the input pin number from
 //  Arduino pin number to the AVR PORT, PIN, DDR,
 //  and which bit of those registers we care about.
 byte bitmask;
 if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
   port = &PORTD;
   ddr = &DDRD;
   bitmask = 1 << pinToMeasure;
   pin = &PIND;
 }
 if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
   port = &PORTB;
   ddr = &DDRB;
   bitmask = 1 << (pinToMeasure - 8);
   pin = &PINB;
 }
 if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
   port = &PORTC;
   ddr = &DDRC;
   bitmask = 1 << (pinToMeasure - 13);
   pin = &PINC;
 }
 // Discharge the pin first by setting it low and output
 *port &= ~(bitmask);
 *ddr  |= bitmask;
 delay(1);
 // Make the pin an input WITHOUT the internal pull-up on
 *ddr &= ~(bitmask);
 // Now see how long the pin to get pulled up
 int cycles = 16000;
 for(int i = 0; i < cycles; i++){
   if (*pin & bitmask){
     cycles = i;
     break;
   }
 }
 // Discharge the pin again by setting it low and output
 //  It's important to leave the pins low if you want to 
 //  be able to touch more than 1 sensor at a time - if
 //  the sensor is left pulled high, when you touch
 //  two sensors, your body will transfer the charge between
 //  sensors.
 *port &= ~(bitmask);
 *ddr  |= bitmask;
 
 return cycles;
}

That capacitive sensing is just awful. It's only going to work on an UNO and it's so sensitive to subtle timing errors. If you can find a better library, then I suggest you use it. However, since you say you are using an UNO and it seems to be detecting your touch so far, leave that part alone.

    delay(250);                            //Button Debounce. How would I remove this break using millis()??

There's probably a zillion ways of doing debouncing. For this one, you probably want to be sure that the user pressed and held the button for a tenth of a second or more. The code as posted will detect even the slightest glitch and then plug its ears and eyes for 1/4 second (250milliseconds) making it impossible to determine if this was a short-term glitch or a real press.

First, define a constant near the top of the program code which will be the debounce interval. Putting it at the top makes it easy to change and easy to use in many places in the program.

const unsigned long int DEBOUNCE_INTERVAL = 100; //milliseconds, sensor must be touched for at least this long

Then we need to add the main part of the debounce code, in the place where you had that delay(250). To make it easier for you, there's also a small part that goes after the main if(), so I'll just copy your code and put it all here. You can paste this in instead of void loop(){...}.

void loop() {
  static boolean WasPressed = false;
  static boolean ActionCarriedOut = false;
  static unsigned long TimePressed = 0;
  
  if (readCapacitivePin(capSensePin) > touchThreshold)   //If the value of capSensePin exceeds touchThreshold then do...
  {
    if(!WasPressed) {
      //it wasn't pressed but now it is, start the debounce timer
      WasNotPressed = false;
      TimePressed = millis();
    } else {
      //was previosuly pressed - was it pressed for long enough?
      //But it may be still held down from the last time, so we
      //don't do the action again
      if(millis() - TimePressed > DEBOUNCE_INTERVAL && !ActionCarriedOut) {
        ActionCarriedOut = true; //need to record that we did it, otherwise we would repeat for a long press
        
        mode++; //increase value of mode by 1
        if (mode > 3) mode = 0;
    
        //This next section outlines the different LED brightness levels
        if (mode == 0) analogWrite(LEDPin, 0);
        if (mode == 1) analogWrite(LEDPin, 100);
        if (mode == 2) analogWrite(LEDPin, 150);
        if (mode == 3) analogWrite(LEDPin, 255);

        //If the value for mode is equal to 4 then set value of mode to 0.
        Serial.print("The current mode is..."); //Serial monitor to bebug mode increases
        Serial.println(mode);  //print value of mode to seial monitor
        
      }
    }

  } else {
    //sensor under threshold - record that it wasn't pressed
    WasPressed = false;
    //also record that the finger was lifted off the sensor, so we know the next press should be actioned 
    ActionCarriedout = false;
  }

  // THIS POINT ONWARD I DIDNT WRITE.
  // Every 500 ms, print the value of the capacitive sensor
  if ( (millis() % 500) == 0) {
    Serial.print("Capacitive Sensor on Pin 12 reads: ");
    Serial.println(readCapacitivePin(capSensePin));
  }
}

This debounce requires you to lift your finger off the sensor to make another increment. Holding it down will only increment it once. It's not possible to tell if this is what you want, from your specification.

int LEDPin = 11;       //PWM Output pin for LED 
int capSensePin = 12;   //Pin to attach to capacitive sensor
int mode = 0;         //Determines LED brightness. 0 is off. Varies between 0 and 255.
int touchThreshold = 254; //Minimum capacitive touch value  in order to trigger next  mode
const unsigned long int DEBOUNCE_INTERVAL = 100; //milliseconds, sensor must be touched for at least this long


void setup(){
 Serial.begin(9600);
 pinMode(LEDPin, OUTPUT);  //Set LEDPin to output mode
}

void loop() {
  static boolean WasPressed = false;
  static boolean ActionCarriedOut = false;
  static unsigned long TimePressed = 0;
  
  if (readCapacitivePin(capSensePin) > touchThreshold)   //If the value of capSensePin exceeds touchThreshold then do...
  {
    if(!WasPressed) {
      //it wasn't pressed but now it is, start the debounce timer
      WasNotPressed = false;
      TimePressed = millis();
    } else {
      //was previosuly pressed - was it pressed for long enough?
      //But it may be still held down from the last time, so we
      //don't do the action again
      if(millis() - TimePressed > DEBOUNCE_INTERVAL && !ActionCarriedOut) {
        ActionCarriedOut = true; //need to record that we did it, otherwise we would repeat for a long press
        
        mode++; //increase value of mode by 1
        if (mode > 3) mode = 0;
    
        //This next section outlines the different LED brightness levels
        if (mode == 0) analogWrite(LEDPin, 0);
        if (mode == 1) analogWrite(LEDPin, 100);
        if (mode == 2) analogWrite(LEDPin, 150);
        if (mode == 3) analogWrite(LEDPin, 255);

        //If the value for mode is equal to 4 then set value of mode to 0.
        Serial.print("The current mode is..."); //Serial monitor to bebug mode increases
        Serial.println(mode);  //print value of mode to seial monitor
        
      }
    }

  } else {
    //sensor under threshold - record that it wasn't pressed
    WasPressed = false;
    //also record that the finger was lifted off the sensor, so we know the next press should be actioned 
    ActionCarriedout = false;
  }

  // THIS POINT ONWARD I DIDNT WRITE.
  // Every 500 ms, print the value of the capacitive sensor
  if ( (millis() % 500) == 0) {
    Serial.print("Capacitive Sensor on Pin 12 reads: ");
    Serial.println(readCapacitivePin(capSensePin));
  }
}

Thanks so much for the help Morgan, really appreciate it. Sorry, I should have mentioned the specifics of the capacitive touch. I want the LED brightness levels to cycle when you touch the lamp so no need to hold it down to change cycles. When I uploaded the code to the compiler I get an error on the last line of code <Serial.println(readCapacitivePin(capSensePin));> saying that 'readCapacitivePin' was not declared in this scope. Do I need to add something to the void setup?

It looks like you accidentally deleted the readCapacitivePin() function which was placed after loop().

int LEDPin = 11;       //PWM Output pin for LED 
int capSensePin = 12;   //Pin to attach to capacitive sensor
int mode = 0;         //Determines LED brightness. 0 is off. Varies between 0 and 255.
int touchThreshold = 254; //Minimum capacitive touch value  in order to trigger next  mode
const unsigned long int DEBOUNCE_INTERVAL = 100; //milliseconds, sensor must be touched for at least this long


void setup(){
 Serial.begin(9600);
 pinMode(LEDPin, OUTPUT);  //Set LEDPin to output mode
}

void loop() {
  static boolean WasPressed = false;
  static boolean ActionCarriedOut = false;
  static unsigned long TimePressed = 0;
  
  if (readCapacitivePin(capSensePin) > touchThreshold)   //If the value of capSensePin exceeds touchThreshold then do...
  {
    if(!WasPressed) {
      //it wasn't pressed but now it is, start the debounce timer
      WasNotPressed = false;
      TimePressed = millis();
    } else {
      //was previosuly pressed - was it pressed for long enough?
      //But it may be still held down from the last time, so we
      //don't do the action again
      if(millis() - TimePressed > DEBOUNCE_INTERVAL && !ActionCarriedOut) {
        ActionCarriedOut = true; //need to record that we did it, otherwise we would repeat for a long press
        
        mode++; //increase value of mode by 1
        if (mode > 3) mode = 0;
    
        //This next section outlines the different LED brightness levels
        if (mode == 0) analogWrite(LEDPin, 0);
        if (mode == 1) analogWrite(LEDPin, 100);
        if (mode == 2) analogWrite(LEDPin, 150);
        if (mode == 3) analogWrite(LEDPin, 255);

        //If the value for mode is equal to 4 then set value of mode to 0.
        Serial.print("The current mode is..."); //Serial monitor to bebug mode increases
        Serial.println(mode);  //print value of mode to seial monitor
        
      }
    }

  } else {
    //sensor under threshold - record that it wasn't pressed
    WasPressed = false;
    //also record that the finger was lifted off the sensor, so we know the next press should be actioned 
    ActionCarriedout = false;
  }

  // THIS POINT ONWARD I DIDNT WRITE.
  // Every 500 ms, print the value of the capacitive sensor
  if ( (millis() % 500) == 0) {
    Serial.print("Capacitive Sensor on Pin 12 reads: ");
    Serial.println(readCapacitivePin(capSensePin));
  }
}

// readCapacitivePin
//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
 // This is how you declare a variable which
 //  will hold the PORT, PIN, and DDR registers
 //  on an AVR
 volatile uint8_t* port;
 volatile uint8_t* ddr;
 volatile uint8_t* pin;
 // Here we translate the input pin number from
 //  Arduino pin number to the AVR PORT, PIN, DDR,
 //  and which bit of those registers we care about.
 byte bitmask;
 if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
   port = &PORTD;
   ddr = &DDRD;
   bitmask = 1 << pinToMeasure;
   pin = &PIND;
 }
 if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
   port = &PORTB;
   ddr = &DDRB;
   bitmask = 1 << (pinToMeasure - 8);
   pin = &PINB;
 }
 if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
   port = &PORTC;
   ddr = &DDRC;
   bitmask = 1 << (pinToMeasure - 13);
   pin = &PINC;
 }
 // Discharge the pin first by setting it low and output
 *port &= ~(bitmask);
 *ddr  |= bitmask;
 delay(1);
 // Make the pin an input WITHOUT the internal pull-up on
 *ddr &= ~(bitmask);
 // Now see how long the pin to get pulled up
 int cycles = 16000;
 for(int i = 0; i < cycles; i++){
   if (*pin & bitmask){
     cycles = i;
     break;
   }
 }
 // Discharge the pin again by setting it low and output
 //  It's important to leave the pins low if you want to 
 //  be able to touch more than 1 sensor at a time - if
 //  the sensor is left pulled high, when you touch
 //  two sensors, your body will transfer the charge between
 //  sensors.
 *port &= ~(bitmask);
 *ddr  |= bitmask;
 
 return cycles;
}

Got it, thanks again. Now it's saying 'WasNotPressed' was not declared in this scope in the <WasNotPressed = false;> line under the void loop.

Oops, sorry. I changed my "not" halfway through writing that. Find the WasNotPressed and change it to WasPressed. And change false to true.