Przeglądaj źródła

Use pinMode for initial pin configuration & clean up whitespace

PaulStoffregen 11 lat temu
rodzic
commit
4a5dc4fc2b
1 zmienionych plików z 34 dodań i 31 usunięć
  1. 34 31
      CapacitiveSensor.cpp

+ 34 - 31
CapacitiveSensor.cpp

@@ -26,35 +26,35 @@ CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
 	// Serial.begin(9600);		// for debugging
 	error = 1;
 	loopTimingFactor = 310;		// determined empirically -  a hack
-	
+
 	CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
 	CS_AutocaL_Millis = 20000;
-    
+
 	// Serial.print("timwOut =  ");
 	// Serial.println(CS_Timeout_Millis);
-	
+
 	// get pin mapping and port for send Pin - from PinMode function in core
 
 #ifdef NUM_DIGITAL_PINS
 	if (sendPin >= NUM_DIGITAL_PINS) error = -1;
 	if (receivePin >= NUM_DIGITAL_PINS) error = -1;
 #endif
-	
+
+	pinMode(sendPin, OUTPUT);						// sendpin to OUTPUT
+	pinMode(receivePin, INPUT);						// receivePin to INPUT
+
 	sBit =  digitalPinToBitMask(sendPin);			// get send pin's ports and bitmask
 	sPort = digitalPinToPort(sendPin);
 	sReg = portModeRegister(sPort);
-	sOut = portOutputRegister(sPort);				// get pointer to output register   
+	sOut = portOutputRegister(sPort);				// get pointer to output register
 
-	rBit = digitalPinToBitMask(receivePin);			// get receive pin's ports and bitmask 
+	rBit = digitalPinToBitMask(receivePin);			// get receive pin's ports and bitmask
 	rPort = digitalPinToPort(receivePin);
 	rReg = portModeRegister(rPort);
 	rIn  = portInputRegister(rPort);
    	rOut = portOutputRegister(rPort);
-	
+
 	// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
-    noInterrupts();
-	*sReg |= sBit;              // set sendpin to OUTPUT 
-    interrupts();
 	leastTotal = 0x0FFFFFFFL;   // input large value for autocalibrate begin
 	lastCal = millis();         // set millis for start
 }
@@ -86,7 +86,7 @@ long CapacitiveSensor::capacitiveSensor(uint8_t samples)
 			leastTotal = 0x0FFFFFFFL;          // reset for "autocalibrate"
 			lastCal = millis();
 		}
-		/*else{                                // debugging 
+		/*else{                                // debugging
 			Serial.print("  total =  ");
 			Serial.print(total);
 
@@ -98,11 +98,11 @@ long CapacitiveSensor::capacitiveSensor(uint8_t samples)
 			Serial.print(x);
 			Serial.print("     .1 * leastTotal = ");
 			x = (int)(.1 * (float)leastTotal);
-			Serial.println(x);   
+			Serial.println(x);
 		} */
 
 	// routine to subtract baseline (non-sensed capacitance) from sensor return
-	if (total < leastTotal) leastTotal = total;                 // set floor value to subtract from sensed value         
+	if (total < leastTotal) leastTotal = total;                 // set floor value to subtract from sensed value
 	return(total - leastTotal);
 
 }
@@ -139,39 +139,42 @@ void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
 int CapacitiveSensor::SenseOneCycle(void)
 {
     noInterrupts();
-	*sOut &= ~sBit;        // set Send Pin Register low
-	
-	*rReg &= ~rBit;        // set receivePin to input
-	*rOut &= ~rBit;        // set receivePin Register low to make sure pullups are off
-	
-	*rReg |= rBit;         // set pin to OUTPUT - pin is now LOW AND OUTPUT
-	*rReg &= ~rBit;        // set pin to INPUT 
-
-	*sOut |= sBit;         // set send Pin High
+	*sOut &= ~sBit;        // sendPin Register low
+
+	*rReg &= ~rBit;        // receivePin to input
+	*rOut &= ~rBit;        // receivePin Register low to make sure pullups are off
+
+	*rReg |= rBit;         // receivePin to OUTPUT - pin is now LOW AND OUTPUT
+	*rReg &= ~rBit;        // receivePin to INPUT
+
+	*sOut |= sBit;         // sendPin High
     interrupts();
 
 	while ( !(*rIn & rBit)  && (total < CS_Timeout_Millis) ) {  // while receive pin is LOW AND total is positive value
 		total++;
 	}
-    
+	Serial.print("SenseOneCycle(1): ");
+	Serial.println(total);
+
 	if (total > CS_Timeout_Millis) {
 		return -2;         //  total variable over timeout
 	}
-   
-	// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V 
+
+	// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
     noInterrupts();
-	*rOut  |= rBit;        // set receive pin HIGH - turns on pullup 
-	*rReg |= rBit;         // set pin to OUTPUT - pin is now HIGH AND OUTPUT
-	*rReg &= ~rBit;        // set pin to INPUT 
-	*rOut  &= ~rBit;       // turn off pullup
+	*rOut  |= rBit;        // receivePin - turns on pullup
+	*rReg |= rBit;         // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
+	*rReg &= ~rBit;        // receivePin to INPUT
+	*rOut  &= ~rBit;       // receivePin turn off pullup
 
-	*sOut &= ~sBit;        // set send Pin LOW
+	*sOut &= ~sBit;        // sendPin LOW
     interrupts();
 
 	while ( (*rIn & rBit) && (total < CS_Timeout_Millis) ) {  // while receive pin is HIGH  AND total is less than timeout
 		total++;
 	}
-	// Serial.println(total);
+	Serial.print("SenseOneCycle(2): ");
+	Serial.println(total);
 
 	if (total >= CS_Timeout_Millis) {
 		return -2;     // total variable over timeout