#include #include SoftwareSerial mySerial(5, 1); int i; unsigned int x, y; float accum, fout, fval = 0.001; //long initialThreshold = 0; long threshold = 0; //int value; //const int potPin = 10; const int capR = 8; const int capS = 7; void setup() { mySerial.begin(9600); mySerial.print("PRONTO!"); pinMode (capR, INPUT); pinMode (capS, OUTPUT); for (short i = 0; i < 10; i++) { long max; y = 0; x = 0; for (short i = 0; i < 4; i++) { digitalWrite(capS, HIGH); while (!digitalRead(capR)) { x++; } delay(1); digitalWrite(capS, LOW); while(digitalRead(capR)) { y++; } delay(1); } max = (x > y ? x : y); threshold = (threshold > max ? threshold : max); } mySerial.print("Threshold: "); //mySerial.println(threshold); } void loop() { mySerial.println('a'); y = 0; x = 0; /* value = analogRead(potPin); mySerial.println("\nValue: "); mySerial.println(value); threshold = initialThreshold - ( 2 * value); mySerial.println("\nNew Threshold: "); mySerial.print(threshold); */ for (short i = 0; i < 4; i++) { digitalWrite(capS, HIGH); while (digitalRead(capR) == LOW) { x++; } delay(1); digitalWrite(capS, LOW) ; while (digitalRead(capR) == HIGH) { y++; } delay(1); fout = (fval * (float)x) + ((1-fval) * accum); accum = fout; } if (x > threshold && y > threshold) { mySerial.println("Tocca"); } else { mySerial.println("Non tocca"); } mySerial.print("\nCarica: "); mySerial.print(x); mySerial.print(" / Sarica: "); mySerial.print(y); mySerial.println(); delay(1500); }