Преглед изворни кода

Import CapacitiveSensor version 04

PaulStoffregen пре 11 година
комит
57537a79c2
5 измењених фајлова са 290 додато и 0 уклоњено
  1. 181 0
      CapacitiveSensor.cpp
  2. 51 0
      CapacitiveSensor.h
  3. 13 0
      README.md
  4. 39 0
      examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde
  5. 6 0
      keywords.txt

+ 181 - 0
CapacitiveSensor.cpp

@@ -0,0 +1,181 @@
+/*
+ CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring
+ Copyright (c) 2009 Paul Bagder  All right reserved.
+ Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix
+ vim: set ts=4:
+ */
+
+#if ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#include "pins_arduino.h"
+#include "WConstants.h"
+#endif
+
+#include "CapacitiveSensor.h"
+
+// Constructor /////////////////////////////////////////////////////////////////
+// Function that handles the creation and setup of instances
+
+CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
+{
+	uint8_t sPort, rPort;
+
+	// initialize this instance's variables
+	// 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
+	
+	sBit =  digitalPinToBitMask(sendPin);			// get send pin's ports and bitmask
+	sPort = digitalPinToPort(sendPin);
+	sReg = portModeRegister(sPort);
+	sOut = portOutputRegister(sPort);				// get pointer to output register   
+
+	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
+}
+
+// Public Methods //////////////////////////////////////////////////////////////
+// Functions available in Wiring sketches, this library, and other libraries
+
+long CapacitiveSensor::capacitiveSensor(uint8_t samples)
+{
+	total = 0;
+	if (samples == 0) return 0;
+	if (error < 0) return -1;            // bad pin
+
+
+	for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
+		if (SenseOneCycle() < 0)  return -2;   // variable over timeout
+}
+
+		// only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline
+		// this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal
+
+		if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total  - leastTotal) < (int)(.10 * (float)leastTotal) ) {
+
+			// Serial.println();               // debugging
+			// Serial.println("auto-calibrate");
+			// Serial.println();
+			// delay(2000); */
+
+			leastTotal = 0x0FFFFFFFL;          // reset for "autocalibrate"
+			lastCal = millis();
+		}
+		/*else{                                // debugging 
+			Serial.print("  total =  ");
+			Serial.print(total);
+
+			Serial.print("   leastTotal  =  ");
+			Serial.println(leastTotal);
+
+			Serial.print("total - leastTotal =  ");
+			x = total - leastTotal ;
+			Serial.print(x);
+			Serial.print("     .1 * leastTotal = ");
+			x = (int)(.1 * (float)leastTotal);
+			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         
+	return(total - leastTotal);
+
+}
+
+long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
+{
+	total = 0;
+	if (samples == 0) return 0;
+	if (error < 0) return -1;                  // bad pin - this appears not to work
+
+	for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
+		if (SenseOneCycle() < 0)  return -2;   // variable over timeout
+	}
+
+	return total;
+}
+
+
+void CapacitiveSensor::reset_CS_AutoCal(void){
+	leastTotal = 0x0FFFFFFFL;
+}
+
+void CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){
+	CS_AutocaL_Millis = autoCal_millis;
+}
+
+void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
+	CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000;  // floats to deal with large numbers
+}
+
+// Private Methods /////////////////////////////////////////////////////////////
+// Functions only available to other functions in this library
+
+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
+    interrupts();
+
+	while ( !(*rIn & rBit)  && (total < CS_Timeout_Millis) ) {  // while receive pin is LOW AND total is positive value
+		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 
+    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
+
+	*sOut &= ~sBit;        // set send Pin LOW
+    interrupts();
+
+	while ( (*rIn & rBit) && (total < CS_Timeout_Millis) ) {  // while receive pin is HIGH  AND total is less than timeout
+		total++;
+	}
+	// Serial.println(total);
+
+	if (total >= CS_Timeout_Millis) {
+		return -2;     // total variable over timeout
+	} else {
+		return 1;
+	}
+}

+ 51 - 0
CapacitiveSensor.h

@@ -0,0 +1,51 @@
+/*
+  CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring
+  Copyright (c) 2008 Paul Bagder  All rights reserved.
+  Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix
+  vim: set ts=4:
+*/
+
+// ensure this library description is only included once
+#ifndef CapacitiveSensor_h
+#define CapacitiveSensor_h
+
+#if ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#endif
+
+// library interface description
+class CapacitiveSensor
+{
+  // user-accessible "public" interface
+  public:
+  // methods
+	CapacitiveSensor(uint8_t sendPin, uint8_t receivePin);
+	long capacitiveSensorRaw(uint8_t samples);
+	long capacitiveSensor(uint8_t samples);
+	void set_CS_Timeout_Millis(unsigned long timeout_millis);
+	void reset_CS_AutoCal();
+	void set_CS_AutocaL_Millis(unsigned long autoCal_millis);
+  // library-accessible "private" interface
+  private:
+  // variables
+	int error;
+	unsigned long  leastTotal;
+	unsigned int   loopTimingFactor;
+	unsigned long  CS_Timeout_Millis;
+	unsigned long  CS_AutocaL_Millis;
+	unsigned long  lastCal;
+	unsigned long  total;
+	uint8_t sBit;   // send pin's ports and bitmask
+	volatile uint8_t *sReg;
+	volatile uint8_t *sOut;
+	uint8_t rBit;    // receive pin's ports and bitmask 
+	volatile uint8_t *rReg;
+	volatile uint8_t *rIn;
+	volatile uint8_t *rOut;
+  // methods
+	int SenseOneCycle(void);
+};
+
+#endif

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+#CapacitiveSensor Library#
+
+CapacitiveSensor lets you create sensors that can detect touch or proximity. 
+
+http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
+
+http://playground.arduino.cc/Main/CapacitiveSensor
+
+http://www.youtube.com/watch?v=BHQPqQ_5ulc
+
+CapacitiveSensor was originally written by Paul Badger and is now maintained by Paul Stoffregen.
+
+![CapacitiveSensor Demo](http://www.pjrc.com/teensy/td_libs_CapacitiveSensor_1.jpg)

+ 39 - 0
examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde

@@ -0,0 +1,39 @@
+#include <CapacitiveSensor.h>
+
+/*
+ * CapitiveSense Library Demo Sketch
+ * Paul Badger 2008
+ * Uses a high value resistor e.g. 10M between send pin and receive pin
+ * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
+ * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
+ */
+
+
+CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
+CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
+CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
+
+void setup()                    
+{
+   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
+   Serial.begin(9600);
+}
+
+void loop()                    
+{
+    long start = millis();
+    long total1 =  cs_4_2.capacitiveSensor(30);
+    long total2 =  cs_4_6.capacitiveSensor(30);
+    long total3 =  cs_4_8.capacitiveSensor(30);
+
+    Serial.print(millis() - start);        // check on performance in milliseconds
+    Serial.print("\t");                    // tab character for debug windown spacing
+
+    Serial.print(total1);                  // print sensor output 1
+    Serial.print("\t");
+    Serial.print(total2);                  // print sensor output 2
+    Serial.print("\t");
+    Serial.println(total3);                // print sensor output 3
+
+    delay(10);                             // arbitrary delay to limit data to serial port 
+}

+ 6 - 0
keywords.txt

@@ -0,0 +1,6 @@
+CapacitiveSensor	KEYWORD1
+capacitiveSensorRaw	KEYWORD2
+capacitiveSensor	KEYWORD2
+set_CS_Timeout_Millis	KEYWORD2
+reset_CS_AutoCal	KEYWORD2
+set_CS_AutocaL_Millis	KEYWORD2