From 693ed9806b7c4b8073ce210adb944ce8e9b15bdb Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 31 Jul 2016 23:50:10 +0800 Subject: [PATCH] Initial Commit --- CapacitiveButton.cpp | 37 +++++++++++++++++++ CapacitiveButton.h | 20 ++++++++++ .../CapacitiveButtonExample.ino | 17 +++++++++ library.properties | 9 +++++ 4 files changed, 83 insertions(+) create mode 100644 CapacitiveButton.cpp create mode 100644 CapacitiveButton.h create mode 100644 examples/CapacitiveButtonExample/CapacitiveButtonExample.ino create mode 100644 library.properties diff --git a/CapacitiveButton.cpp b/CapacitiveButton.cpp new file mode 100644 index 0000000..54c9908 --- /dev/null +++ b/CapacitiveButton.cpp @@ -0,0 +1,37 @@ +#include "CapacitiveButton.h" + +// This is a fast capacitive touch library that aims to have low response time for activation + +CapacitiveButton::CapacitiveButton(uint8_t sendPin, uint8_t receivePin, uint16_t nthreshold) : sensor(sendPin, receivePin) { + threshold = nthreshold; +} + +bool CapacitiveButton::getState() { + return state; +} + +uint16_t CapacitiveButton::getRaw() { + return average; +} + +bool CapacitiveButton::update() { + uint16_t reading = sensor.capacitiveSensor(1); + uint16_t last_cycletime = millis() - last_time; + last_cycletime += last_cycletime ? 0 : 1; // prevent divide by zero + last_time = millis(); + // average = (reading + average * SMOOTHING_CYCLES) / (SMOOTHING_CYCLES + 1); + float cycletime_factor = SMOOTHING_TIME / (float) last_cycletime; + average = (reading + average * cycletime_factor) / (cycletime_factor + 1); + if (state != (average > threshold)) { // if state of button has changed + if (!state) { // if new state is active + average += TIMEOUT_FACTOR * threshold; + state = true; + } + else { + state = false; + } + return true; + } + return false; +} + diff --git a/CapacitiveButton.h b/CapacitiveButton.h new file mode 100644 index 0000000..f0e5c09 --- /dev/null +++ b/CapacitiveButton.h @@ -0,0 +1,20 @@ +#include + +//#define SMOOTHING_CYCLES 10 // increase to reduce activation errors +#define SMOOTHING_TIME 16 // smoothing time in milliseconds +#define TIMEOUT_FACTOR 100 // increase to 600 for reduced repeated touches + +class CapacitiveButton { + public: + CapacitiveButton(uint8_t sendPin, uint8_t receivePin, uint16_t threshold = 40); + bool getState(); + uint16_t getRaw(); + bool update(); // returns true when state has changed + private: + CapacitiveSensor sensor; + uint16_t threshold = 40; + bool state = false; + uint16_t average = 0; + uint16_t last_time = 0; +}; + diff --git a/examples/CapacitiveButtonExample/CapacitiveButtonExample.ino b/examples/CapacitiveButtonExample/CapacitiveButtonExample.ino new file mode 100644 index 0000000..00deb34 --- /dev/null +++ b/examples/CapacitiveButtonExample/CapacitiveButtonExample.ino @@ -0,0 +1,17 @@ +#include "CapacitiveButton.h" + +CapacitiveButton btn1 = CapacitiveButton(2, 5); +CapacitiveButton btn2 = CapacitiveButton(2, 6); + +void setup() { + Serial.begin(9600); +} + +void loop() { + if (btn1.update() || btn2.update()) { + Serial.print(btn1.getState()); + Serial.print("\t"); + Serial.println(btn2.getState()); + } + delay(1); // if the looptime is less than one ms, fluctuations will be more sensetive +} \ No newline at end of file diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..66083be --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=CapacitiveButton +version=0.0.1 +author=Ambrose Chua +maintainer=Ambrose Chua +sentence=CapacitiveSensor wrapper for capacitive buttons. +paragraph=CapacitiveButton is a small wrapper around CapacitiveSensor that filters the raw capacitive sensor data for use as a button. Requires CapacitiveSensor to be installed. +category=Sensors +url=http://playground.arduino.cc/Main/CapacitiveSensor +architectures=* \ No newline at end of file