#include // Fun with capacitive sensing and some machine code - for the Arduino (or Wiring Boards). // Note that the machine code is based on Arduino Board and will probably require some changes for Wiring Board // This works with a high value (1-10M) resistor between an output pin and an input pin. // When the output pin changes it changes the state of the input pin in a time constant determined by R * C // where R is the resistor and C is the capacitance of the pin plus any capacitance present at the sensor. // It is possible when using this setup to see some variation in capacitance when one's hand is 3 to 4 inches from the sensors // Try experimenting with larger sensors. Lower values of R will probably yield higher reliability. // Use 1 M resistor (or less maybe) for absolute touch to activate. // With a 10 M resistor the sensor will start to respond 1-2 inches away // Setup // Connect a 10M resistor between pins 8 and 9 on the Arduino Board // Connect a small piece of alluminum or copper foil to a short wire and also connect it to pin 9 // When using this in an installation or device it's going to be important to use shielded cable if the wire between the sensor is // more than a few inches long, or it runs by anything that is not supposed to be sensed. // Calibration is also probably going to be an issue. // Instead of "hard wiring" threshold values - store the "non touched" values in a variable on startup - and then compare. // If your sensed object is many feet from the Arduino Board you're probably going to be better off using the Quantum cap sensors. // Machine code and Port stuff from a forum post by ARP http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169088394/0#0 int i; unsigned int x, y; float accum, fout, fval = 0.001; // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter long initialThreshold = 0; long threshold = 0; int value; const int potPin = A0; void setup() { Serial.begin(9600); DDRB = 0b00100101; PORTB = 0b00000000; // Threshold calibration for (short i = 0; i < 10; i++) { long max; y = 0; x = 0; for (short i = 0; i < 4; i++) { // LOW-to-HIGH transition PORTB |= 0b00000001; // Pin 8 HIGH while ((PINB & 0b00000010) != 0b00000010 ) { // loop while pin 9 is LOW x++; } delay(1); // HIGH-to-LOW transition PORTB &= 0b11111110; // Pin 8 LOW while((PINB & 0b00000010) != 0b00000000 ) { // loop while pin 10 is HIGH y++; } delay(1); } max = (x > y ? x : y); threshold = (threshold > max ? threshold : max); } Serial.print("Threshold: "); Serial.println(threshold); Serial.print('\n'); } void loop() { y = 0; x = 0; value = analogRead(potPin); Serial.println("\nValue: "); Serial.println(value); threshold = initialThreshold - ( 2 * value); Serial.println("\nNew Threshold: "); Serial.print(threshold); for (short i = 0; i < 4; i++) { // LOW-to-HIGH transition PORTB |= 0b00000001; // Pin 8 HIGH while ((PINB & 0b00000010) != 0b00000010 ) { // loop while pin 9 is LOW x++; } delay(1); // HIGH-to-LOW transition PORTB &= 0b11111110; // Pin 8 LOW while((PINB & 0b00000010) != 0b00000000 ) { // loop while pin 10 is HIGH y++; } delay(1); fout = (fval * (float)x) + ((1-fval) * accum); // Easy smoothing filter "fval" determines amount of new data in fout accum = fout; } // Check on threshold if (x > threshold && y > threshold) { PORTB |= 0b00100000; Serial.println("Tocca"); } else { PORTB &= 0b11011111; Serial.println("Non tocca"); } Serial.print("\nCarica: "); Serial.print(x); Serial.print(" / Sarica: "); Serial.print(y); Serial.println(); delay(1500); }