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: