Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // Sensor pin and readings
  2. int sensorPin = 5;
  3. const int numReadings = 10;
  4. int sensorValues[numReadings];
  5. int sensorAverage;
  6. int sensorThreshold = 100;
  7. int sensorBaseValue = 438;
  8.  
  9. // Sip and puff detection variables
  10. boolean sipThresholdCrossed = false;
  11. boolean puffThresholdCrossed = false;
  12. boolean sipDetected = false;
  13. boolean puffDetected = false;
  14.  
  15. // General program behavior
  16. boolean debug = true;
  17.  
  18. void setup() {
  19.   pinMode(sensorPin, INPUT);
  20.  
  21.   Serial.begin(9600);
  22.   Keyboard.begin();
  23. }
  24.  
  25. void loop() {
  26.   readSensor();
  27.   processSensor();
  28.  
  29.   // Sip has been detected - do something
  30.   if(sipDetected) {
  31.     Serial.println("# Sip detected");
  32.     Keyboard.write(' ');
  33.     sipDetected = false;
  34.   }
  35.  
  36.   // Puff has been detected - do something
  37.   if(puffDetected) {
  38.     Serial.println("# Puff detected");
  39.     puffDetected = false;
  40.   }
  41.  
  42.   if(debug)
  43.     erial.println(sensorAverage);
  44. }
  45.  
  46. /****************************************
  47.  readSensor()
  48.  Read sensor value several times, store
  49.  average in globally-accessible variable
  50. *****************************************/
  51. void readSensor() {
  52.   // Take a few readings
  53.   for(int i=0; i<numReadings; i++) {
  54.     int v = analogRead(sensorPin);
  55.     sensorValues[i] = v;
  56.   }
  57.  
  58.   // Find average of all readings
  59.   int total = 0;
  60.   for(int i=0; i<numReadings; i++) {
  61.     total += sensorValues[i];
  62.   }
  63.   sensorAverage = total/numReadings;
  64. }
  65.  
  66. /*******************************************
  67.  Process the sensor data to derive sipping
  68.  and puffing activity.
  69. ********************************************/
  70. void processSensor() {
  71.   // While data is hovering in safe zone
  72.   if(sipThresholdCrossed == false && puffThresholdCrossed == false) {
  73.     // Check if the data has crossed either the sip or puff threshold
  74.     if(sensorAverage <= sensorBaseValue - sensorThreshold) {
  75.       sipThresholdCrossed = true;
  76.     } else if(sensorAverage >= sensorBaseValue + sensorThreshold) {
  77.       puffThresholdCrossed = true;
  78.     }
  79.    
  80.   // If data has drifted above or below safe zone
  81.   } else {
  82.     // Check if the data has returned to safe zone
  83.     if(sensorAverage <= sensorBaseValue + sensorThreshold && sensorAverage >= sensorBaseValue - sensorThreshold) {
  84.       if(sipThresholdCrossed) {
  85.         sipDetected = true;
  86.         sipThresholdCrossed = false;
  87.       }
  88.      
  89.       if(puffThresholdCrossed) {
  90.         puffDetected = true;
  91.         puffThresholdCrossed = false;
  92.       }
  93.     }      
  94.   }
  95. }