|
| 1 | +/* |
| 2 | + CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring |
| 3 | + https://github.com/PaulStoffregen/CapacitiveSensor |
| 4 | + http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html |
| 5 | + http://playground.arduino.cc/Main/CapacitiveSensor |
| 6 | + Copyright (c) 2009 Paul Bagder All right reserved. |
| 7 | + Version 05 by Paul Stoffregen - Support non-AVR board: Teensy 3.x, Arduino Due |
| 8 | + Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix |
| 9 | + vim: set ts=4: |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +#include "Arduino.h" |
| 14 | +#include "CapacitiveSensor.h" |
| 15 | + |
| 16 | +// Constructor ///////////////////////////////////////////////////////////////// |
| 17 | +// Function that handles the creation and setup of instances |
| 18 | + |
| 19 | +CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin) |
| 20 | +{ |
| 21 | + // initialize this instance's variables |
| 22 | + // Serial.begin(9600); // for debugging |
| 23 | + error = 1; |
| 24 | + loopTimingFactor = 310; // determined empirically - a hack |
| 25 | + |
| 26 | + CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000; |
| 27 | + CS_AutocaL_Millis = 20000; |
| 28 | + |
| 29 | + // get pin mapping and port for send Pin - from PinMode function in core |
| 30 | + if ((sendPin >= NUM_DIGITAL_PINS) || (receivePin >= NUM_DIGITAL_PINS)) { |
| 31 | + error = -1; |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + rPin = receivePin; |
| 36 | + sPin = sendPin; |
| 37 | + |
| 38 | + pinMode(sPin, OUTPUT); // sendpin to OUTPUT |
| 39 | + pinMode(rPin, INPUT); // receivePin to INPUT |
| 40 | + digitalWrite(sPin, LOW); |
| 41 | + |
| 42 | + // get pin mapping and port for receive Pin - from digital pin functions in Wiring.c |
| 43 | + leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin |
| 44 | + lastCal = millis(); // set millis for start |
| 45 | +} |
| 46 | + |
| 47 | +// Public Methods ////////////////////////////////////////////////////////////// |
| 48 | +// Functions available in Wiring sketches, this library, and other libraries |
| 49 | + |
| 50 | +long CapacitiveSensor::capacitiveSensor(uint8_t samples) |
| 51 | +{ |
| 52 | + total = 0; |
| 53 | + if (samples == 0) return 0; |
| 54 | + if (error < 0) return -1; // bad pin |
| 55 | + |
| 56 | + |
| 57 | + for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter |
| 58 | + if (SenseOneCycle() < 0) return -2; // variable over timeout |
| 59 | +} |
| 60 | + |
| 61 | + // only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline |
| 62 | + // this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal |
| 63 | + |
| 64 | + if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total - leastTotal) < (int)(.10 * (float)leastTotal) ) { |
| 65 | + |
| 66 | + // Serial.println(); // debugging |
| 67 | + // Serial.println("auto-calibrate"); |
| 68 | + // Serial.println(); |
| 69 | + // delay(2000); */ |
| 70 | + |
| 71 | + leastTotal = 0x0FFFFFFFL; // reset for "autocalibrate" |
| 72 | + lastCal = millis(); |
| 73 | + } |
| 74 | + /*else{ // debugging |
| 75 | + Serial.print(" total = "); |
| 76 | + Serial.print(total); |
| 77 | +
|
| 78 | + Serial.print(" leastTotal = "); |
| 79 | + Serial.println(leastTotal); |
| 80 | +
|
| 81 | + Serial.print("total - leastTotal = "); |
| 82 | + x = total - leastTotal ; |
| 83 | + Serial.print(x); |
| 84 | + Serial.print(" .1 * leastTotal = "); |
| 85 | + x = (int)(.1 * (float)leastTotal); |
| 86 | + Serial.println(x); |
| 87 | + } */ |
| 88 | + |
| 89 | + // routine to subtract baseline (non-sensed capacitance) from sensor return |
| 90 | + if (total < leastTotal) leastTotal = total; // set floor value to subtract from sensed value |
| 91 | + return(total - leastTotal); |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples) |
| 96 | +{ |
| 97 | + total = 0; |
| 98 | + if (samples == 0) return 0; |
| 99 | + if (error < 0) return -1; // bad pin - this appears not to work |
| 100 | + |
| 101 | + for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter |
| 102 | + if (SenseOneCycle() < 0) return -2; // variable over timeout |
| 103 | + } |
| 104 | + |
| 105 | + return total; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +void CapacitiveSensor::reset_CS_AutoCal(void){ |
| 110 | + leastTotal = 0x0FFFFFFFL; |
| 111 | +} |
| 112 | + |
| 113 | +void CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){ |
| 114 | + CS_AutocaL_Millis = autoCal_millis; |
| 115 | +} |
| 116 | + |
| 117 | +void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){ |
| 118 | + CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000; // floats to deal with large numbers |
| 119 | +} |
| 120 | + |
| 121 | +// Private Methods ///////////////////////////////////////////////////////////// |
| 122 | +// Functions only available to other functions in this library |
| 123 | + |
| 124 | +int CapacitiveSensor::SenseOneCycle(void) |
| 125 | +{ |
| 126 | + //noInterrupts(); |
| 127 | + digitalWrite(sPin, LOW);// sendPin Register low |
| 128 | + pinMode(rPin, INPUT);// receivePin to input (pullups are off) |
| 129 | + pinMode(rPin, OUTPUT);// receivePin to OUTPUT |
| 130 | + digitalWrite(rPin, LOW);// pin is now LOW AND OUTPUT |
| 131 | + delayMicroseconds(10); |
| 132 | + pinMode(rPin, INPUT);// receivePin to input (pullups are off) |
| 133 | + digitalWrite(sPin, HIGH);// sendPin High |
| 134 | + //interrupts(); |
| 135 | + |
| 136 | + while ( !digitalRead(rPin) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value |
| 137 | + total++; |
| 138 | + } |
| 139 | + //Serial.print("SenseOneCycle(1): "); |
| 140 | + //Serial.println(total); |
| 141 | + |
| 142 | + if (total > CS_Timeout_Millis) { |
| 143 | + return -2; // total variable over timeout |
| 144 | + } |
| 145 | + |
| 146 | + // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V |
| 147 | + //noInterrupts(); |
| 148 | + //pinMode(receivePin, INPUT_PULLUP); |
| 149 | + pinMode(rPin, OUTPUT); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT |
| 150 | + digitalWrite(rPin, HIGH); |
| 151 | + pinMode(rPin, INPUT);// receivePin to INPUT (pullup is off) |
| 152 | + digitalWrite(sPin, LOW);// sendPin LOW |
| 153 | + //interrupts(); |
| 154 | + |
| 155 | + while ( digitalRead(rPin) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout |
| 156 | + total++; |
| 157 | + } |
| 158 | + //Serial.print("SenseOneCycle(2): "); |
| 159 | + //Serial.println(total); |
| 160 | + |
| 161 | + if (total >= CS_Timeout_Millis) { |
| 162 | + return -2; // total variable over timeout |
| 163 | + } else { |
| 164 | + return 1; |
| 165 | + } |
| 166 | +} |
0 commit comments