MCPcopy Create free account
hub / github.com/PaulStoffregen/CapacitiveSensor / SenseOneCycle

Method SenseOneCycle

CapacitiveSensor.cpp:137–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

135// Functions only available to other functions in this library
136
137int CapacitiveSensor::SenseOneCycle(void)
138{
139 noInterrupts();
140 DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low
141 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
142 DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
143 DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT
144 delayMicroseconds(10);
145 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
146 DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High
147 interrupts();
148
149 while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
150 total++;
151 }
152 //Serial.print("SenseOneCycle(1): ");
153 //Serial.println(total);
154
155 if (total > CS_Timeout_Millis) {
156 return -2; // total variable over timeout
157 }
158
159 // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
160 noInterrupts();
161 DIRECT_WRITE_HIGH(rReg, rBit);
162 DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
163 DIRECT_WRITE_HIGH(rReg, rBit);
164 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
165 DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW
166 interrupts();
167
168#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
169 DIRECT_MODE_OUTPUT(rReg, rBit);
170 DIRECT_WRITE_LOW(rReg, rBit);
171 delayMicroseconds(10);
172 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
173#else
174 while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
175 total++;
176 }
177#endif
178 //Serial.print("SenseOneCycle(2): ");
179 //Serial.println(total);
180
181 if (total >= CS_Timeout_Millis) {
182 return -2; // total variable over timeout
183 } else {
184 return 1;
185 }
186}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected