From fbdb4b8b83bbf79d73770f4d8a3e4170511ba6e7 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Nov 2016 04:24:48 +0100 Subject: [PATCH] Capacitance fir st commit, on branch cap. piezo_full_fast for the whole natural scale is not complete (notes, test hardware). --- .../capacitance/led_binary/led_binary.ino | 44 ++++ .../led_binary_triple/led_binary_triple.ino | 68 ++++++ .../capacitance/led_piezo/led_piezo.ino | 54 ++++ .../capacitance/led_pwm/led_pwm.ino | 54 ++++ .../piezo_full_fast/piezo_full_fast.ino | 75 ++++++ .../capacitance/piezo_full_fast/pitches.h | 95 ++++++++ .../piezo_triple_fast/piezo_triple_fast.ino | 74 ++++++ .../capacitance/piezo_triple_fast/pitches.h | 95 ++++++++ .../CapacitiveSensor/CapacitiveSensor.cpp | 186 ++++++++++++++ libraries/CapacitiveSensor/CapacitiveSensor.h | 230 ++++++++++++++++++ libraries/CapacitiveSensor/README.md | 13 + .../CapacitiveSensorSketch.pde | 39 +++ libraries/CapacitiveSensor/keywords.txt | 6 + libraries/CapacitiveSensor/library.properties | 10 + 14 files changed, 1043 insertions(+) create mode 100644 advanced_projects/capacitance/led_binary/led_binary.ino create mode 100644 advanced_projects/capacitance/led_binary_triple/led_binary_triple.ino create mode 100644 advanced_projects/capacitance/led_piezo/led_piezo.ino create mode 100644 advanced_projects/capacitance/led_pwm/led_pwm.ino create mode 100644 advanced_projects/capacitance/piezo_full_fast/piezo_full_fast.ino create mode 100644 advanced_projects/capacitance/piezo_full_fast/pitches.h create mode 100644 advanced_projects/capacitance/piezo_triple_fast/piezo_triple_fast.ino create mode 100644 advanced_projects/capacitance/piezo_triple_fast/pitches.h create mode 100644 libraries/CapacitiveSensor/CapacitiveSensor.cpp create mode 100644 libraries/CapacitiveSensor/CapacitiveSensor.h create mode 100644 libraries/CapacitiveSensor/README.md create mode 100644 libraries/CapacitiveSensor/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde create mode 100644 libraries/CapacitiveSensor/keywords.txt create mode 100644 libraries/CapacitiveSensor/library.properties diff --git a/advanced_projects/capacitance/led_binary/led_binary.ino b/advanced_projects/capacitance/led_binary/led_binary.ino new file mode 100644 index 0000000..0cd3cb1 --- /dev/null +++ b/advanced_projects/capacitance/led_binary/led_binary.ino @@ -0,0 +1,44 @@ +#include + +/* + * 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 +// pin 8 is sensor pin, add a wire and or foil +const int led = 13; // pin that the Piezo is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value +void setup() +{ + cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + Serial.begin(9600); + pinMode(led, OUTPUT); +} + +void loop() +{ + long start = millis(); + long total1 = cs_4_2.capacitiveSensor(30); + + if (total1 > (sensorMax / 2 - sensorMin)) { + digitalWrite(led, HIGH); + delay(100); + } + else { + digitalWrite(led, LOW); + delay(100); + } + + Serial.print(millis() - start); // check on performance in milliseconds + Serial.print("\t Capacitance: "); // tab character for debug windown spacing + + Serial.println(total1); // print sensor output 1 + delay(1); // arbitrary delay to limit data to serial port + +} diff --git a/advanced_projects/capacitance/led_binary_triple/led_binary_triple.ino b/advanced_projects/capacitance/led_binary_triple/led_binary_triple.ino new file mode 100644 index 0000000..4a09d59 --- /dev/null +++ b/advanced_projects/capacitance/led_binary_triple/led_binary_triple.ino @@ -0,0 +1,68 @@ +#include + +/* + * 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 + * + * Schema: http://i.stack.imgur.com/FMI00.png + */ + + + + +CapacitiveSensor cs_2_3 = CapacitiveSensor(2, 3); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired +CapacitiveSensor cs_2_4 = CapacitiveSensor(2, 4); +CapacitiveSensor cs_2_5 = CapacitiveSensor(2, 5); + + + + +const int led = 13; // pin that the Led is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value +void setup() +{ + cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + Serial.begin(9600); + pinMode(led, OUTPUT); +} + +void loop() +{ + long start = millis(); + long total1 = cs_2_3.capacitiveSensor(30); + long total2 = cs_2_4.capacitiveSensor(30); + long total3 = cs_2_5.capacitiveSensor(30); + + if (total1 > (sensorMax / 2 - sensorMin)) { + digitalWrite(led, HIGH); + Serial.println("Primo"); + } + + + if (total2 > (sensorMax / 2 - sensorMin)) { + digitalWrite(led, HIGH); + Serial.println("Secondo"); + } + + if (total3 > (sensorMax / 2 - sensorMin)) { + digitalWrite(led, HIGH); + Serial.println("terzo"); + } + + Serial.print(millis() - start); // check on performance in milliseconds + Serial.print("\t Capacitance 1: "); // tab character for debug windown spacing + + Serial.print(total1); // print sensor output 1 + Serial.print("\t 2: "); // tab character for debug windown spacing + + Serial.print(total2); // print sensor output 1 + Serial.print("\t 3: "); // tab character for debug windown spacing + Serial.println(total3); // print sensor output 1 + + delay(1); // arbitrary delay to limit data to serial port +} + diff --git a/advanced_projects/capacitance/led_piezo/led_piezo.ino b/advanced_projects/capacitance/led_piezo/led_piezo.ino new file mode 100644 index 0000000..402a752 --- /dev/null +++ b/advanced_projects/capacitance/led_piezo/led_piezo.ino @@ -0,0 +1,54 @@ +#include + +/* + * 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 +// pin 8 is sensor pin, add a wire and or foil +const int piezo = 9; // pin that the Piezo is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value +void setup() +{ + cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + Serial.begin(9600); + pinMode(piezo, OUTPUT); +} + +void loop() +{ + long start = millis(); + long total1 = smoothRead(cs_4_2.capacitiveSensor(30)); + + int value = map(total1, sensorMin, sensorMax, 220, 3500); + + value = constrain(value, 220, 3500); + tone(piezo, value, 10); + + Serial.print(millis() - start); // check on performance in milliseconds + Serial.print("\t Capacitance: "); // tab character for debug windown spacing + + Serial.print(total1); // print sensor output 1 + Serial.print(" \t Pitch: "); + Serial.println(value); + delay(1); // arbitrary delay to limit data to serial port + +} + +// Funzioni + +int smoothRead(int value) { +// Legge 10 valori dal sensore e ritorna il valore medio tra questi. + int total = 0; + for (int i = 0; i < 10; i++) { + total = total + value; + } + return(total / 10); +} + diff --git a/advanced_projects/capacitance/led_pwm/led_pwm.ino b/advanced_projects/capacitance/led_pwm/led_pwm.ino new file mode 100644 index 0000000..29e1476 --- /dev/null +++ b/advanced_projects/capacitance/led_pwm/led_pwm.ino @@ -0,0 +1,54 @@ +#include + +/* + * 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 +// pin 8 is sensor pin, add a wire and or foil +const int led = 9; // pin that the Piezo is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value +void setup() +{ + cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + Serial.begin(9600); + pinMode(led, OUTPUT); +} + +void loop() +{ + long start = millis(); + long total1 = smoothRead(cs_4_2.capacitiveSensor(30)); + + int value = map(total1, sensorMin, sensorMax, 0, 255); + + value = constrain(value, 0, 255); + analogWrite(led, value); + + Serial.print(millis() - start); // check on performance in milliseconds + Serial.print("\t Capacitance: "); // tab character for debug windown spacing + + Serial.print(total1); // print sensor output 1 + Serial.print(" \t Luminosita: "); + Serial.println(value); + delay(1); // arbitrary delay to limit data to serial port + +} + +// Funzioni + +int smoothRead(int value) { +// Legge 10 valori dal sensore e ritorna il valore medio tra questi. + int total = 0; + for (int i = 0; i < 10; i++) { + total = total + value; + } + return(total / 10); +} + diff --git a/advanced_projects/capacitance/piezo_full_fast/piezo_full_fast.ino b/advanced_projects/capacitance/piezo_full_fast/piezo_full_fast.ino new file mode 100644 index 0000000..6bd50db --- /dev/null +++ b/advanced_projects/capacitance/piezo_full_fast/piezo_full_fast.ino @@ -0,0 +1,75 @@ +#include +#include "pitches.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 + * + * Schema: http://i.stack.imgur.com/FMI00.png + */ + + + + +CapacitiveSensor cs_2_3 = CapacitiveSensor(2, 3); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired +CapacitiveSensor cs_2_4 = CapacitiveSensor(2, 4); +CapacitiveSensor cs_2_5 = CapacitiveSensor(2, 5); +CapacitiveSensor cs_2_6 = CapacitiveSensor(2, 6); +CapacitiveSensor cs_2_7 = CapacitiveSensor(2, 7); +CapacitiveSensor cs_2_8 = CapacitiveSensor(2, 8); +CapacitiveSensor cs_2_9 = CapacitiveSensor(2, 9); +CapacitiveSensor cs_2_10 = CapacitiveSensor(2, 10); +const int led = 13; // pin that the Led is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value + +// Carica un file di esempio con tutte le note + +int notes[] = {NOTE_C4, NOTE_E4, NOTE_G5 }; // suona una prima, quinta, ottava + + + +const int piezo_pin = 11; + + +void setup() +{ + cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + Serial.begin(9600); // Disable Serial to make it fastw + pinMode(led, OUTPUT); + pinMode(piezo_pin, OUTPUT); +} + +void loop() +{ + long total[] = { + cs_2_3.capacitiveSensor(30), + cs_2_4.capacitiveSensor(30), + cs_2_5.capacitiveSensor(30), + cs_2_6.capacitiveSensor(30), + cs_2_7.capacitiveSensor(30), + cs_2_8.capacitiveSensor(30), + cs_2_9.capacitiveSensor(30), + cs_2_10.capacitiveSensor(30) + }; + + +// long total[1] = cs_2_4.capacitiveSensor(30); +// long total[2] = cs_2_5.capacitiveSensor(30); +// long total[3] = cs_2_6.capacitiveSensor(30); +// long total[4] = cs_2_7.capacitiveSensor(30); +// long total[5] = cs_2_8.capacitiveSensor(30); +// long total[6] = cs_2_9.capacitiveSensor(30); +// long total[7] = cs_2_10.capacitiveSensor(30); + +for (int thisSensor =0; thisSensor < 8; thisSensor++) { + if (total[thisSensor] > (sensorMax / 2 - sensorMin)) { + tone(piezo_pin, notes[thisSensor], 50); // Notes array is translated + Serial.println(thisSensor); + } +} + +} + diff --git a/advanced_projects/capacitance/piezo_full_fast/pitches.h b/advanced_projects/capacitance/piezo_full_fast/pitches.h new file mode 100644 index 0000000..55c7d54 --- /dev/null +++ b/advanced_projects/capacitance/piezo_full_fast/pitches.h @@ -0,0 +1,95 @@ +/************************************************* + * Public Constants + *************************************************/ + +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 + + diff --git a/advanced_projects/capacitance/piezo_triple_fast/piezo_triple_fast.ino b/advanced_projects/capacitance/piezo_triple_fast/piezo_triple_fast.ino new file mode 100644 index 0000000..7e5e17a --- /dev/null +++ b/advanced_projects/capacitance/piezo_triple_fast/piezo_triple_fast.ino @@ -0,0 +1,74 @@ +#include +#include "pitches.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 + * + * Schema: http://i.stack.imgur.com/FMI00.png + */ + + + + +CapacitiveSensor cs_2_3 = CapacitiveSensor(2, 3); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired +CapacitiveSensor cs_2_4 = CapacitiveSensor(2, 4); +CapacitiveSensor cs_2_5 = CapacitiveSensor(2, 5); + +const int led = 13; // pin that the Led is attached to +int sensorMin = 50; // minimum sensor value +int sensorMax = 3200; // maximum sensor value + +// Carica un file di esempio con tutte le note + + int notes[] = {NOTE_C4, NOTE_E4,NOTE_G5 }; // suona una prima, quinta, ottava + + + +const int piezo_pin = 9; + + +void setup() +{ + cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example + //Serial.begin(9600); // Disable Serial to make it fastw + pinMode(led, OUTPUT); + pinMode(piezo_pin, OUTPUT); +} + +void loop() +{ + long start = millis(); + long total1 = cs_2_3.capacitiveSensor(30); + long total2 = cs_2_4.capacitiveSensor(30); + long total3 = cs_2_5.capacitiveSensor(30); + + if (total1 > (sensorMax / 2 - sensorMin)) { + tone(piezo_pin, notes[0], 50); // Notes array is translated + Serial.println("Primo"); + } + + if (total2 > (sensorMax / 2 - sensorMin)) { + tone(piezo_pin, notes[1], 50); // Notes array is translated + Serial.println("Secondo"); + } + + if (total3 > (sensorMax / 2 - sensorMin)) { + tone(piezo_pin, notes[2], 50); // Notes array is translated + Serial.println("terzo"); + } + + Serial.print(millis() - start); // check on performance in milliseconds + Serial.print("\t Capacitance 1: "); // tab character for debug windown spacing + + Serial.print(total1); // print sensor output 1 + Serial.print("\t 2: "); // tab character for debug windown spacing + + Serial.print(total2); // print sensor output 1 + Serial.print("\t 3: "); // tab character for debug windown spacing + Serial.println(total3); // print sensor output 1 + +} + diff --git a/advanced_projects/capacitance/piezo_triple_fast/pitches.h b/advanced_projects/capacitance/piezo_triple_fast/pitches.h new file mode 100644 index 0000000..55c7d54 --- /dev/null +++ b/advanced_projects/capacitance/piezo_triple_fast/pitches.h @@ -0,0 +1,95 @@ +/************************************************* + * Public Constants + *************************************************/ + +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 + + diff --git a/libraries/CapacitiveSensor/CapacitiveSensor.cpp b/libraries/CapacitiveSensor/CapacitiveSensor.cpp new file mode 100644 index 0000000..fc983fe --- /dev/null +++ b/libraries/CapacitiveSensor/CapacitiveSensor.cpp @@ -0,0 +1,186 @@ +/* + CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring + https://github.com/PaulStoffregen/CapacitiveSensor + http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html + http://playground.arduino.cc/Main/CapacitiveSensor + Copyright (c) 2009 Paul Bagder All right reserved. + Version 05 by Paul Stoffregen - Support non-AVR board: Teensy 3.x, Arduino Due + 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) +{ + // 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 + + pinMode(sendPin, OUTPUT); // sendpin to OUTPUT + pinMode(receivePin, INPUT); // receivePin to INPUT + digitalWrite(sendPin, LOW); + + sBit = PIN_TO_BITMASK(sendPin); // get send pin's ports and bitmask + sReg = PIN_TO_BASEREG(sendPin); // get pointer to output register + + rBit = PIN_TO_BITMASK(receivePin); // get receive pin's ports and bitmask + rReg = PIN_TO_BASEREG(receivePin); + + // get pin mapping and port for receive Pin - from digital pin functions in Wiring.c + 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(); + DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low + DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off) + DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT + DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT + delayMicroseconds(10); + DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off) + DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High + interrupts(); + + while ( !DIRECT_READ(rReg, 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 + noInterrupts(); + DIRECT_WRITE_HIGH(rReg, rBit); + DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT + DIRECT_WRITE_HIGH(rReg, rBit); + DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off) + DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW + interrupts(); + +#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND + DIRECT_MODE_OUTPUT(rReg, rBit); + DIRECT_WRITE_LOW(rReg, rBit); + delayMicroseconds(10); + DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off) +#else + while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout + total++; + } +#endif + //Serial.print("SenseOneCycle(2): "); + //Serial.println(total); + + if (total >= CS_Timeout_Millis) { + return -2; // total variable over timeout + } else { + return 1; + } +} diff --git a/libraries/CapacitiveSensor/CapacitiveSensor.h b/libraries/CapacitiveSensor/CapacitiveSensor.h new file mode 100644 index 0000000..4604fc1 --- /dev/null +++ b/libraries/CapacitiveSensor/CapacitiveSensor.h @@ -0,0 +1,230 @@ +/* + CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring + https://github.com/PaulStoffregen/CapacitiveSensor + http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html + http://playground.arduino.cc/Main/CapacitiveSensor + Copyright (c) 2008 Paul Bagder All rights reserved. + Version 05 by Paul Stoffregen - Support non-AVR board: Teensy 3.x, Arduino Due + 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 + +// Direct I/O through registers and bitmask (from OneWire library) + +#if defined(__AVR__) +#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask), (*((base)+2)) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (1) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (*((base)+512)) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) + +#elif defined(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) + +#elif defined(__SAM3X8E__) +#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask)) + +#elif defined(__PIC32MX__) +#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 +#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08 +#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04 +#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24 +#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 + +#elif defined(ARDUINO_ARCH_ESP8266) +#define PIN_TO_BASEREG(pin) (portOutputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*(base+6)) & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS +#define DIRECT_MODE_INPUT(base, mask) ((*(base+5)) = (mask)) //GPIO_ENABLE_W1TC_ADDRESS +#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+4)) = (mask)) //GPIO_ENABLE_W1TS_ADDRESS +#define DIRECT_WRITE_LOW(base, mask) ((*(base+2)) = (mask)) //GPIO_OUT_W1TC_ADDRESS +#define DIRECT_WRITE_HIGH(base, mask) ((*(base+1)) = (mask)) //GPIO_OUT_W1TS_ADDRESS + +#elif defined(__SAMD21G18A__) +// runs extremely slow/unreliable on Arduino Zero - help wanted.... +#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) + +#elif defined(RBL_NRF51822) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) + +#elif defined(__arc__) + +#include "scss_registers.h" +#include "portable.h" +#include "avr/pgmspace.h" + +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 + +/* GPIO registers base address */ +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); +} + +static inline __attribute__((always_inline)) +void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } +} + +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) + +#endif + +// some 3.3V chips with 5V tolerant pins need this workaround +// +#if defined(__MK20DX256__) +#define FIVE_VOLT_TOLERANCE_WORKAROUND +#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; + IO_REG_TYPE sBit; // send pin's ports and bitmask + volatile IO_REG_TYPE *sReg; + IO_REG_TYPE rBit; // receive pin's ports and bitmask + volatile IO_REG_TYPE *rReg; + // methods + int SenseOneCycle(void); +}; + +#endif diff --git a/libraries/CapacitiveSensor/README.md b/libraries/CapacitiveSensor/README.md new file mode 100644 index 0000000..df45348 --- /dev/null +++ b/libraries/CapacitiveSensor/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) diff --git a/libraries/CapacitiveSensor/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde b/libraries/CapacitiveSensor/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde new file mode 100644 index 0000000..a036612 --- /dev/null +++ b/libraries/CapacitiveSensor/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde @@ -0,0 +1,39 @@ +#include + +/* + * 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 +} diff --git a/libraries/CapacitiveSensor/keywords.txt b/libraries/CapacitiveSensor/keywords.txt new file mode 100644 index 0000000..1933303 --- /dev/null +++ b/libraries/CapacitiveSensor/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 diff --git a/libraries/CapacitiveSensor/library.properties b/libraries/CapacitiveSensor/library.properties new file mode 100644 index 0000000..35a4a4a --- /dev/null +++ b/libraries/CapacitiveSensor/library.properties @@ -0,0 +1,10 @@ +name=CapacitiveSensor +version=0.5.1 +author=Paul Bagder, Paul Stoffregen +maintainer=Paul Stoffregen +sentence=Create capacitive sensors that can detect touch or proximity. +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. +category=Sensors +url=http://playground.arduino.cc/Main/CapacitiveSensor +architectures=* + -- 2.39.2