<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Dimmable led actuator sketch enhancement]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I've been working on a simple mod of the dimmable led actuator available in the examples folder.</p>
<p dir="auto">I needed a few things that were not included, like the ability to set a more precise fade level, make longer fades and also being able to change those fade times dynamically.</p>
<p dir="auto">Also, as a result of longer dafe times, I had to modify the sketch so the node is still able to send/receive messages during fade time.</p>
<p dir="auto">You could send messages from VERA this way:</p>
<pre><code>luup.call_action("urn:upnp-arduino-cc:serviceId:arduino1", "SendCommand", {radioId="6;100", variableId="DIMMER", value="100,25"}, 130)
</code></pre>
<p dir="auto">This message is sent to node id 6, with parent node id 130, and tells the node to fade to 100% in 25 seconds.<br />
You could choose any number (0-100) to fade to, instead of the 10% increment Vera offers. Also the old functionality remains working as before.</p>
<p dir="auto">Here's the sketch in case someone is interested:</p>
<pre><code>/***
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * 
 * DESCRIPTION
 * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad 
 * &lt;henrik.ekblad@gmail.com&gt; Vera Arduino Sensor project.  
 * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
 * 
 * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.  
 * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
 * to the LED negative terminal and the MOSFET Source pin is connected to ground.  
 *
 * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
 *
 * REVISION HISTORY
 * Version 1.0 - February 15, 2014 - Bruce Lacey
 * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek) 
 * Version 1.2 - April 13, 2015 - Added long dynamic and precise fade, also made fade non exclusive (ferpando)
 ***/
 
 #include &lt;Time.h&gt; 
 
#define SN "DimmableLED"
#define SV "1.2"

#include &lt;MySensor.h&gt; 
#include &lt;SPI.h&gt;

#define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin

MySensor gw(9,10);

static int currentLevel = 0;  // Current dim level...
static int requestedLevel = 0;

MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT);

int FADE_DELAY;   // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

/***
 * Dimmable LED initialization method
 */
void setup()  
{ 
  Serial.println( SN ); 
  gw.begin( incomingMessage );
  
  // Register the LED Dimmable Light with the gateway
  gw.present( 0, S_DIMMER );
  
  gw.sendSketchInfo(SN, SV);
  // Pull the gateway's current dim level - restore light level upon sendor node power-up
  gw.request( 0, V_DIMMER );

}

/***
 *  Dimmable LED main processing loop 
 */
void loop() 
{
  gw.process();
  fadeToLevel();
  // if delay is too long, it might stop the node from receiving/sending more messages for a long time.
  //doing it this way, the node can keep functioning regardless fade completition.
}

void incomingMessage(const MyMessage &amp;message) {
  if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
    if(message.sensor == 100 ){ //fade special, addressed to node 100
      // Message payload: value to fade to, seconds to fade     example: "20,10"
      
      int commaIndex = msg.indexOf(',');

      String firstValue = msg.substring(0, commaIndex);
      String secondValue = msg.substring(commaIndex+1);

      requestedLevel=firstValue.toInt();
      int steps=abs(currentLevel-requestedLevel);

      if(commaIndex == -1){
         FADE_DELAY=10;
      }else { 
         FADE_DELAY = 1000* secondValue.toInt() / steps;
      }
    }else { //fade normal
    
          FADE_DELAY=10;
          //  Retrieve the power or dim level from the incoming request message
          requestedLevel = atoi( message.data );
          
          // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
          requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
          
          // Clip incoming level to valid range of 0 to 100
          requestedLevel = requestedLevel &gt; 100 ? 100 : requestedLevel;
          requestedLevel = requestedLevel &lt; 0   ? 0   : requestedLevel;
          
          }
       
      Serial.print( "Changing level to " );
      Serial.print( requestedLevel );
      Serial.print( ", from " ); 
      Serial.println( currentLevel ); 
       
         
      // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
      gw.send(lightMsg.set(currentLevel &gt; 0 ? 1 : 0));
  
      // hek comment: Is this really nessesary?
      gw.send( dimmerMsg.set(currentLevel) );
    
    }
}

/***
 *  This method provides a graceful fade up/down effect
 */
void fadeToLevel() {

  if ( currentLevel != requestedLevel ) {
    int delta = ( requestedLevel - currentLevel ) &lt; 0 ? -1 : 1;
  
    currentLevel += delta;
    Serial.print( "analog write: " );
    Serial.println( currentLevel / 100. * 255 );
    analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
    delay( FADE_DELAY );
  }
  
}
</code></pre>
]]></description><link>https://forum.mysensors.org/topic/1241/dimmable-led-actuator-sketch-enhancement</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 10:43:47 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/1241.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 13 Apr 2015 11:38:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dimmable led actuator sketch enhancement on Thu, 16 Apr 2015 07:58:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/drcashman" aria-label="Profile: Drcashman">@<bdi>Drcashman</bdi></a><br />
by outputs you mean to control 2-3 light fades?<br />
I guess it wouldn't be too difficult. Just add counters for current and requested values for each fade to keep them independent</p>
]]></description><link>https://forum.mysensors.org/post/12780</link><guid isPermaLink="true">https://forum.mysensors.org/post/12780</guid><dc:creator><![CDATA[ferpando]]></dc:creator><pubDate>Thu, 16 Apr 2015 07:58:13 GMT</pubDate></item><item><title><![CDATA[Reply to Dimmable led actuator sketch enhancement on Thu, 16 Apr 2015 07:56:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/awi" aria-label="Profile: AWI">@<bdi>AWI</bdi></a><br />
You are right but now the blocking is just a few milliseconds instead of the whole fade time.<br />
I think messages are still received because the are interrupt driven, so in the next cycle message will arrive.<br />
You will be just delaying a few millis sending new messages.</p>
<p dir="auto">Your solutions is indeed an improvement to be considered.<br />
In my case I don't need that much control.</p>
]]></description><link>https://forum.mysensors.org/post/12779</link><guid isPermaLink="true">https://forum.mysensors.org/post/12779</guid><dc:creator><![CDATA[ferpando]]></dc:creator><pubDate>Thu, 16 Apr 2015 07:56:11 GMT</pubDate></item><item><title><![CDATA[Reply to Dimmable led actuator sketch enhancement on Tue, 14 Apr 2015 19:48:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ferpando" aria-label="Profile: ferpando">@<bdi>ferpando</bdi></a> I maybe misinterpreting things but for me it looks like the "fadeToLevel" is still blocking with respect to the receipt of messages for a time as long as FADE_DELAY.  would suggest making it non-blocking by means of a time counter.</p>
<p dir="auto">like</p>
<pre><code>// timer value for "once in a while" events
    unsigned long now = millis();
    gw.process() ;								// process MySensors messages (needed for repeater)
    // Read/update every "delay" (60) seconds
    if (now-lastUpdate &gt; 60000) {
		read_sensors();
		lastUpdate = now;
    }
</code></pre>
]]></description><link>https://forum.mysensors.org/post/12721</link><guid isPermaLink="true">https://forum.mysensors.org/post/12721</guid><dc:creator><![CDATA[AWI]]></dc:creator><pubDate>Tue, 14 Apr 2015 19:48:46 GMT</pubDate></item><item><title><![CDATA[Reply to Dimmable led actuator sketch enhancement on Tue, 14 Apr 2015 18:47:52 GMT]]></title><description><![CDATA[<p dir="auto">Can this be expanded to include  2-3 more outputs?</p>
]]></description><link>https://forum.mysensors.org/post/12711</link><guid isPermaLink="true">https://forum.mysensors.org/post/12711</guid><dc:creator><![CDATA[Drcashman]]></dc:creator><pubDate>Tue, 14 Apr 2015 18:47:52 GMT</pubDate></item><item><title><![CDATA[Reply to Dimmable led actuator sketch enhancement on Tue, 14 Apr 2015 09:14:27 GMT]]></title><description><![CDATA[<p dir="auto">A few bug fixes and also this sends back to gateway the value of 'requestedValue' instead of 'currentValue' as I think it should be.<br />
Delays and precise setpoint are now correct.</p>
<pre><code>/***
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * 
 * DESCRIPTION
 * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad 
 * &lt;henrik.ekblad@gmail.com&gt; Vera Arduino Sensor project.  
 * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
 * 
 * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.  
 * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
 * to the LED negative terminal and the MOSFET Source pin is connected to ground.  
 *
 * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
 *
 * REVISION HISTORY
 * Version 1.0 - February 15, 2014 - Bruce Lacey
 * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek) 
 * Version 1.2 - April 13, 2015 - Added long dynamic and precise fade, also made fade non exclusive (ferpando)
 ***/
 
 #include &lt;Time.h&gt; 
 
