Robert_l

CapTouch

Apr 13th, 2020
176
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. float offset;
  3. float distance;
  4. float distance_raw;
  5. float filter_coef = 0.025;
  6. float measureToDistance = 1;
  7.  
  8. void setup() {
  9.   // put your setup code here, to run once:
  10.   Serial.begin(9600);
  11.   offset=0;
  12.   /*
  13.   for (int i = 0; i < 1000; i++) {
  14.     offset += measure();
  15.   }
  16.   offset /= 1000;
  17.   */
  18. }
  19.  
  20. void loop() {
  21.   // put your main code here, to run repeatedly:
  22.   distance_raw = ((float)capMeasure() - offset) * measureToDistance;
  23.   distance = distance * (1 - filter_coef) + distance_raw * filter_coef;
  24.   Serial.print(distance_raw);
  25.   Serial.print(',');
  26.   Serial.print(distance);
  27.   Serial.println();
  28.   delay(10);
  29. }
  30.  
  31. unsigned int capMeasure() {
  32.   //charge share capacitance measure on A1 pin. A0 is used to charge S/H cap
  33.   pinMode(A1, OUTPUT);
  34.   pinMode (A0, INPUT_PULLUP);
  35.   analogRead(A0);
  36.   pinMode(A1, INPUT);
  37.   unsigned long value = analogRead(A1);
  38.   pinMode(A1, OUTPUT);
  39.   return value;
  40. }
RAW Paste Data