MySensors Library & Examples  2.3.2-62-ge298769
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-2022 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-Width-Modulation (PWM) 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 //#define MY_PJON
47 
48 #include <MySensors.h>
49 
50 #define SN "DimmableLED"
51 #define SV "1.1"
52 
53 #define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
54 #define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
55 
56 static int16_t currentLevel = 0; // Current dim level...
57 MyMessage dimmerMsg(0, V_DIMMER);
58 MyMessage lightMsg(0, V_LIGHT);
59 
60 
61 /***
62  * Dimmable LED initialization method
63  */
64 void setup()
65 {
66  // Pull the gateway's current dim level - restore light level upon node power-up
67  request( 0, V_DIMMER );
68 }
69 
71 {
72  // Register the LED Dimmable Light with the gateway
73  present( 0, S_DIMMER );
74 
75  sendSketchInfo(SN, SV);
76 }
77 
78 /***
79  * Dimmable LED main processing loop
80  */
81 void loop()
82 {
83 }
84 
85 
86 
87 void receive(const MyMessage &message)
88 {
89  if (message.getType() == V_LIGHT || message.getType() == V_DIMMER) {
90 
91  // Retrieve the power or dim level from the incoming request message
92  int requestedLevel = atoi( message.data );
93 
94  // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
95  requestedLevel *= ( message.getType() == V_LIGHT ? 100 : 1 );
96 
97  // Clip incoming level to valid range of 0 to 100
98  requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
99  requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
100 
101  Serial.print( "Changing level to " );
102  Serial.print( requestedLevel );
103  Serial.print( ", from " );
104  Serial.println( currentLevel );
105 
106  fadeToLevel( requestedLevel );
107 
108  // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
109  send(lightMsg.set(currentLevel > 0));
110 
111  // hek comment: Is this really nessesary?
112  send( dimmerMsg.set(currentLevel) );
113 
114 
115  }
116 }
117 
118 /***
119  * This method provides a graceful fade up/down effect
120  */
121 void fadeToLevel( int toLevel )
122 {
123 
124  int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
125 
126  while ( currentLevel != toLevel ) {
127  currentLevel += delta;
128  analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
129  delay( FADE_DELAY );
130  }
131 }
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:87
loop
void loop()
Main loop.
Definition: DimmableLEDActuator.ino:81
presentation
void presentation()
Node presentation.
Definition: DimmableLEDActuator.ino:70
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:64
MyMessage::data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:661
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:290