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

int LEDPin1 = 38;
int LEDPin2 = 39;
int LEDPin3 = 40;
int LEDPin5 = 41;
int LEDPin4 = 42;
int LEDPin6 = 43;
int LEDPin7 = 44;
int LEDPin8 = 45;
int LEDPin9 = 46;
int LEDPin10 = 47;
int LEDPin11 = 48;
int LEDPin12 = 49;
int LEDPin13 = 50;
int LEDPin14 = 51;
int LEDPin15 = 52;
int LEDPin16 = 53;

void setup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
   pinMode(LEDPin1, OUTPUT);
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(LEDPin4, OUTPUT);
  pinMode(LEDPin5, OUTPUT);
  pinMode(LEDPin6, OUTPUT);
  pinMode(LEDPin7, OUTPUT);
  pinMode(LEDPin8, OUTPUT);
  pinMode(LEDPin9, OUTPUT);
  pinMode(LEDPin10, OUTPUT);
  pinMode(LEDPin11, OUTPUT);
  pinMode(LEDPin12, OUTPUT);
  pinMode(LEDPin13, OUTPUT);
  pinMode(LEDPin14, OUTPUT);
  pinMode(LEDPin15, OUTPUT);
  pinMode(LEDPin16, OUTPUT);
  digitalWrite(LEDPin1, LOW);
  digitalWrite(LEDPin2, LOW);
  digitalWrite(LEDPin3, LOW);
  digitalWrite(LEDPin4, LOW);
  digitalWrite(LEDPin5, LOW);
  digitalWrite(LEDPin6, LOW);
  digitalWrite(LEDPin7, LOW);
  digitalWrite(LEDPin8, LOW);
  digitalWrite(LEDPin9, LOW);
  digitalWrite(LEDPin10, LOW);
  digitalWrite(LEDPin11, LOW);
  digitalWrite(LEDPin12, LOW);
  digitalWrite(LEDPin13, LOW);
  digitalWrite(LEDPin14, LOW);
  digitalWrite(LEDPin15, LOW);
  digitalWrite(LEDPin16, LOW);
}

void loop()                    
{
    long start = millis();
    long rawSensor =  cs_4_2.capacitiveSensor(30);
    
    constrain(rawSensor, 0, 30000);
    int drawingSensor = map(rawSensor, 0, 30000, 0, 950);

   if (drawingSensor >200) {
  digitalWrite(LEDPin1, HIGH);
  delay(10);
  digitalWrite(LEDPin1, LOW);
}
else {
  digitalWrite(LEDPin1, LOW);
}

  if (drawingSensor >345) {
  digitalWrite(LEDPin2, HIGH);
  delay(10);
  digitalWrite(LEDPin2, LOW);
}
else {
  digitalWrite(LEDPin2, LOW);
}

  if (drawingSensor >543) {
  digitalWrite(LEDPin3, HIGH);
  delay(30);
  digitalWrite(LEDPin3, LOW);
}
else {
  digitalWrite(LEDPin3, LOW);
}

  if (drawingSensor >467) {
  digitalWrite(LEDPin4, HIGH);
  delay(15);
  digitalWrite(LEDPin4, LOW);
}
else {
  digitalWrite(LEDPin4, LOW);
}

  if (drawingSensor >793) {
  digitalWrite(LEDPin5, HIGH);
  delay(20);
  digitalWrite(LEDPin5, LOW);
}
else {
  digitalWrite(LEDPin5, LOW);

}

if (drawingSensor >398) {
  digitalWrite(LEDPin6, HIGH);
  delay(10);
  digitalWrite(LEDPin6, LOW);
}
else {
  digitalWrite(LEDPin6, LOW);

}

  if (drawingSensor >612) {
  digitalWrite(LEDPin7, HIGH);
  delay(10);
  digitalWrite(LEDPin7, LOW);
}
else {
  digitalWrite(LEDPin7, LOW);
}

  if (drawingSensor >709) {
  digitalWrite(LEDPin8, HIGH);
  delay(10);
  digitalWrite(LEDPin8, LOW);
}
else {
  digitalWrite(LEDPin8, LOW);
}

  if (drawingSensor >622) {
  digitalWrite(LEDPin9, HIGH);
  delay(10);
  digitalWrite(LEDPin9, LOW);
}
else {
  digitalWrite(LEDPin9, LOW);
}

  if (drawingSensor >22) {
  digitalWrite(LEDPin10, HIGH);
  delay(10);
  digitalWrite(LEDPin10, LOW);
}
else {
  digitalWrite(LEDPin10, LOW);
}

  if (drawingSensor >114) {
  digitalWrite(LEDPin11, HIGH);
  delay(10);
  digitalWrite(LEDPin11, LOW);
}
else {
  digitalWrite(LEDPin11, LOW);
}

  if (drawingSensor >712) {
  digitalWrite(LEDPin12, HIGH);
  delay(10);
  digitalWrite(LEDPin12, LOW);
}
else {
  digitalWrite(LEDPin12, LOW);
}

  if (drawingSensor >313) {
  digitalWrite(LEDPin13, HIGH);
  delay(10);
  digitalWrite(LEDPin13, LOW);
}
else {
  digitalWrite(LEDPin13, LOW);
}

  if (drawingSensor >814) {
  digitalWrite(LEDPin14, HIGH);
  delay(10);
  digitalWrite(LEDPin14, LOW);
}
else {
  digitalWrite(LEDPin14, LOW);
}

  if (drawingSensor >815) {
  digitalWrite(LEDPin15, HIGH);
  delay(10);
  digitalWrite(LEDPin15, LOW);
}
else {
  digitalWrite(LEDPin15, LOW);
}

  if (drawingSensor >800) {
  digitalWrite(LEDPin16, HIGH);
  delay(10);
  digitalWrite(LEDPin16, LOW);
  
}
else {
  digitalWrite(LEDPin16, LOW);
}
  
    
    //Serial.print(millis() - start);        // check on performance in milliseconds
    //Serial.print("\t");                    // tab character for debug windown spacing
    //Serial.print("Raw Sensor Value ");
    //Serial.print("\t");
    //Serial.println(rawSensor);                  // print sensor output 1
    Serial.print("Mapped Sensor Value ");
    Serial.print("\t");
    Serial.println(drawingSensor);
 
    //delay(1000);                             // arbitrary delay to limit data to serial port 
}
