Skip to content

Commit 8e53f75

Browse files
committed
Add dimmable LED actuator example
1 parent 9f48f09 commit 8e53f75

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/***
2+
* This program is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU General Public License
4+
* version 2 as published by the Free Software Foundation.
5+
*
6+
* DESCRIPTION
7+
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
8+
* <[email protected]> Vera Arduino Sensor project.
9+
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
10+
*
11+
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
12+
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
13+
* to the LED negative terminal and the MOSFET Source pin is connected to ground.
14+
*
15+
* This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
16+
*
17+
* REVISION HISTORY
18+
* Version 1.0 - February 15, 2014 - Bruce Lacey
19+
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
20+
*
21+
***/
22+
#define SN "DimmableLED"
23+
#define SV "1.1"
24+
25+
#include <MySensor.h>
26+
#include <SPI.h>
27+
28+
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
29+
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
30+
31+
MySensor gw(9,10);
32+
33+
static int currentLevel = 0; // Current dim level...
34+
MyMessage dimmerMsg(0, V_DIMMER);
35+
MyMessage lightMsg(0, V_LIGHT);
36+
37+
38+
/***
39+
* Dimmable LED initialization method
40+
*/
41+
void setup()
42+
{
43+
Serial.println( SN );
44+
gw.begin( incomingMessage );
45+
46+
// Register the LED Dimmable Light with the gateway
47+
gw.present( 0, S_DIMMER );
48+
49+
gw.sendSketchInfo(SN, SV);
50+
// Pull the gateway's current dim level - restore light level upon sendor node power-up
51+
gw.request( 0, V_DIMMER );
52+
}
53+
54+
/***
55+
* Dimmable LED main processing loop
56+
*/
57+
void loop()
58+
{
59+
gw.process();
60+
}
61+
62+
63+
64+
void incomingMessage(const MyMessage &message) {
65+
if (message.type == V_LIGHT || message.type == V_DIMMER) {
66+
67+
// Retrieve the power or dim level from the incoming request message
68+
int requestedLevel = atoi( message.data );
69+
70+
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
71+
requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
72+
73+
// Clip incoming level to valid range of 0 to 100
74+
requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
75+
requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
76+
77+
Serial.print( "Changing level to " );
78+
Serial.print( requestedLevel );
79+
Serial.print( ", from " );
80+
Serial.println( currentLevel );
81+
82+
fadeToLevel( requestedLevel );
83+
84+
// Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
85+
gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
86+
87+
// hek comment: Is this really nessesary?
88+
gw.send( dimmerMsg.set(currentLevel) );
89+
90+
91+
}
92+
}
93+
94+
/***
95+
* This method provides a graceful fade up/down effect
96+
*/
97+
void fadeToLevel( int toLevel ) {
98+
99+
int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
100+
101+
while ( currentLevel != toLevel ) {
102+
currentLevel += delta;
103+
analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
104+
delay( FADE_DELAY );
105+
}
106+
}
107+

0 commit comments

Comments
 (0)