MySensors Library & Examples  2.3.2
DimmableLEDActuator.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * REVISION HISTORY
22  * Version 1.0 - February 15, 2014 - Bruce Lacey
23  * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
24  *
25  * DESCRIPTION
26  * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
27  * <[email protected]> Vera Arduino Sensor project.
28  * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
29  *
30  * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
31  * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
32  * to the LED negative terminal and the MOSFET Source pin is connected to ground.
33  *
34  * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
35  * http://www.mysensors.org/build/dimmer
36  */
37 
38 // Enable debug prints to serial monitor
39 #define MY_DEBUG
40 
41 // Enable and select radio type attached
42 #define MY_RADIO_RF24
43 //#define MY_RADIO_NRF5_ESB
44 //#define MY_RADIO_RFM69
45 //#define MY_RADIO_RFM95
46 
47 #include <MySensors.h>
48 
49 #define SN "DimmableLED"
50 #define SV "1.1"
51 
52 #define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
53 #define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
54 
55 static int16_t currentLevel = 0; // Current dim level...
56 MyMessage dimmerMsg(0, V_DIMMER);
57 MyMessage lightMsg(0, V_LIGHT);
58 
59 
60 /***
61  * Dimmable LED initialization method
62  */
63 void setup()
64 {
65  // Pull the gateway's current dim level - restore light level upon node power-up
66  request( 0, V_DIMMER );
67 }
68 
70 {
71  // Register the LED Dimmable Light with the gateway
72  present( 0, S_DIMMER );
73 
74  sendSketchInfo(SN, SV);
75 }
76 
77 /***
78  * Dimmable LED main processing loop
79  */
80 void loop()
81 {
82 }
83 
84 
85 
86 void receive(const MyMessage &message)
87 {
88  if (message.getType() == V_LIGHT || message.getType() == V_DIMMER) {
89 
90  // Retrieve the power or dim level from the incoming request message
91  int requestedLevel = atoi( message.data );
92 
93  // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
94  requestedLevel *= ( message.getType() == V_LIGHT ? 100 : 1 );
95 
96  // Clip incoming level to valid range of 0 to 100
97  requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
98  requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
99 
100  Serial.print( "Changing level to " );
101  Serial.print( requestedLevel );
102  Serial.print( ", from " );
103  Serial.println( currentLevel );
104 
105  fadeToLevel( requestedLevel );
106 
107  // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
108  send(lightMsg.set(currentLevel > 0));
109 
110  // hek comment: Is this really nessesary?
111  send( dimmerMsg.set(currentLevel) );
112 
113 
114  }
115 }
116 
117 /***
118  * This method provides a graceful fade up/down effect
119  */
120 void fadeToLevel( int toLevel )
121 {
122 
123  int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
124 
125  while ( currentLevel != toLevel ) {
126  currentLevel += delta;
127  analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
128  delay( FADE_DELAY );
129  }
130 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
receive
void receive(const MyMessage &message)
Callback for incoming messages.
Definition: DimmableLEDActuator.ino:86
loop
void loop()
Main loop.
Definition: DimmableLEDActuator.ino:80
presentation
void presentation()
Node presentation.
Definition: DimmableLEDActuator.ino:69
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
MyMessage::getType
uint8_t getType(void) const
Get message type.
request
bool request(const uint8_t childSensorId, const uint8_t variableType, const uint8_t destination=GATEWAY_ADDRESS)
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
setup
void setup()
Called after node initialises but before main loop.
Definition: DimmableLEDActuator.ino:63
MyMessage::data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:660
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289