Capacitive LED fade


Very new to all this but trying my best.

I'm trying to make an LED FADE up when triggered by a capacitive event, then stay up for 30 seconds, then fade out if the capacitive source is no longer present.

I have searched for a while for code where a pushbutton that is pressed momentarily fades up an LED. They all seem to require you to hold the button down in order to get the LED to full.

Here is the code I have borrowed, stolen and hacked together so far>>>>

 * CapacitiveSense Switch Demo by
 * Based on CapitiveSense Library Demo Sketch by Paul Badger
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K – 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin – try different amounts of foil/metal on this pin
#include <CapSense.h>
CapSense   cs_4_2 = CapSense(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

void setup()                    
   pinMode(9, OUTPUT);
void loop()                    
  long total =  cs_4_2.capSense(30);
  if (total > 300)
      analogWrite(9, brightness);    

  brightness = brightness + fadeAmount;
    delay (50);

    digitalWrite(9, LOW);    // set the LED off
  delay(10);    // arbitrary delay

I need help with:

-Fading the LED down after 'total' is below its threshold (capacitive source is no longer present)
-Delaying the LED at FULL, after the fade has reached full

Thanks to all. This forum is a wealth!

Random amounts of white space and indenting do not make for readable code. Use the control-t function in the IDE to properly format your code, and remove the useless blank lines. Then post the code again.

If you are trying to fade the LED up, you need to do that in a loop, after detecting the presence of sufficient output from the capsense library. You don't have a loop. You have one step in the fading of the LED.

To fade down, you need another loop. Once the capsense function returns a value below the threshold, delay for 30 seconds, then loop to fade down.

EDIT: Adding some comments. :slight_smile:

#include <CapSense.h>
CapSense   cs_4_2 = CapSense(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil

const int LEDpin = 9;   //  PWM output to LED
const unsigned long fullDelay = 5000;  // Five seconds minimum at full brightness

// Fade rate settings.
//  Start with 'fadeAmount' = 1 and "fadeDelay" = 1
//  That will give a fade speed of about 1/4 second (256 milliseconds).  
//  If that is too slow, increase 'fadeAmount'
//  If that is too fast, increase 'fadeDelay'
const int fadeAmount = 1;
const int fadeDelay = 8;

int brightness = 0;
unsigned long fullTime;  //  The millis() reading last time the brightness was at 255

void setup()                    
   pinMode(LEDpin, OUTPUT);

void loop()                    
    //  brighten = true if someone is touching the capacitive input
    boolean brighten =  cs_4_2.capSense(30) > 300;

    if (brighten)  // Someone is there!
       //  Fade up the LED but don't go past 255
       brightness = min(brightness+fadeAmount, 255);

       // If we're at full brightness, note the time
       if (brightness == 255)
           fullTime = millis();  // Note the time

    // If nobody is there and it's been a while since we were last at full brightness
    // we can fade the LED down a bit.
    if (!brighten && millis() - fullTime > fullDelay)
        brightness -= min(brightness, fadeAmount);
    //  Update the LED
    analogWrite(LEDpin, brightness);

    // Slow down the fade 

John I cant thank you enough! This is exactly what I needed. I'll post a video of the result this afternoon