Guest User

Untitled

a guest
Mar 3rd, 2018
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <CapacitiveSensor.h>
  2. #include "RunningMedian.h"
  3. /*
  4. * CapitiveSense Library Demo Sketch
  5. * Paul Badger 2008
  6. * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
  7. * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
  8. * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
  9. * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
  10. */
  11.  
  12. RunningMedian samples = RunningMedian(100);
  13. RunningMedian samples2 = RunningMedian(100);
  14. long count = 0;
  15. int i = 0;
  16. CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
  17. CapacitiveSensor cs_8_7 = CapacitiveSensor(8,7); // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
  18. //CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
  19.  
  20. void setup()
  21. {
  22. pinMode(A0, INPUT);
  23. //digitalWrite(A0, LOW);
  24. cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
  25. Serial.begin(115200);
  26.  
  27. }
  28.  
  29. void loop()
  30. {
  31. long start = millis();
  32. unsigned long int total1 = cs_4_2.capacitiveSensor(30);
  33. unsigned long int total2 = cs_8_7.capacitiveSensor(30);
  34. //Serial.print("A0 : ");
  35. //Serial.print(analogRead(A0));
  36. Serial.print(" Total 1 : ");
  37. Serial.print(total1);
  38. Serial.print(" Total 2 : ");
  39. Serial.println(total2);
  40.  
  41. delay(100);
  42. while ((total1 >= 200) && (i <101))
  43. {
  44. long total1 = cs_4_2.capacitiveSensor(30);
  45. long total2 = cs_8_7.capacitiveSensor(30);
  46. Serial.print("A0 : ");
  47. Serial.print(analogRead(A0));
  48. Serial.print("Total 1 : ");
  49. Serial.print(total1);
  50. Serial.print(" Total2 : ");
  51. Serial.println(total2);
  52. samples.add(total1);
  53. samples2.add(total2);
  54. i++;
  55. delay(100);
  56. if (total1 <= 200)
  57. break;
  58. }
  59.  
  60. float m = samples.getMedian();
  61. float n = samples2.getMedian();
  62. Serial.print("m : ");
  63. Serial.print(m);
  64. Serial.print(" n : ");
  65. Serial.println(n);
  66.  
  67. }
RAW Paste Data