Fade an LED with a capacitive sensor

Hello everybody,
I have a problem with a current project: I’m trying to fade an LED touching a Capacitive sensor (a metal foil, in this case) Multiple times, Touching once it should bright a bit, the second it should be brighter, the third as well and at the 4th times it should shut down.
The problem is in the code for sure, because I’ve used an easier code and worked perfectly.
Can some one help me please?

#include <CapacitiveSensor.h>

 
/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * 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
 * Modified by Becky Stern 2013 to change the color of one RGB Neo Pixel based on touch input
 */
 
 
CapacitiveSensor   capSensor = CapacitiveSensor(9,10);        

int threshold = 900;
int threshold2 = 850;
int threshold3 = 800;
int threshold4 = 750;

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

const int ledPin= 12; 

unsigned long previousTime = 0; 


void setup()  {

  Serial.begin(9600);    

    pinMode(ledPin, OUTPUT);                  
}
 
void loop()                    
{
    long total1 =  capSensor.capacitiveSensor(30);

    unsigned long currentTime = millis();
 

    Serial.println(total1);                 

analogWrite(ledPin, brightness);

  if (total1 > threshold){
      digitalWrite(ledPin, 50);
    }
  if (total1 > threshold2 && ledPin == 50 ){
    (previousTime || currentTime);
      digitalWrite(ledPin, 200);
    }

    if (ledPin == 200  && total1 > threshold3){
      digitalWrite(ledPin, 255);
    }

     if (ledPin == 255  && total1 > threshold4){
      digitalWrite(ledPin, 0);
    }
 
    delay(10);                             // arbitrary delay to limit data to serial port 
}

What does that 'brighness' variable do? Why are you both digital writing and analog writing to the pin? Why are you comparing the ledPin to 50, 200 and 255 when it's a constant equal to 12 and it will never be equal to 200?

You are confusing the pin number (12) with the current value of the pin (0,50,200,255). If I was in a zen mood, I'd get you to ask me for five bucks and then offer you an empty wallet.

Try adding this to your sketch, and rewriting the main loop so that it does not use the brightness variable or the ledPin at all - it only uses these two functions.

void setBrightness(int b) {
  brightness = b;
  analogWrite(ledPin, b);
}

int getBrighness() {
  return brightnness;
}