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;
}