source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/CPlay_CapacitiveSensor.cpp

Last change on this file was 3, checked in by markus, 7 years ago

Setup project structure

File size: 8.0 KB
Line 
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#if ARDUINO >= 100
13#include "Arduino.h"
14#else
15#include "WProgram.h"
16#include "pins_arduino.h"
17#include "WConstants.h"
18#endif
19
20#include "CPlay_CapacitiveSensor.h"
21
22/**************************************************************************/
23/*!
24    @brief  Function that handles the creation and setup of instances
25    @param sendPin send pin for the sensor
26    @param receivePin the receiving pin for the sensor
27*/
28/**************************************************************************/
29CPlay_CapacitiveSensor::CPlay_CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
30{
31        // initialize this instance's variables
32        // Serial.begin(9600);          // for debugging
33        error = 1;
34        loopTimingFactor = 300;         // determined empirically -  a hack
35
36        CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
37        CS_AutocaL_Millis = 20000;
38
39        // Serial.print("timwOut =  ");
40        // Serial.println(CS_Timeout_Millis);
41        // get pin mapping and port for send Pin - from PinMode function in core
42
43#ifdef NUM_DIGITAL_PINS
44        if (sendPin >= NUM_DIGITAL_PINS) error = -1;
45        if (receivePin >= NUM_DIGITAL_PINS) error = -2;
46#endif
47       
48        // sendpin to OUTPUT
49#ifdef ARDUINO_TEEONARDU_FLORA
50        // terrible hack!
51        DDRD |= _BV(5);
52#else
53        pinMode(sendPin, OUTPUT);
54#endif
55
56        pinMode(receivePin, INPUT);                                             // receivePin to INPUT
57
58#ifdef ARDUINO_TEEONARDU_FLORA
59        // terrible hack!
60        PORTD &= ~_BV(5);
61#else
62        digitalWrite(sendPin, LOW);
63#endif
64
65        sBit = PIN_TO_BITMASK(sendPin);                                 // get send pin's ports and bitmask
66        sReg = PIN_TO_BASEREG(sendPin);                                 // get pointer to output register
67
68        rBit = PIN_TO_BITMASK(receivePin);                              // get receive pin's ports and bitmask
69        rReg = PIN_TO_BASEREG(receivePin);
70
71        // get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
72        leastTotal = 0x0FFFFFFFL;   // input large value for autocalibrate begin
73        lastCal = millis();         // set millis for start
74}
75
76// Public Methods //////////////////////////////////////////////////////////////
77// Functions available in Wiring sketches, this library, and other libraries
78
79
80/**************************************************************************/
81/*!
82    @brief  get a capacitive sensor reading
83    @param samples number of samples to take
84    @return the sensor reading
85*/
86/**************************************************************************/
87long CPlay_CapacitiveSensor::capacitiveSensor(uint8_t samples)
88{
89        total = 0;
90        if (samples == 0) return 0;
91        if (error < 0) return -1;            // bad pin
92
93
94        for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
95                if (SenseOneCycle() < 0)  return -2;   // variable over timeout
96}
97
98                // only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline
99                // this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal
100
101                if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total  - leastTotal) < (int)(.10 * (float)leastTotal) ) {
102
103                        // Serial.println();               // debugging
104                        // Serial.println("auto-calibrate");
105                        // Serial.println();
106                        // delay(2000); */
107
108                        leastTotal = 0x0FFFFFFFL;          // reset for "autocalibrate"
109                        lastCal = millis();
110                }
111                /*else{                                // debugging
112                        Serial.print("  total =  ");
113                        Serial.print(total);
114
115                        Serial.print("   leastTotal  =  ");
116                        Serial.println(leastTotal);
117
118                        Serial.print("total - leastTotal =  ");
119                        x = total - leastTotal ;
120                        Serial.print(x);
121                        Serial.print("     .1 * leastTotal = ");
122                        x = (int)(.1 * (float)leastTotal);
123                        Serial.println(x);
124                } */
125
126        // routine to subtract baseline (non-sensed capacitance) from sensor return
127        if (total < leastTotal) leastTotal = total;                 // set floor value to subtract from sensed value
128        return(total - leastTotal);
129
130}
131
132/**************************************************************************/
133/*!
134    @brief  get a raw sensor reading
135    @param samples the number of samples to take
136    @return -1 for error, -2 for timeout, other values are a raw sensor reading
137*/
138/**************************************************************************/
139long CPlay_CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
140{
141        total = 0;
142        if (samples == 0) return 0;
143        if (error < 0) return -1;                  // bad pin - this appears not to work
144
145        for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
146                if (SenseOneCycle() < 0)  return -2;   // variable over timeout
147        }
148
149        return total;
150}
151
152/**************************************************************************/
153/*!
154    @brief  reset the auto calibration
155*/
156/**************************************************************************/
157void CPlay_CapacitiveSensor::reset_CS_AutoCal(void){
158        leastTotal = 0x0FFFFFFFL;
159}
160
161/**************************************************************************/
162/*!
163    @brief set the auto-calibration time
164    @param autoCal_millis the desired calibration time in milliseconds
165*/
166/**************************************************************************/
167void CPlay_CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){
168        CS_AutocaL_Millis = autoCal_millis;
169}
170
171/**************************************************************************/
172/*!
173    @brief  set the sensor timeout
174    @param timeout_millis the number of milliseconds to set the timeout to
175*/
176/**************************************************************************/
177void CPlay_CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
178        CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000;  // floats to deal with large numbers
179}
180
181// Private Methods /////////////////////////////////////////////////////////////
182// Functions only available to other functions in this library
183
184/**************************************************************************/
185/*!
186    @brief  sense a single sicle
187    @return the reading
188*/
189/**************************************************************************/
190int CPlay_CapacitiveSensor::SenseOneCycle(void)
191{
192    noInterrupts();
193        DIRECT_WRITE_LOW(sReg, sBit);   // sendPin Register low
194        DIRECT_MODE_INPUT(rReg, rBit);  // receivePin to input (pullups are off)
195        DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
196        DIRECT_WRITE_LOW(rReg, rBit);   // pin is now LOW AND OUTPUT
197        delayMicroseconds(10);
198        DIRECT_MODE_INPUT(rReg, rBit);  // receivePin to input (pullups are off)
199        DIRECT_WRITE_HIGH(sReg, sBit);  // sendPin High
200    interrupts();
201
202        while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) {  // while receive pin is LOW AND total is positive value
203                total++;
204        }
205        //Serial.print("SenseOneCycle(1): "); Serial.println(total);
206
207        if (total > CS_Timeout_Millis) {
208                return -2;         //  total variable over timeout
209        }
210
211        // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
212    noInterrupts();
213        DIRECT_WRITE_HIGH(rReg, rBit);
214        DIRECT_MODE_OUTPUT(rReg, rBit);  // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
215        DIRECT_WRITE_HIGH(rReg, rBit);
216        DIRECT_MODE_INPUT(rReg, rBit);  // receivePin to INPUT (pullup is off)
217        DIRECT_WRITE_LOW(sReg, sBit);   // sendPin LOW
218    interrupts();
219
220#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
221        DIRECT_MODE_OUTPUT(rReg, rBit);
222        DIRECT_WRITE_LOW(rReg, rBit);
223        delayMicroseconds(10);
224        DIRECT_MODE_INPUT(rReg, rBit);  // receivePin to INPUT (pullup is off)
225#else
226        while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) {  // while receive pin is HIGH  AND total is less than timeout
227                total++;
228        }
229#endif
230        //Serial.print("SenseOneCycle(2): ");
231        //Serial.println(total);
232
233        if (total >= CS_Timeout_Millis) {
234                return -2;     // total variable over timeout
235        } else {
236                return 1;
237        }
238}
Note: See TracBrowser for help on using the repository browser.