#define SN "DimmableLED"
#define SV "1.2"

#include &lt;MySensor.h&gt; 
#include &lt;SPI.h&gt;

#define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin

MySensor gw(9,10);

static int currentLevel = 0;  // Current dim level...
static int requestedLevel = 0;
boolean preciseFade = false;

MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT);

int FADE_DELAY;   // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

/***
 * Dimmable LED initialization method
 */
void setup()  
{ 
  Serial.println( SN ); 
  gw.begin( incomingMessage );
  
  // Register the LED Dimmable Light with the gateway
  gw.present( 0, S_DIMMER );
  
  gw.sendSketchInfo(SN, SV);
  // Pull the gateway's current dim level - restore light level upon sendor node power-up
  gw.request( 0, V_DIMMER );

}

/***
 *  Dimmable LED main processing loop 
 */
void loop() 
{
  gw.process();
  fadeToLevel();
  // if delay is too long, it might stop the node from receiving/sending more messages for a long time.
  //doing it this way, the node can keep functioning regardless fade completition.
}

void incomingMessage(const MyMessage &amp;message) {
  if (message.type == V_LIGHT || message.type == V_DIMMER) {
    int steps;
    int multiplier;
    if(message.sensor == 100 ){ //fade special, addressed to node 100
      // Message payload: value to fade to, seconds to fade     example: "20,10"
      preciseFade = true;
      String msg=message.getString();
      
      int commaIndex = msg.indexOf(',');

      String firstValue = msg.substring(0, commaIndex);
      String secondValue = msg.substring(commaIndex+1);

      requestedLevel=firstValue.toInt();
      steps = currentLevel-requestedLevel;
      multiplier = abs(1000 / steps);
      if(commaIndex == -1){
         FADE_DELAY=multiplier * 10;
      }else { 
         FADE_DELAY = multiplier * secondValue.toInt();
      }
    }else { //fade normal
          preciseFade = false;
          steps = currentLevel-requestedLevel;
          multiplier = abs(1000 / steps);
          FADE_DELAY=multiplier * 10;
          //  Retrieve the power or dim level from the incoming request message
          requestedLevel = atoi( message.data );
          
          // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
          requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
          
          // Clip incoming level to valid range of 0 to 100
          requestedLevel = requestedLevel &gt; 100 ? 100 : requestedLevel;
          requestedLevel = requestedLevel &lt; 0   ? 0   : requestedLevel;
          
          }
          
    Serial.print( "Changing level to " );
    Serial.print( requestedLevel );
    Serial.print( ", from " ); 
    Serial.println( currentLevel ); 
     
    // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
    gw.send(lightMsg.set(requestedLevel &gt; 0 ? 1 : 0));

    // hek comment: Is this really nessesary?
    gw.send( dimmerMsg.set(requestedLevel) );
    
    }
}

/***
 *  This method provides a graceful fade up/down effect
 */
void fadeToLevel() {
  
  if ( currentLevel != requestedLevel ) {
    int pwmLevel;
    int delta = ( requestedLevel - currentLevel ) &lt; 0 ? -1 : 1;
    currentLevel += delta;
    Serial.print( "analog write: " );
    if(preciseFade){ //set to specific number
        pwmLevel = (int)currentLevel;
        }else  { //set to 10% increment level
           pwmLevel=(int)(currentLevel / 100. * 255);
           }
  Serial.println( pwmLevel );
  analogWrite( LED_PIN, pwmLevel );
  delay( FADE_DELAY );
  }
}
</code></pre>
]]></description><link>https://forum.mysensors.org/post/12685</link><guid isPermaLink="true">https://forum.mysensors.org/post/12685</guid><dc:creator><![CDATA[ferpando]]></dc:creator><pubDate>Tue, 14 Apr 2015 09:14:27 GMT</pubDate></item></channel></rss>