Advertisement
Guest User

main.cpp

a guest
Feb 25th, 2017
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /*
  2. * main.cpp
  3. *
  4. *
  5. * 500K resistor and 5pF cap between D1 and ground
  6. * measured time 10-15uS without touch, >150uS with touch
  7. *
  8. */
  9.  
  10.  
  11.  
  12. #include "Arduino.h"
  13.  
  14. #define D1 5
  15. #define CHARGE_TIME 500000 // 500ms
  16. #define ERR_TIMEOUT -2
  17. #define ERR_NOTREADY -1
  18.  
  19. long waitFor(int value) {
  20. unsigned long start = micros();
  21. unsigned long end = start + CHARGE_TIME; // 100ms is far too long
  22. unsigned long now = start;
  23. while(true) {
  24. if(now>end) { return ERR_TIMEOUT; }
  25. if(digitalRead(D1)==value) { break; }
  26. yield();
  27. now = micros();
  28. }
  29. return now - start;
  30. }
  31.  
  32. long waitHigh() {
  33. return waitFor(HIGH);
  34. }
  35.  
  36. long waitLow() {
  37. return waitFor(LOW);
  38. }
  39.  
  40. void charge() {
  41. pinMode(D1, OUTPUT);
  42. digitalWrite(D1, HIGH);
  43. // Serial.print("charging...");
  44. delayMicroseconds(CHARGE_TIME); // wait for cap to charge
  45. // Serial.println("done");
  46. }
  47.  
  48. void setup() {
  49. Serial.begin(9600);
  50. Serial.println("running test_captouch");
  51. // WiFi.forceSleepBegin();
  52. charge();
  53. Serial.println("setup complete");
  54. }
  55.  
  56. long measure() {
  57. pinMode(D1, INPUT);
  58. long out = waitLow();
  59. charge();
  60. return out;
  61. }
  62.  
  63. void loop() {
  64. long time = measure();
  65. // print sort of a bar graph -- #chars width = time/10
  66. // watch width change as you touch and release
  67. int i;
  68. for(i=0;i<time/10;i++) {
  69. Serial.print("-");
  70. }
  71. Serial.println("X");
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement