Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

Commit d48fbc5

Browse files
ridhaosWi6labsVVESTM
authored andcommitted
Add Capacitive sensor library
Add from https://github.com/stm32duino/Arduino_Core_STM32 version 1.2.0.
1 parent 4693207 commit d48fbc5

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
13+
#include "Arduino.h"
14+
#include "CapacitiveSensor.h"
15+
16+
// Constructor /////////////////////////////////////////////////////////////////
17+
// Function that handles the creation and setup of instances
18+
19+
CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
20+
{
21+
// initialize this instance's variables
22+
// Serial.begin(9600); // for debugging
23+
error = 1;
24+
loopTimingFactor = 310; // determined empirically - a hack
25+
26+
CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
27+
CS_AutocaL_Millis = 20000;
28+
29+
// get pin mapping and port for send Pin - from PinMode function in core
30+
if ((sendPin >= NUM_DIGITAL_PINS) || (receivePin >= NUM_DIGITAL_PINS)) {
31+
error = -1;
32+
return;
33+
}
34+
35+
rPin = receivePin;
36+
sPin = sendPin;
37+
38+
pinMode(sPin, OUTPUT); // sendpin to OUTPUT
39+
pinMode(rPin, INPUT); // receivePin to INPUT
40+
digitalWrite(sPin, LOW);
41+
42+
// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
43+
leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin
44+
lastCal = millis(); // set millis for start
45+
}
46+
47+
// Public Methods //////////////////////////////////////////////////////////////
48+
// Functions available in Wiring sketches, this library, and other libraries
49+
50+
long CapacitiveSensor::capacitiveSensor(uint8_t samples)
51+
{
52+
total = 0;
53+
if (samples == 0) return 0;
54+
if (error < 0) return -1; // bad pin
55+
56+
57+
for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter
58+
if (SenseOneCycle() < 0) return -2; // variable over timeout
59+
}
60+
61+
// only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline
62+
// this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal
63+
64+
if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total - leastTotal) < (int)(.10 * (float)leastTotal) ) {
65+
66+
// Serial.println(); // debugging
67+
// Serial.println("auto-calibrate");
68+
// Serial.println();
69+
// delay(2000); */
70+
71+
leastTotal = 0x0FFFFFFFL; // reset for "autocalibrate"
72+
lastCal = millis();
73+
}
74+
/*else{ // debugging
75+
Serial.print(" total = ");
76+
Serial.print(total);
77+
78+
Serial.print(" leastTotal = ");
79+
Serial.println(leastTotal);
80+
81+
Serial.print("total - leastTotal = ");
82+
x = total - leastTotal ;
83+
Serial.print(x);
84+
Serial.print(" .1 * leastTotal = ");
85+
x = (int)(.1 * (float)leastTotal);
86+
Serial.println(x);
87+
} */
88+
89+
// routine to subtract baseline (non-sensed capacitance) from sensor return
90+
if (total < leastTotal) leastTotal = total; // set floor value to subtract from sensed value
91+
return(total - leastTotal);
92+
93+
}
94+
95+
long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
96+
{
97+
total = 0;
98+
if (samples == 0) return 0;
99+
if (error < 0) return -1; // bad pin - this appears not to work
100+
101+
for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter
102+
if (SenseOneCycle() < 0) return -2; // variable over timeout
103+
}
104+
105+
return total;
106+
}
107+
108+
109+
void CapacitiveSensor::reset_CS_AutoCal(void){
110+
leastTotal = 0x0FFFFFFFL;
111+
}
112+
113+
void CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){
114+
CS_AutocaL_Millis = autoCal_millis;
115+
}
116+
117+
void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
118+
CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000; // floats to deal with large numbers
119+
}
120+
121+
// Private Methods /////////////////////////////////////////////////////////////
122+
// Functions only available to other functions in this library
123+
124+
int CapacitiveSensor::SenseOneCycle(void)
125+
{
126+
//noInterrupts();
127+
digitalWrite(sPin, LOW);// sendPin Register low
128+
pinMode(rPin, INPUT);// receivePin to input (pullups are off)
129+
pinMode(rPin, OUTPUT);// receivePin to OUTPUT
130+
digitalWrite(rPin, LOW);// pin is now LOW AND OUTPUT
131+
delayMicroseconds(10);
132+
pinMode(rPin, INPUT);// receivePin to input (pullups are off)
133+
digitalWrite(sPin, HIGH);// sendPin High
134+
//interrupts();
135+
136+
while ( !digitalRead(rPin) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
137+
total++;
138+
}
139+
//Serial.print("SenseOneCycle(1): ");
140+
//Serial.println(total);
141+
142+
if (total > CS_Timeout_Millis) {
143+
return -2; // total variable over timeout
144+
}
145+
146+
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
147+
//noInterrupts();
148+
//pinMode(receivePin, INPUT_PULLUP);
149+
pinMode(rPin, OUTPUT); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
150+
digitalWrite(rPin, HIGH);
151+
pinMode(rPin, INPUT);// receivePin to INPUT (pullup is off)
152+
digitalWrite(sPin, LOW);// sendPin LOW
153+
//interrupts();
154+
155+
while ( digitalRead(rPin) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
156+
total++;
157+
}
158+
//Serial.print("SenseOneCycle(2): ");
159+
//Serial.println(total);
160+
161+
if (total >= CS_Timeout_Millis) {
162+
return -2; // total variable over timeout
163+
} else {
164+
return 1;
165+
}
166+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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) 2008 Paul Bagder All rights 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+
// ensure this library description is only included once
13+
#ifndef CapacitiveSensor_h
14+
#define CapacitiveSensor_h
15+
16+
#include "Arduino.h"
17+
18+
// library interface description
19+
class CapacitiveSensor
20+
{
21+
// user-accessible "public" interface
22+
public:
23+
// methods
24+
CapacitiveSensor(uint8_t sendPin, uint8_t receivePin);
25+
long capacitiveSensorRaw(uint8_t samples);
26+
long capacitiveSensor(uint8_t samples);
27+
void set_CS_Timeout_Millis(unsigned long timeout_millis);
28+
void reset_CS_AutoCal();
29+
void set_CS_AutocaL_Millis(unsigned long autoCal_millis);
30+
// library-accessible "private" interface
31+
private:
32+
// variables
33+
int error;
34+
unsigned long leastTotal;
35+
unsigned int loopTimingFactor;
36+
unsigned long CS_Timeout_Millis;
37+
unsigned long CS_AutocaL_Millis;
38+
unsigned long lastCal;
39+
unsigned long total;
40+
//IO_REG_TYPE sBit; // send pin's ports and bitmask
41+
//volatile IO_REG_TYPE *sReg;
42+
//IO_REG_TYPE rBit; // receive pin's ports and bitmask
43+
//volatile IO_REG_TYPE *rReg;
44+
uint32_t rPin;
45+
uint32_t sPin;
46+
// methods
47+
int SenseOneCycle(void);
48+
};
49+
50+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#CapacitiveSensor Library#
2+
3+
CapacitiveSensor lets you create sensors that can detect touch or proximity.
4+
5+
http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
6+
7+
http://playground.arduino.cc/Main/CapacitiveSensor
8+
9+
http://www.youtube.com/watch?v=BHQPqQ_5ulc
10+
11+
CapacitiveSensor was originally written by Paul Badger and is now maintained by Paul Stoffregen.
12+
13+
![CapacitiveSensor Demo](http://www.pjrc.com/teensy/td_libs_CapacitiveSensor_1.jpg)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <CapacitiveSensor.h>
2+
3+
/*
4+
* CapitiveSense Library Demo Sketch
5+
* Paul Badger 2008
6+
* Uses a high value resistor e.g. 10M between send pin and receive pin
7+
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
8+
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
9+
*/
10+
11+
12+
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
13+
CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
14+
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
15+
16+
void setup()
17+
{
18+
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
19+
Serial.begin(9600);
20+
}
21+
22+
void loop()
23+
{
24+
long start = millis();
25+
long total1 = cs_4_2.capacitiveSensor(30);
26+
long total2 = cs_4_6.capacitiveSensor(30);
27+
long total3 = cs_4_8.capacitiveSensor(30);
28+
29+
Serial.print(millis() - start); // check on performance in milliseconds
30+
Serial.print("\t"); // tab character for debug windown spacing
31+
32+
Serial.print(total1); // print sensor output 1
33+
Serial.print("\t");
34+
Serial.print(total2); // print sensor output 2
35+
Serial.print("\t");
36+
Serial.println(total3); // print sensor output 3
37+
38+
delay(10); // arbitrary delay to limit data to serial port
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CapacitiveSensor KEYWORD1
2+
capacitiveSensorRaw KEYWORD2
3+
capacitiveSensor KEYWORD2
4+
set_CS_Timeout_Millis KEYWORD2
5+
reset_CS_AutoCal KEYWORD2
6+
set_CS_AutocaL_Millis KEYWORD2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=CapacitiveSensor
2+
version=0.5.1
3+
author=Paul Bagder, Paul Stoffregen
4+
maintainer=Paul Stoffregen
5+
sentence=Create capacitive sensors that can detect touch or proximity.
6+
paragraph=The capacitiveSensor library turns two or more Arduino pins into a capacitive sensor, which can sense the electrical capacitance of the human body. All the sensor setup requires is a medium to high value resistor and a piece of wire and a small (to large) piece of aluminum foil on the end. At its most sensitive, the sensor will start to sense a hand or body inches away from the sensor.
7+
category=Sensors
8+
url=http://playground.arduino.cc/Main/CapacitiveSensor
9+
architectures=stm32

0 commit comments

Comments
 (0)