-
Notifications
You must be signed in to change notification settings - Fork 15
/
CapSlider.cpp
executable file
·200 lines (136 loc) · 6.01 KB
/
CapSlider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
CapSlider.cpp v.05 - Capacitive Sensing Library for 'duino / Wiring
Paul Bagder 2009-2012
Implements a capacitive slider using some form of resistive bridge.
*/
// include this library's description file
#include "CapSlider.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#include "WConstants.h"
#endif
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
CapSlider::CapSlider(uint8_t sendPin, uint8_t receivePin){
// initialize this instance's variables
// Serial.begin(9600); // for debugging
// error = 1;
// Serial.print("timwOut = ");
// get pin mapping and port for send Pin - from PinMode function in Arduino core
sBit = digitalPinToBitMask(sendPin); // get send pin's ports and bitmask
sPort = digitalPinToPort(sendPin);
sOut = portOutputRegister(sPort); // get pointer to output register
sIn = portInputRegister(sPort); // get pointer to input register
sReg = portModeRegister(sPort);
rBit = digitalPinToBitMask(receivePin); // get receive pin's ports and bitmask
rPort = digitalPinToPort(receivePin);
rOut = portOutputRegister(rPort);
if (sPort == NOT_A_PORT) {error = -1; // this Arduino function does not appear to work
// Serial.println("bad pin");
}
// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
if (rPort == NOT_A_PORT){error = -1; // this Arduino function does not appear to work
// Serial.println("bad pin");
}
rReg = portModeRegister(rPort);
rOut = portOutputRegister(rPort);
rIn = portInputRegister(rPort);
*sReg |= sBit; // set sendpin to OUTPUT
calibrateFlag = 0;
}
// Public Methods //////////////////////////////////////////////////////////////
// readSlider reads the sensor the first time through, and uses that value for
// baseline reading to calibrate the sensor. It takes a chip reboot to update the baseline values.
int CapSlider::readSlider(unsigned int samples){
int i;
totalR = 0; // total for when "receive" pin is receiving (acting as the sensor pin)
totalS = 0; // total for when "send" pin is receiving
head: // repeat for calibrate first time through
for(i =0; i < samples; i++){
// we will set up a send-receive cycle for each pin
// in the second cycle, the role of the send and receive pins will be reversed
// while the pin actions stay the same as the first pair
*sReg |= sBit; // set send pin to OUTPUT
*sOut &= ~sBit; // set Send Pin Register LOW to discharge send pin
*rOut &= ~rBit; // receive pin LOW - because the bottom loop will exit when pin is ~ 2.5V
*rReg |= rBit; // receive pin is LOW AND OUTPUT to discharge pin
delayMicroseconds(10); // short pause to discharge pin
*rReg &= ~rBit; // set receive pin to INPUT
*sOut |= sBit; // set send pin High
while( !(*rIn & rBit) && (totalR < timeoutCount )){ // while receive pin is LOW && total < timeout value
totalR++;
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
*rOut |= rBit; // set receive pin HIGH - turns on pullup
*rReg |= rBit; // pin is now HIGH AND OUTPUT
delayMicroseconds(10); // short pause to discharge pin
*rReg &= ~rBit; // set pin to INPUT
*rOut &= ~rBit; // turn off pullup
*sOut &= ~sBit; // set send pin LOW
while( (*rIn & rBit) && (totalR < timeoutCount)){ // while receive pin is HIGH AND total is less than timeout
totalR++;
}
// OK reverse the roles now so sIn is doing the receiving
// receive pin is now doing the sending
// actual pins in [brackets] in comments - maybe not worth the confusion
*rReg |= rBit; // set send [receive] pin to OUTPUT
*rOut &= ~rBit; // set Send Pin Register LOW to discharge send pin
*sOut &= ~sBit; // receive [send] pin LOW - because the bottom loop will exit when pin is ~ 2.5V
*sReg |= sBit; // receive [send] pin is LOW AND OUTPUT to discharge pin
delayMicroseconds(10); // short pause to discharge pin
*sReg &= ~sBit; // set receive [send] pin to INPUT
*rOut |= rBit; // set send [receive] pin High
while( !(*sIn & sBit) && (totalS < timeoutCount )){ // while receive pin is LOW && total < timeout value
totalS++;
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
*sOut |= sBit; // set receive [send] pin HIGH - turns on pullup
*sReg |= sBit; // pin is now HIGH AND OUTPUT
delayMicroseconds(10); // short pause to discharge pin
*sReg &= ~sBit; // set receive [send] pin to INPUT
*sOut &= ~sBit; // turn off pullup
*rOut &= ~rBit; // set send [receive] pin LOW
while( (*sIn & sBit) && (totalS < timeoutCount)){ // while receive pin is HIGH AND total is less than timeout
totalS++;
}
}
if (calibrateFlag == 0){
rBaseline = totalR++;
sBaseline = totalS++;
calibrateFlag = 1;
goto head;
}
/* Serial.print("rBaseline ");
Serial.print(rBaseline);
Serial.print(" ");
Serial.print(totalR);
Serial.print(" ");
Serial.print(totalR - rBaseline);
Serial.print(" sBaseline ");
Serial.print(sBaseline);
Serial.print(" ");
Serial.print(totalS);
Serial.print(" ");
Serial.println(totalS - sBaseline); */
if (totalR + totalS >= timeoutCount){
Serial.println("Sensor over timeout. Check your wiring");
return -2; // total variable over timeout
}
totalR = totalR - rBaseline;
if (totalR < 0) totalR = 0;
totalS = totalS - sBaseline;
if (totalS < 0) totalS = 0;
/* Serial.print(totalR);
Serial.print(" ");
Serial.println(totalS); */
pressure = totalR + totalS;
if (pressure < SENSOR_NOT_TOUCHED){
return 0; // return zero instead of random noise for untouched sensor
}
else {
return ((float)totalR * 100.0) / (float)(totalR + totalS);
}
}