Capacitor switch for LED

Hey guys,

Desperately trying to get a reading from an exposed wire (acting as a capacitive sensor) and use this to turn an LED to HIGH.

But my LED just stays on the whole time.

Please let me know what I'm doing wrong.

Thanks!

const int ledPin = 2;      // the number of the LED pin
int threshold = 2;
int touchState = 0;
void setup()
{
    Serial.begin(9600);      // connect to the serial port
  
  
  pinMode(ledPin, OUTPUT);      
  
}

void loop ()
{
  char capval[6];
  char pinval[6] = {1<<PINB0,1<<PINB1,1<<PINB2,1<<PINB3,1<<PINB4,1<<PINB5};
  delay(1000);
  for(char i = 0; i < 6; i++)
  {
    capval[i] = getcap(pinval[i]);
    Serial.print("digital ");
    Serial.print(i+8, DEC);
    Serial.print(": ");
    Serial.println(capval[i], DEC);
  }
  Serial.println("");
  
  touchState = digitalRead(capval[0]);
  
  // check if the capval[1] value is higher than 2
  // if it is, the  is HIGH:
  if (touchState > 2) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
   // print the analog value:
  Serial.println(capval[0], DEC);
}

// returns capacity on one input pin
// pin must be the bitmask for the pin e.g. (1<<PB0)
char getcap(char pin)
{
  char i = 0;
  DDRB &= ~pin;          // input
  PORTB |= pin;          // pullup on
  for(i = 0; i < 16; i++)
    if( (PINB & pin) ) break;
  PORTB &= ~pin;         // low level
  DDRB |= pin;           // discharge
  return i;
}

Addition:

here is how I have set up the wiring.

I cant figure out why the LED is constantly HIGH.

Thanks again.
whattheshock

What value is the Arduino reporting via serial?

It reads 1 when not touching and 4-5 when touching.

Is that what you mean?

so its sensing my touch, which is great, but im having trouble making the difference in that value make the LED turn on and off. the LED is just on the whole time.

touchState = digitalRead(capval[0]);

capval[0] is not a pin number.

  if (touchState > 2) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }

The digitalRead function return 0 or 1. touchState will never be 2 or higher.