Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
D

DoccY

@DoccY
About
Posts
8
Topics
0
Shares
0
Groups
0
Followers
1
Following
0

Posts

Recent Best Controversial

  • Led dimmer with touch sensor
    D DoccY

    Dear all,

    first of all, many thanks for this thread which helps me a lot.

    I know this post is old, but I give you my contribution in return.

    I completely rewrite the code to make it modular and more reusable.

    I wrote 3 classes (Dimmer, DimmerDriver an extendedCapacitiveSensor) and a two sketches for Arduino (one without MySensors and one with it).

    In order to compile the scketches, all files must be saved either in the same directory or in a directory listed in the ARDUINO_LIB_PATH or in the USER_LIB_PATH environnement variable.

    Have fun.
    DoccY.

    PS since the posts are limited in length and I can't upload files due to forum policy, I will provide the files in the next messages.

    Development

  • Led dimmer with touch sensor
    D DoccY

    File 'extendedCapacitiveSensor.cpp'

    /******************************************************************************
    *                                                                             *
    *  Copyright © 2018 -- DoccY's productions                                    *
    *                                                                             *
    *  Auteurs/Authors: DoccY <doccy@mancheron.fr>                                *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  Ce fichier  étend  la classe 'CapacitiveSensor'  et permet  de lisser les  *
    *  résultats renvoyés par le capteur sur un échantillon de mesures.           *
    *                                                                             *
    *  Ce logiciel est régi par la licence CeCILL-C soumise au droit français et  *
    *  respectant les principes  de diffusion des logiciels libres.  Vous pouvez  *
    *  utiliser, modifier et/ou redistribuer ce programme sous les conditions de  *
    *  la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA sur  *
    *  le site "http://www.cecill.info".                                          *
    *                                                                             *
    *  En contrepartie de l'accessibilité au code source et des droits de copie,  *
    *  de modification et de redistribution accordés par cette licence, il n'est  *
    *  offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,  *
    *  seule une responsabilité  restreinte pèse  sur l'auteur du programme,  le  *
    *  titulaire des droits patrimoniaux et les concédants successifs.            *
    *                                                                             *
    *  À  cet égard  l'attention de  l'utilisateur est  attirée sur  les risques  *
    *  associés  au chargement,  à  l'utilisation,  à  la modification  et/ou au  *
    *  développement  et à la reproduction du  logiciel par  l'utilisateur étant  *
    *  donné  sa spécificité  de logiciel libre,  qui peut le rendre  complexe à  *
    *  manipuler et qui le réserve donc à des développeurs et des professionnels  *
    *  avertis  possédant  des  connaissances  informatiques  approfondies.  Les  *
    *  utilisateurs  sont donc  invités  à  charger  et  tester  l'adéquation du  *
    *  logiciel  à leurs besoins  dans des conditions  permettant  d'assurer  la  *
    *  sécurité de leurs systêmes et ou de leurs données et,  plus généralement,  *
    *  à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.         *
    *                                                                             *
    *  Le fait  que vous puissiez accéder  à cet en-tête signifie  que vous avez  *
    *  pris connaissance de la licence CeCILL-C, et que vous en avez accepté les  *
    *  termes.                                                                    *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  This File extends the 'CapacitiveSensor' class and allows to smooth the    *
    *  results returned by the sensor on a record sample.                         *
    *                                                                             *
    *  The Gk-arrays-MPI library aims at indexing k-factors from a huge set of    *
    *  sequencing reads generated by High Throughput Sequencing by using the MPI  *
    *  protocol, which is implemented by OpenMPI.                                 *
    *                                                                             *
    *  This software is governed by the CeCILL-C license under French law and     *
    *  abiding by the rules of distribution of free software. You can use,        *
    *  modify and/ or redistribute the software under the terms of the CeCILL-C   *
    *  license as circulated by CEA, CNRS and INRIA at the following URL          *
    *  "http://www.cecill.info".                                                  *
    *                                                                             *
    *  As a counterpart to the access to the source code and rights to copy,      *
    *  modify and redistribute granted by the license, users are provided only    *
    *  with a limited warranty and the software's author, the holder of the       *
    *  economic rights, and the successive licensors have only limited            *
    *  liability.                                                                 *
    *                                                                             *
    *  In this respect, the user's attention is drawn to the risks associated     *
    *  with loading, using, modifying and/or developing or reproducing the        *
    *  software by the user in light of its specific status of free software,     *
    *  that may mean that it is complicated to manipulate, and that also          *
    *  therefore means that it is reserved for developers and experienced         *
    *  professionals having in-depth computer knowledge. Users are therefore      *
    *  encouraged to load and test the software's suitability as regards their    *
    *  requirements in conditions enabling the security of their systems and/or   *
    *  data to be ensured and, more generally, to use and operate it in the same  *
    *  conditions as regards security.                                            *
    *                                                                             *
    *  The fact that you are presently reading this means that you have had       *
    *  knowledge of the CeCILL-C license and that you accept its terms.           *
    *                                                                             *
    ******************************************************************************/
    
    /*
     * The functionnalities of this extension is *completely* inspired by
     * the code given at:
     * https://forum.mysensors.org/topic/2510/led-dimmer-with-touch-sensor/3
     */
    
    #include "extendedCapacitiveSensor.h"
    
    #ifdef DEBUG
    #  define DBG(msg) if (Serial) Serial.print(msg)
    #  define DBGln(msg) if (Serial) Serial.println(msg)
    #else
    #  define DBG(msg) (void) 0
    #  define DBGln(msg) (void) 0
    #endif
    
    ExtendedCapacitiveSensor::ExtendedCapacitiveSensor(byte send_pin, byte recv_pin,
    						   unsigned int sample_size, byte resolution):
      CapacitiveSensor(send_pin, recv_pin),
      sample_size(sample_size),
      resolution(resolution),
      values(NULL), total(0), threshold(0), index(0)
    {
      values = new long int[this->sample_size];
      // Initialize the samples to 0
      for (index = 0; index < this->sample_size; ++index) {
        values[index] = 0;
      }
      index = 0;
    }
    
    /*
     * Copy constructor
     */
    ExtendedCapacitiveSensor::ExtendedCapacitiveSensor(const ExtendedCapacitiveSensor &ecs):
      CapacitiveSensor(ecs),
      sample_size(ecs.sample_size),
      resolution(ecs.resolution),
      values(NULL), total(ecs.total),
      threshold(ecs.threshold), index(0) {
      values = new long int[sample_size];
      for (index = 0; index < sample_size; ++index) {
        values[index] = ecs.values[index];
      }
      index = ecs.index;
    }
    
    /*
     * Destructor
     */
    ExtendedCapacitiveSensor::~ExtendedCapacitiveSensor() {
      delete [] values;
    }
    
    /*
     * Affectation operator
     */
    ExtendedCapacitiveSensor &ExtendedCapacitiveSensor::operator=(const ExtendedCapacitiveSensor &ecs) {
      if (this != &ecs) {
        CapacitiveSensor::operator=(ecs);
        if (sample_size != ecs.sample_size) {
          delete [] values;
          sample_size = ecs.sample_size;
          values = new long int[sample_size];
        }
        for (index = 0; index < sample_size; ++index) {
          values[index] = ecs.values[index];
        }
        total = ecs.total;
        threshold = ecs.threshold;
        index = ecs.index;
        resolution = ecs.resolution;
      }
      return *this;
    }
    
    /*
     * Calibrate the capacitive sensor using nb reads.
     */
    void ExtendedCapacitiveSensor::calibrate(unsigned long nb) {
      // Calibrate the unsensed threshold using the maximum sample total
      // value over 100 tries.
      threshold = 0;
      for (unsigned long i = 0; i < nb; ++i) {
        next();
        threshold = max(total, threshold);
      }
      // Using twice this threshold
      threshold <<= 1;
      threshold /= sample_size;
      DBG("threshold is set to ");
      DBGln(threshold);
    }
    
    /*
     * Update the home made capacitive sensor values
     */
    ExtendedCapacitiveSensor &ExtendedCapacitiveSensor::next() {
      long int v = capacitiveSensor(resolution);
      total -= values[index];
      values[index] = v >= 0 ? v : 0;
      total += values[index];
      /*
        DBG("CS: value[");
        DBG(index);
        DBG("] = ");
        DBG(v);
        DBG(", total = ");
        DBGln(total);
      */
      ++index %= sample_size;
      return *this;
    }
    
    /*
     * Returns true on sensing
     */
    ExtendedCapacitiveSensor::operator bool() const {
      return (total / sample_size) > threshold;
    }
    
    Development

  • Led dimmer with touch sensor
    D DoccY

    File 'LightDimmer.ino'

    /******************************************************************************
    *                                                                             *
    *  Copyright © 2018 -- DoccY's productions                                    *
    *                                                                             *
    *  Auteurs/Authors: DoccY <doccy@mancheron.fr>                                *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  Ce logiciel  permet de monitorer  un variateur de lumière (typiquement un  *
    *  ruban de LED) à partir d'un (ou plusieurs) capteurs sensitifs.             *
    *                                                                             *
    *  Ce logiciel est régi par la   licence CeCILL soumise au droit français et  *
    *  respectant les principes  de diffusion des logiciels libres.  Vous pouvez  *
    *  utiliser, modifier et/ou redistribuer ce programme sous les conditions de  *
    *  la licence CeCILL telle que diffusée par   le CEA, le CNRS et l'INRIA sur  *
    *  le site "http://www.cecill.info".                                          *
    *                                                                             *
    *  En contrepartie de l'accessibilité au code source et des droits de copie,  *
    *  de modification et de redistribution accordés par cette licence, il n'est  *
    *  offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,  *
    *  seule une responsabilité  restreinte pèse  sur l'auteur du programme,  le  *
    *  titulaire des droits patrimoniaux et les concédants successifs.            *
    *                                                                             *
    *  À  cet égard  l'attention de  l'utilisateur est  attirée sur  les risques  *
    *  associés au   chargement, à   l'utilisation, à  la modification  et/ou au  *
    *  développement et   à la reproduction du  logiciel par l'utilisateur étant  *
    *  donné  sa spécificité  de logiciel libre,  qui peut le rendre  complexe à  *
    *  manipuler et qui le réserve donc à des développeurs et des professionnels  *
    *  avertis  possédant  des  connaissances  informatiques  approfondies.  Les  *
    *  utilisateurs sont   donc invités   à charger   et tester  l'adéquation du  *
    *  logiciel à   leurs besoins  dans des  conditions permettant  d'assurer la  *
    *  sécurité de leurs systèmes et ou de leurs données et, plus  généralement,  *
    *  à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.         *
    *                                                                             *
    *  Le fait que   vous puissiez accéder à cet  en-tête signifie que vous avez  *
    *  pris connaissance de la licence CeCILL,   et que vous en avez accepté les  *
    *  termes.                                                                    *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  This software allows to drive a light dimmer (typically a led strip) from  *
    *  one or more touch sensors.                                                 *
    *                                                                             *
    *  This software is governed by the CeCILL license under French law and       *
    *  abiding by the rules of distribution of free software. You can use,        *
    *  modify and/ or redistribute the software under the terms of the CeCILL     *
    *  license as circulated by CEA, CNRS and INRIA at the following URL          *
    *  "http://www.cecill.info".                                                  *
    *                                                                             *
    *  As a counterpart to the access to the source code and rights to copy,      *
    *  modify and redistribute granted by the license, users are provided only    *
    *  with a limited warranty and the software's author, the holder of the       *
    *  economic rights, and the successive licensors have only limited            *
    *  liability.                                                                 *
    *                                                                             *
    *  In this respect, the user's attention is drawn to the risks associated     *
    *  with loading, using, modifying and/or developing or reproducing the        *
    *  software by the user in light of its specific status of free software,     *
    *  that may mean that it is complicated to manipulate, and that also          *
    *  therefore means that it is reserved for developers and experienced         *
    *  professionals having in-depth computer knowledge. Users are therefore      *
    *  encouraged to load and test the software's suitability as regards their    *
    *  requirements in conditions enabling the security of their systems and/or   *
    *  data to be ensured and, more generally, to use and operate it in the same  *
    *  conditions as regards security.                                            *
    *                                                                             *
    *  The fact that you are presently reading this means that you have had       *
    *  knowledge of the CeCILL license and that you accept its terms.             *
    *                                                                             *
    ******************************************************************************/
    
    /*
     * The driver functionnalities is merely inspired by the code given at:
     * https://forum.mysensors.org/topic/2510/led-dimmer-with-touch-sensor/3
     *
     * Default MOSFET pin is 3
     */
    
    // Constants
    #define TITLE "Light Dimmer Driver for touch sensors"
    #define VERSION "1.0"
    
    // Libraries
    #include <Bounce2.h>
    
    #include "dimmer.h"
    #include "dimmerDriver.h"
    #include "extendedCapacitiveSensor.h"
    
    // Pins
    #define LED_PIN 3           // Pin attached to N-MOSFET Gate pin
    #define CS_PIN 4            // Auto alimented capacitive touch sensor pin
    #define CS_SEND_PIN 7       // Send pin for the extendedCapacitiveSensor
    #define CS_RECV_PIN 8       // Receive pin for the extendedCapacitiveSensor
    
    Bounce debouncer = Bounce();
    
    #define EEPROM_DIM_LEVEL_SAVE 1 // set to -1 to disable EEPROM saving light level state 
    #define EEPROM_DIM_STATE_SAVE 2 // set to -1 to disable EEPROM saving switch state
    Dimmer dimmer(LED_PIN, DimmerDriver::SHORT_FADE_DELAY, delay, EEPROM_DIM_LEVEL_SAVE, EEPROM_DIM_STATE_SAVE);
    // All but the led pin argument have default values (see dimmer.h).
    // 'Dimmer dimmer(LED_PIN)' is equivalent to
    // 'Dimmer dimmer(LED_PIN, 10ul,delay, -1, -1)'
    
    #define CS_SHORT_TOUCH_MAX_TIME 250ul
    #define CS_COUNTING_TIME_WINDOW 350ul
    DimmerDriver driver(dimmer, CS_SHORT_TOUCH_MAX_TIME, CS_COUNTING_TIME_WINDOW);
    // All but the dimmer argument have default values (see
    // dimmerDriver.h).
    // 'DimmerDriver driver(dimmer)' is equivalent to
    // 'DimmerDriver driver(dimmer, 250ul, 250ul)'
    
    #define CS_SAMPLE_SIZE 10
    #define CS_RESOLUTION 20
    ExtendedCapacitiveSensor cs(CS_SEND_PIN, CS_RECV_PIN, CS_SAMPLE_SIZE, CS_RESOLUTION);
    // All but the send and receive pins have default values (see
    // extendedCapacitiveSensor.h).
    // 'ExtendedCapacitiveSensor cs(x, y)' is equivalent to
    // 'ExtendedCapacitiveSensor cs(x, y, 3, 30);
    
    /*
     * Microcontroler initialization
     */
    void setup() {
    
      Serial.begin(115200);
      while (!Serial) {
        delay(10);
      }
      Serial.print(TITLE);
      Serial.print(" (version ");
      Serial.print(VERSION);
      Serial.println(")");
      Serial.println("========================");
      Serial.println("Starting the Sensor Node");
    
      // Instantiate digital pins
      pinMode(LED_PIN, OUTPUT);
      pinMode(CS_PIN, INPUT_PULLUP);
    
      cs.calibrate(100);
    
      // Restore both the switch state and the light level
      dimmer.load();
    
      // Switch the leds off
      Serial.println("Switch the leds off");
      analogWrite(LED_PIN, 0);
    
      dimmer.update();
    
      Serial.println("Sensor Node is ready...");
      Serial.println("=======================");
    }
    
    /*
     * Microcontroler loop
     */
    void loop() {
    
      // Check if someone touch the Velleman capacitive sensor
      // or the home made capacitive sensor
      bool state = (digitalRead(CS_PIN) == HIGH) || cs.next();
    
      if (state) {
        driver.activate();
      } else {
        driver.deactivate();
      }
    
      // Update the Light Level
      dimmer.update();
    
    }
    
    // Local Variables:
    // mode:c++
    // End:
    

    File 'MySensorsLightDimmer.ino'

    /*
      The MySensors Arduino library handles the wireless radio link and protocol
      between your home built sensors/actuators and HA controller of choice.
      The sensors forms a self healing radio network with optional repeaters. Each
      repeater and gateway builds a routing tables in EEPROM which keeps track of the
      network topology allowing messages to be routed to nodes.
    
      Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
      Copyright (C) 2013-2015 Sensnology AB
      Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    
      Documentation: http://www.mysensors.org
      Support Forum: http://forum.mysensors.org
    */
    
    /******************************************************************************
    *                                                                             *
    *  Copyright © 2018 -- DoccY's productions                                    *
    *                                                                             *
    *  Auteurs/Authors: DoccY <doccy@mancheron.fr>                                *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  Ce logiciel  permet de monitorer  un variateur de lumière (typiquement un  *
    *  ruban de LED)  à partir d'un (ou plusieurs) capteurs sensitifs comme d'un  *
    *  serveur MySensors.                                                         *
    *                                                                             *
    *  Ce logiciel est régi par la   licence CeCILL soumise au droit français et  *
    *  respectant les principes  de diffusion des logiciels libres.  Vous pouvez  *
    *  utiliser, modifier et/ou redistribuer ce programme sous les conditions de  *
    *  la licence CeCILL telle que diffusée par   le CEA, le CNRS et l'INRIA sur  *
    *  le site "http://www.cecill.info".                                          *
    *                                                                             *
    *  En contrepartie de l'accessibilité au code source et des droits de copie,  *
    *  de modification et de redistribution accordés par cette licence, il n'est  *
    *  offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,  *
    *  seule une responsabilité  restreinte pèse  sur l'auteur du programme,  le  *
    *  titulaire des droits patrimoniaux et les concédants successifs.            *
    *                                                                             *
    *  À  cet égard  l'attention de  l'utilisateur est  attirée sur  les risques  *
    *  associés au   chargement, à   l'utilisation, à  la modification  et/ou au  *
    *  développement et   à la reproduction du  logiciel par l'utilisateur étant  *
    *  donné  sa spécificité  de logiciel libre,  qui peut le rendre  complexe à  *
    *  manipuler et qui le réserve donc à des développeurs et des professionnels  *
    *  avertis  possédant  des  connaissances  informatiques  approfondies.  Les  *
    *  utilisateurs sont   donc invités   à charger   et tester  l'adéquation du  *
    *  logiciel à   leurs besoins  dans des  conditions permettant  d'assurer la  *
    *  sécurité de leurs systèmes et ou de leurs données et, plus  généralement,  *
    *  à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.         *
    *                                                                             *
    *  Le fait que   vous puissiez accéder à cet  en-tête signifie que vous avez  *
    *  pris connaissance de la licence CeCILL,   et que vous en avez accepté les  *
    *  termes.                                                                    *
    *                                                                             *
    *  -------------------------------------------------------------------------  *
    *                                                                             *
    *  This software allows to drive a light dimmer (typically a led strip) from  *
    *  one or more touch sensors as well as from a MySensors server.              *
    *                                                                             *
    *  This software is governed by the CeCILL license under French law and       *
    *  abiding by the rules of distribution of free software. You can use,        *
    *  modify and/ or redistribute the software under the terms of the CeCILL     *
    *  license as circulated by CEA, CNRS and INRIA at the following URL          *
    *  "http://www.cecill.info".                                                  *
    *                                                                             *
    *  As a counterpart to the access to the source code and rights to copy,      *
    *  modify and redistribute granted by the license, users are provided only    *
    *  with a limited warranty and the software's author, the holder of the       *
    *  economic rights, and the successive licensors have only limited            *
    *  liability.                                                                 *
    *                                                                             *
    *  In this respect, the user's attention is drawn to the risks associated     *
    *  with loading, using, modifying and/or developing or reproducing the        *
    *  software by the user in light of its specific status of free software,     *
    *  that may mean that it is complicated to manipulate, and that also          *
    *  therefore means that it is reserved for developers and experienced         *
    *  professionals having in-depth computer knowledge. Users are therefore      *
    *  encouraged to load and test the software's suitability as regards their    *
    *  requirements in conditions enabling the security of their systems and/or   *
    *  data to be ensured and, more generally, to use and operate it in the same  *
    *  conditions as regards security.                                            *
    *                                                                             *
    *  The fact that you are presently reading this means that you have had       *
    *  knowledge of the CeCILL license and that you accept its terms.             *
    *                                                                             *
    ******************************************************************************/
    
    /*
     * The driver functionnalities is merely inspired by the code given at:
     * https://forum.mysensors.org/topic/2510/led-dimmer-with-touch-sensor/3
     *
     * Default MOSFET pin is 3
     *
     * REVISION HISTORY
     *   Version 1.0 - Developed by Bruce Lacey and GizMoCuz (Domoticz)
     *   Version 1.1 - Modified by hek to incorporate a rotary encode to adjust
     *                 light level locally at node
     *   Version 1.2 - Modified FreakOfNature to replace rotary encode with touch dim
     *   Version 1.3 - Complete rewrite and update to MySensors v.2
     */
    
    // Constants
    #define TITLE "MySensors Light Dimmer /w touch sensors"
    #define VERSION "1.3"
    
    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    
    // Libraries
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #include "dimmer.h"
    #include "dimmerDriver.h"
    #include "extendedCapacitiveSensor.h"
    
    // Pins
    #define LED_PIN 3           // Pin attached to N-MOSFET Gate pin
    #define CS_PIN 4            // Auto alimented capacitive touch sensor pin
    #define CS_SEND_PIN 7       // Send pin for the extendedCapacitiveSensor
    #define CS_RECV_PIN 8       // Receive pin for the extendedCapacitiveSensor
    
    Bounce debouncer = Bounce();
    
    #define EEPROM_DIM_LEVEL_SAVE 1 // set to -1 to disable EEPROM saving light level state 
    #define EEPROM_DIM_STATE_SAVE 2 // set to -1 to disable EEPROM saving switch state
    Dimmer dimmer(LED_PIN, DimmerDriver::SHORT_FADE_DELAY, delay, EEPROM_DIM_LEVEL_SAVE, EEPROM_DIM_STATE_SAVE);
    // All but the led pin argument have default values (see dimmer.h).
    // 'Dimmer dimmer(LED_PIN)' is equivalent to
    // 'Dimmer dimmer(LED_PIN, 10ul,delay, -1, -1)'
    
    #define CS_SHORT_TOUCH_MAX_TIME 250ul
    #define CS_COUNTING_TIME_WINDOW 350ul
    DimmerDriver driver(dimmer, CS_SHORT_TOUCH_MAX_TIME, CS_COUNTING_TIME_WINDOW);
    // All but the dimmer argument have default values (see
    // dimmerDriver.h).
    // 'DimmerDriver driver(dimmer)' is equivalent to
    // 'DimmerDriver driver(dimmer, 250ul, 250ul)'
    
    #define CS_SAMPLE_SIZE 10
    #define CS_RESOLUTION 20
    ExtendedCapacitiveSensor cs(CS_SEND_PIN, CS_RECV_PIN, CS_SAMPLE_SIZE, CS_RESOLUTION);
    // All but the send and receive pins have default values (see
    // extendedCapacitiveSensor.h).
    // 'ExtendedCapacitiveSensor cs(x, y)' is equivalent to
    // 'ExtendedCapacitiveSensor cs(x, y, 3, 30);
    
    #define CHILD_ID_LIGHT 1
    MyMessage switchStateMsg(CHILD_ID_LIGHT, V_STATUS);
    MyMessage lightLevelMsg(CHILD_ID_LIGHT, V_PERCENTAGE);
    
    
    /*
     * MySensors Initialization
     */
    void presentation() {
    
      // Send the Sketch Version Information to the Gateway
    #ifdef MY_DEBUG
      Serial.print("Presenting the sensor to MySensors gateway: ");
      Serial.print(SN);
      Serial.print(" version ");
      Serial.println(SV);
    #endif
      present(CHILD_ID_LIGHT, S_DIMMER);
      sendSketchInfo(TITLE, VERSION);
    }
    
    /*
     * MySensors node receive operation
     */
    void receive(const MyMessage &message) {
      if (message.type == V_STATUS) {
        // Incoming on/off command sent from controller ("1" or "0")
        if (message.getBool()) {
          dimmer.ON();
        } else {
          dimmer.OFF();
        }
      } else {
        if (message.type == V_DIMMER) {
          // Incoming dim-level command sent from controller (or ack message)
          dimmer.setFadingDelay(DimmerDriver::SHORT_FADE_DELAY);
          dimmer = message.getUInt();
        }
      }
    }
    
    
    /*
     * Send light level and switch state to MySensors server
     */
    void sendDimmerInfos() {
      switchStateMsg.set((bool) dimmer);
      send(switchStateMsg);
      lightLevelMsg.set(dimmer.getLevel(Dimmer::CURRENT));
      send(lightLevelMsg);
    }
    
    
    /*
     * Microcontroler initialization
     */
    void setup() {
    
      Serial.begin(115200);
      while (!Serial) {
        delay(10);
      }
      Serial.print(TITLE);
      Serial.print(" (version ");
      Serial.print(VERSION);
      Serial.println(")");
      Serial.println("===========================");
      Serial.println("Starting the MySensors Node");
    
      // Instantiate digital pins
      pinMode(LED_PIN, OUTPUT);
      pinMode(CS_PIN, INPUT_PULLUP);
    
      cs.calibrate(100);
    
      // Restore both the switch state and the light level
      dimmer.load();
    
      // Switch the leds off
      Serial.println("Switch the leds off");
      analogWrite(LED_PIN, 0);
    
      dimmer.update();
    
      // Send informations to MySensors
      sendDimmerInfos();
    
      Serial.println("MySensors Node is ready...");
      Serial.println("==========================");
    }
    
    /*
     * Microcontroler loop
     */
    void loop() {
    
      // Check if someone touch the Velleman capacitive sensor
      // or the home made capacitive sensor
      bool state = (digitalRead(CS_PIN) == HIGH) || cs.next();
    
      if (state) {
        driver.activate();
      } else {
        driver.deactivate();
      }
    
      // Update the Light Level
      dimmer.update();
    
      // Send informations to MySensors
      sendDimmerInfos();
    
    }
    
    // Local Variables:
    // mode:c++
    // End:
    
    Development
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular