Advertisement
Guest User

Raspberry Pi Touch Switch

a guest
Apr 6th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. // Filename: touch_toggle.c
  2.  
  3. /* Compile with gcc -lwiringPi touch_toggle.c -o touch_toggle
  4.  * Run with sudo ./touch_toggle
  5.  * Do not touch while starting the program so it can initialize properly
  6.  */
  7.  
  8. /* SCHEMATIC
  9.  *
  10.  * ,----------------------,
  11.  * |   Raspberry Pi       |
  12.  * |                      |
  13.  * | TOUCH_PIN        VCC |
  14.  * `-----+-------------+--'
  15.  *       |             |
  16.  *       +---[1MΩ]-----+
  17.  *       |
  18.  *   Touch surface
  19.  *
  20.  */
  21. #include <wiringPi.h>
  22. #include <stdio.h>
  23. // Note: Pin numbers are in BCM notation (pin number format is set by wiringPiSetupGpio)
  24. // See pinout.xyz
  25. #define TOUCH_PIN 20
  26. #define OUTPUT_PIN 21
  27. // How long to pull the touch pin low
  28. // Controls loop speed and affects CPU usage
  29. #define DELAY 15
  30.  
  31. int main(void) {
  32.     wiringPiSetupGpio();
  33.     unsigned int timer;
  34.     unsigned int threshold = 0;
  35.     unsigned char state = 0; // Currently being touched?
  36.     unsigned char out_state = 0; // State of output pin
  37.     signed char hysteresis = 0; // Counter for consecutive readings
  38.     pullUpDnControl(TOUCH_PIN, PUD_OFF); // Not sure if this would ever be set, just to be safe
  39.     pinMode(OUTPUT_PIN, OUTPUT);
  40.     digitalWrite(OUTPUT_PIN, out_state);
  41.  
  42.     // Measure capacitance to calibrate touch sensitivity
  43.     for (char i=0; i < 10; i++) {
  44.         // Pull touch pin low to discharge
  45.         pinMode(TOUCH_PIN, OUTPUT);
  46.         digitalWrite(TOUCH_PIN, LOW);
  47.         // Wait a bit
  48.         delay(DELAY);
  49.         // Start timer
  50.         timer = micros();
  51.         pinMode(TOUCH_PIN, INPUT);
  52.         // Wait for pin to become high
  53.         while (!digitalRead(TOUCH_PIN));
  54.         // Get time elapsed
  55.         threshold += micros() - timer;
  56.     }
  57.     // Set threshold to twice the average capacitance
  58.     threshold /= 5; // This number might need to be increased if the touch is not sensitive enough
  59.     printf("threshold=%d\n",threshold);
  60.  
  61.     while (1) {
  62.         pinMode(TOUCH_PIN, OUTPUT);
  63.         digitalWrite(TOUCH_PIN, LOW);
  64.         delay(DELAY);
  65.  
  66.         timer = micros();
  67.         pinMode(TOUCH_PIN, INPUT);
  68.         while (!digitalRead(TOUCH_PIN));
  69.         timer = micros() - timer;
  70.  
  71.         if (timer > threshold) {
  72.             if (hysteresis < 0) hysteresis = 0;
  73.             hysteresis++;
  74.         } else {
  75.             if (hysteresis > 0) hysteresis = 0;
  76.             hysteresis--;
  77.         }
  78.  
  79.         // 3 consecutive readings are required to toggle touch state
  80.         if (hysteresis > 2) {
  81.             if (state == 0) {
  82.                 out_state = !out_state;
  83.                 digitalWrite(OUTPUT_PIN, out_state);
  84.                 state = 1;
  85.                
  86.                 // Print when touch starts and the measured value
  87.                 // Can be commented out
  88.                 printf("START %d", timer);
  89.                 fflush(stdout); // Display instantly (by default only flushed on newline)
  90.             }
  91.             hysteresis = 0;
  92.         } else if (hysteresis < -2) {
  93.             if (state == 1) {
  94.                 state = 0;
  95.                
  96.                 printf(" END\n");
  97.             }
  98.             hysteresis = 0;
  99.         }
  100.     }
  101.     return 0;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement