Dimmable LED help needed !



  • Hi
    I'm testing Dimmable LED Actuator --> https://www.mysensors.org/build/dimmer
    Everything seems to work with Domoticz except one thing - It doesn't remember last dimming value (This is a feature I really need).
    After switching the light off and back on , no matter what value was before it goes everytime to 100%
    Figured out If I change the value 100 : 1 to 10 : 1 - It goes to 10% - but still It doesnt remember the last dimming value.

     // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
            requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );```
    

    I've found a sketch DimmableLight in My sensors examples which I would like to test unfortunately I cannot force it to work with LED stripe connected according to dimmer sketch( Mosfet , led pin 3 etc..)

    Could someone help me please with the code ?
    Which part is responsible for sending the information to LED_PIN 3 ? I tried to define LED pin3, also fade delay etc... to the below sketch - no luck.
    Does it matter if the variables are V_LIGHT and V_DIMMER if the type is S_DIMMER ? (both sketches) Shouldn't it be V_STATUS and V_PERCENTAGE ?

    Any help very welcome !
    Thanks in advance.

    /**
     * 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
     *
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - January 30, 2015 - Developed by GizMoCuz (Domoticz)
     *
     * DESCRIPTION
     * This sketch provides an example how to implement a Dimmable Light
     * It is pure virtual and it logs messages to the serial output
     * It can be used as a base sketch for actual hardware.
     * Stores the last light state and level in eeprom.
     *
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    
    #define CHILD_ID_LIGHT 1
    
    #define EPROM_LIGHT_STATE 1
    #define EPROM_DIMMER_LEVEL 2
    
    #define LIGHT_OFF 0
    #define LIGHT_ON 1
    
    #define SN "Dimable Light"
    #define SV "1.0"
    
    int16_t LastLightState=LIGHT_OFF;
    int16_t LastDimValue=100;
    
    MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
    MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
    
    void setup()
    {
    	//Retreive our last light state from the eprom
    	int LightState=loadState(EPROM_LIGHT_STATE);
    	if (LightState<=1) {
    		LastLightState=LightState;
    		int DimValue=loadState(EPROM_DIMMER_LEVEL);
    		if ((DimValue>0)&&(DimValue<=100)) {
    			//There should be no Dim value of 0, this would mean LIGHT_OFF
    			LastDimValue=DimValue;
    		}
    	}
    
    	//Here you actualy switch on/off the light with the last known dim level
    	SetCurrentState2Hardware();
    
    	Serial.println( "Node ready to receive messages..." );
    }
    
    void presentation()
    {
    	// Send the Sketch Version Information to the Gateway
    	sendSketchInfo(SN, SV);
    
    	present(CHILD_ID_LIGHT, S_DIMMER );
    }
    
    void loop()
    {
    }
    
    void receive(const MyMessage &message)
    {
    	if (message.type == V_LIGHT) {
    		Serial.println( "V_LIGHT command received..." );
    
    		int lstate= atoi( message.data );
    		if ((lstate<0)||(lstate>1)) {
    			Serial.println( "V_LIGHT data invalid (should be 0/1)" );
    			return;
    		}
    		LastLightState=lstate;
    		saveState(EPROM_LIGHT_STATE, LastLightState);
    
    		if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
    			//In the case that the Light State = On, but the dimmer value is zero,
    			//then something (probably the controller) did something wrong,
    			//for the Dim value to 100%
    			LastDimValue=100;
    			saveState(EPROM_DIMMER_LEVEL, LastDimValue);
    		}
    
    		//When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value
    		//This means if you previously set the lights dimmer value to 50%, and turn the light ON
    		//it will do so at 50%
    	} else if (message.type == V_DIMMER) {
    		Serial.println( "V_DIMMER command received..." );
    		int dimvalue= atoi( message.data );
    		if ((dimvalue<0)||(dimvalue>100)) {
    			Serial.println( "V_DIMMER data invalid (should be 0..100)" );
    			return;
    		}
    		if (dimvalue==0) {
    			LastLightState=LIGHT_OFF;
    		} else {
    			LastLightState=LIGHT_ON;
    			LastDimValue=dimvalue;
    			saveState(EPROM_DIMMER_LEVEL, LastDimValue);
    		}
    	} else {
    		Serial.println( "Invalid command received..." );
    		return;
    	}
    
    	//Here you set the actual light state/level
    	SetCurrentState2Hardware();
    }
    
    void SetCurrentState2Hardware()
    {
    	if (LastLightState==LIGHT_OFF) {
    		Serial.println( "Light state: OFF" );
    	} else {
    		Serial.print( "Light state: ON, Level: " );
    		Serial.println( LastDimValue );
    	}
    
    	//Send current state to the controller
    	SendCurrentState2Controller();
    }
    
    void SendCurrentState2Controller()
    {
    	if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
    		send(dimmerMsg.set((int16_t)0));
    	} else {
    		send(dimmerMsg.set(LastDimValue));
    	}
    }
    

  • Mod

    @Plantex could you post the serial output fom the node? That would be very useful, allowing us to see what happens.


  • Hero Member

    @plantex said in Dimmable LED help needed !:

    Hi
    I'm testing Dimmable LED Actuator --> https://www.mysensors.org/build/dimmer
    Everything seems to work with Domoticz except one thing - It doesn't remember last dimming value (This is a feature I really need).
    After switching the light off and back on , no matter what value was before it goes everytime to 100%

    Yes that is how that sketch was written to work. You can change it to work the way you want. There are several ways you could attack the problem, you could save the dimmer level to eeprom or perhaps request the dimmer level from the controller.

    Which part is responsible for sending the information to LED_PIN 3 ? I tried to >define LED pin3, also fade delay etc... to the below sketch - no luck.

    It is the line

     analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
    

    But as you have already found the line below is responsible for turning on the lights at 100% when the switch is used

    requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    

    Does it matter if the variables are V_LIGHT and V_DIMMER if the type is >S_DIMMER ? (both sketches) Shouldn't it be V_STATUS and V_PERCENTAGE ?

    While they are interchangeable and working for the present V_LIGHT and V_DIMMER have both been deprecated and may not work in future releases of MySensors. You should use V_STATUS and V_PERCENTAGE instead.

    To get you started I have made a quick change to the the code from the build dimmer page. I have not tested it so give it a try and see if it does what you want.

    /**
     * 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
     *
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - February 15, 2014 - Bruce Lacey
     * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
     *
     * DESCRIPTION
     * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
     * <henrik.ekblad@gmail.com> 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.
     * http://www.mysensors.org/build/dimmer
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    
    #define SN "DimmableLED"
    #define SV "1.1"
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    int dimmerSetting = 0 ;
    
    MyMessage dimmerMsg(0, V_PERCENTAGE);
    MyMessage lightMsg(0, V_STATUS);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()
    {
    	// Pull the gateway's current dim level - restore light level upon sendor node power-up
    	request( 0, V_PERCENTAGE );
    }
    
    void presentation()
    {
    	// Register the LED Dimmable Light with the gateway
    	present( 0, S_DIMMER );
    
    	sendSketchInfo(SN, SV);
    }
    
    /***
     *  Dimmable LED main processing loop
     */
    void loop()
    {
    }
    
    
    /*
    void receive(const MyMessage &message)
    {
    	if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
    		//  Retrieve the power or dim level from the incoming request message
    		int 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 > 100 ? 100 : requestedLevel;
    		requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
    		Serial.print( "Changing level to " );
    		Serial.print( requestedLevel );
    		Serial.print( ", from " );
    		Serial.println( currentLevel );
    
    		fadeToLevel( requestedLevel );
    
    		// Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
    		send(lightMsg.set(currentLevel > 0));
    
    		// hek comment: Is this really nessesary?
    		send( dimmerMsg.set(currentLevel) );
    
    
    	}
    }
    */
    
    
    
    void receive(const MyMessage &message) {
    switch (message.type) {
     
      case V_STATUS:                                           // message is from the switch
       if(message.getBool()){
         fadeToLevel( dimmerSetting );                         // turn light on at current dimmer level
       }
       else fadeToLevel( 0 );                                  // fade light to 0 (off)
      break;
     case V_PERCENTAGE:                                        // message is from the dimmer
       dimmerSetting = message.getInt();                       // get the new dimmer setting from the message
       fadeToLevel( dimmerSetting );                           // fade to the new dimmer setting
       send(lightMsg.set(dimmerSetting > 0 ? 1 : 0), false);   // send switch  state to controller , no ack requested
       break;
      }
      }
    
    
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel( int toLevel )
    {
    
    	int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
    	while ( currentLevel != toLevel ) {
    		currentLevel += delta;
    		analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
    		delay( FADE_DELAY );
    	}
    }
    


  • @Boots33
    Thank You

    I've uploaded Your sketch. I don't know why but LED went from 0 to 100% six times in a row, then it stayed on.
    When I opened serial monitor in Arduino - it did the same (six times 0-100%) and my debug stopped on the line:

    2202 TSF:MSG:READ,0-0-153,s=0,c=2,t=3,pt=0,l=3,sg=0:666
    8909 TSF:MSG:SEND,153-153-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    

    (666 interesting 😉 )

    Then from Domoticz
    Status of the switch was OFF but LED was actually physically ON - so I moved the bar to 33% and it did again (six times but this time from 100% to 33% and stayed on 33%)
    After that It started to behave normally when I moved the bar to 56% and to 9%
    So basically It works but every initial start whatever it is (uploading sketch, serial monitor, switch on from domoticz) it is doing 6 times complete range
    Any Idea why it is doing that ?

    Right now I'm planning to extend it to few more led stripes but this will be next step.

    Below my debug from the steps described above:

    0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
    3 TSM:INIT
    4 TSF:WUR:MS=0
    11 TSM:INIT:TSP OK
    13 TSF:SID:OK,ID=153
    14 TSM:FPAR
    51 TSF:MSG:SEND,153-153-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    128 TSF:MSG:READ,0-0-153,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    133 TSF:MSG:FPAR OK,ID=0,D=1
    2058 TSM:FPAR:OK
    2059 TSM:ID
    2060 TSM:ID:OK
    2062 TSM:UPL
    2065 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2071 TSF:MSG:READ,0-0-153,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2076 TSF:MSG:PONG RECV,HP=1
    2079 TSM:UPL:OK
    2080 TSM:READY:ID=153,PAR=0,DIS=1
    2085 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2093 TSF:MSG:READ,0-0-153,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2100 TSF:MSG:SEND,153-153-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2109 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2116 TSF:MSG:READ,0-0-153,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2123 TSF:MSG:SEND,153-153-0-0,s=0,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
    2131 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=11,pt=0,l=11,sg=0,ft=0,st=OK:DimmableLED
    2141 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    2148 MCO:REG:REQ
    2151 TSF:MSG:SEND,153-153-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2158 TSF:MSG:READ,0-0-153,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2163 MCO:PIM:NODE REG=1
    2165 MCO:BGN:STP
    2168 TSF:MSG:SEND,153-153-0-0,s=0,c=2,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2174 MCO:BGN:INIT OK,TSP=1
    2202 TSF:MSG:READ,0-0-153,s=0,c=2,t=3,pt=0,l=3,sg=0:666
    8909 TSF:MSG:SEND,153-153-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    267588 TSF:MSG:READ,0-0-153,s=1,c=1,t=3,pt=0,l=2,sg=0:33
    267593 TSF:MSG:ACK REQ
    267597 TSF:MSG:SEND,153-153-0-0,s=1,c=1,t=3,pt=0,l=2,sg=0,ft=0,st=OK:33
    273975 TSF:MSG:SEND,153-153-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    276599 TSF:MSG:READ,0-0-153,s=1,c=1,t=3,pt=0,l=2,sg=0:56
    276604 TSF:MSG:ACK REQ
    276609 TSF:MSG:SEND,153-153-0-0,s=1,c=1,t=3,pt=0,l=2,sg=0,ft=0,st=OK:56
    276848 TSF:MSG:SEND,153-153-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    281715 TSF:MSG:READ,0-0-153,s=1,c=1,t=3,pt=0,l=1,sg=0:9
    281720 TSF:MSG:ACK REQ
    281724 TSF:MSG:SEND,153-153-0-0,s=1,c=1,t=3,pt=0,l=1,sg=0,ft=0,st=OK:9
    282206 TSF:MSG:SEND,153-153-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
    

  • Hero Member

    @plantex

    I think the 666 is coming from the initial request made in setup part of the sketch. Perhaps because it is made before the presentation of the node we are getting invalid data?
    I see that in the original part of the sketch that I commented out there is code to constrain the percentage value between 0 and 100 . So maybe this is a known issue. We may need to move the request to the main loop instead.

    In any case we can make a couple of changes to see if it is the cause of the multi switching.

    at the start of the sketch change

    int dimmerSetting = 0 ;
    to
    int dimmerSetting = 100 ;

    Then comment out the request call in the setup section.

    //request( 0, V_PERCENTAGE );

    Now when the node first boots it should turn the lights on to 100% when the switch is used. However once you have set the dimmer it will use that percentage instead.

    I know this is not how you want the final node to work, this is just to see if the request is what is causing the trouble.



  • Hi
    Instead of trying to solve the problem for 1 Led stripe I've decided to go with 6 pcs now - basically I'm gonna need this anyway to do my "high-end" stairs.

    So what I've done is I've combined Your sketch with some other especially the part with few led's declaration

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_RF24_CE_PIN 49
    #define MY_RF24_CS_PIN 53
    
    #define MY_NODE_ID 153 
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define SN "DimmableLED"
    #define SV "1.1"
    
    #define noLEDs 6
    const int LED_Pin[] = {3, 5, 6, 9, 10, 11}; 
    
    //#define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    int dimmerSetting = 100 ;
    
    MyMessage dimmerMsg(noLEDs, V_PERCENTAGE);
    MyMessage lightMsg(noLEDs, V_STATUS);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()
    {
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
     for (int sensor=1; sensor<=noLEDs; sensor++){
        //request( sensor, V_PERCENTAGE );
    }
    }
    void presentation() {
      // Register the LED Dimmable Light with the gateway
     for (int sensor=1; sensor<=noLEDs; sensor++){
     present(sensor, S_DIMMER);
     wait(2);
     }
      sendSketchInfo(SN, SV);
    }
    
    
    /***
     *  Dimmable LED main processing loop
     */
    void loop()
    {
      }
    
    
    /*
    void receive(const MyMessage &message)
    {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
        //  Retrieve the power or dim level from the incoming request message
        int 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 > 100 ? 100 : requestedLevel;
        requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
        Serial.print( "Changing level to " );
        Serial.print( requestedLevel );
        Serial.print( ", from " );
        Serial.println( currentLevel );
    
        fadeToLevel( requestedLevel );
    
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        send(lightMsg.set(currentLevel > 0));
    
        // hek comment: Is this really nessesary?
        send( dimmerMsg.set(currentLevel) );
    
    
      }
    }
    */
    
    
    
    void receive(const MyMessage &message) {
    switch (message.type) {
     
      case V_STATUS:                                           // message is from the switch
       if(message.getBool()){
         fadeToLevel( dimmerSetting, message.sensor );                         // turn light on at current dimmer level
       }
       else fadeToLevel( 0, message.sensor  );                                  // fade light to 0 (off)
      break;
     case V_PERCENTAGE:                                        // message is from the dimmer
       dimmerSetting = message.getInt();                       // get the new dimmer setting from the message
       fadeToLevel( dimmerSetting, message.sensor );                           // fade to the new dimmer setting
       send(lightMsg.set(dimmerSetting > 0 ? 1 : 0), false);   // send switch  state to controller , no ack requested
       break;
      }
      }
    
    
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel( int toLevel, int ledid ) {
    {
    
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( LED_Pin[ledid-1], (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    }
    

    So I have right now 6 LEDs in Domoticz connected to Arduino Mega 3,5,6,9,10,11 pins ( I hope all of them are PWM)
    0_1512688473577_c9b710db-903c-4bc1-9e15-3f48ff751a3a-obraz.png
    0_1512688401483_c08f1a13-3a87-4f1c-a318-31716414f0b3-obraz.png
    At first usage It behaves crazy - It's like every switch needs the first on and off sequence to start behave normally.
    What happens then:
    LED 1 ,2, 3, 5 - all work ok , every single one saves the last state when switch off/on , dimmer bar works fine
    now the best part 😄
    LED 4 - when I switch it On (mouse click in Domoticz) LED is going ON (physically) but the same time LED 1,2,3 is going ON (but only in Domoticz so let's say they are virtually ON but in reality OFF) - dimmer bar LED 4 works ok , saves the last status, You can switch it off but 1,2,3 will be still ON (you have to click each one to take off the virtual status)
    LED 6 - works very similar to 1,2,3,5 - when I move the dimmer bar to e.g. 15% LED it is reacting it is ok BUT suddenly after 1-2 seconds dimmer bar goes to full range (led still shines 15%)
    LED 6 Is also unique - if I play with any of the switches and dimmer (1,2,3,4,5) this one is going virtually ON (domoticz reporting it is on - and its bar is going to full range)

    Please help me to understand where is this behavior coming from.
    Honestly I am not a programmer I am not even sure If the sketch which I combined is anyhow feasible.
    I had to add some messages so I added everywhere after Your dimmerSetting a line.. message.sensor - but I have no idea what it does. Not sure if it should be message.sensor or something else.

    fadeToLevel( dimmerSetting, message.sensor )
    and this
    fadeToLevel( 0, message.sensor  )
    and this
    fadeToLevel( dimmerSetting, message.sensor 
    

    I am thinking maybe I can sacrifice "save the last dimmer value" - just to get this leds working normal 😞 😞 😞

    Help Anybody !
    P.S
    I can paste my debug but there is so many problems I dont even know which bahavior case ( LED 4 or LED 6 or LED1 etc..) I should paste from my debug


  • Hero Member

    @plantex said in Dimmable LED help needed !:

    Hi
    Instead of trying to solve the problem for 1 Led stripe I've decided to go with 6 pcs now - basically I'm gonna need this anyway to do my "high-end" stairs.

    So what I've done is I've combined Your sketch with some other especially the part with few led's declaration

    I do not think it is a good idea to add complexity to an already non working sketch. You should concentrate on getting the single sketch to work first and then add more later.

    I am thinking maybe I can sacrifice "save the last dimmer value" - just to get this leds working normal 😞 😞 😞

    You should also have a clear idea of how you want the lights to work before you proceed any further. It will certainly make a difference to the coding.

    P.S
    I can paste my debug but there is so many problems I dont even know which bahavior case ( LED 4 or LED 6 or LED1 etc..) I should paste from my debug

    This is why you should keep it simple and get that to work first



  • @Boots33 I think I wasn't precise enough.
    So below the sketch which is working fine but for one LED stripe. It remembers the last dimmer value.
    I 've changed it a little bit. It works better for me to receive and display in Domoticz a % dimmer value info instead of information ON/OFF

    send(lightMsg.set(dimmerSetting > 0 ? 1 : 0), false)  changed to  send(dimmerMsg.set(dimmerSetting));
    

    Now my target is to use the maximum quantity of PWM pins in Arduino MEGA (in total I need 16 so I think I am gonna need two Arduinos)
    Anyway could You help me please to add extra LEDs into this sketch ?

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_RF24_CE_PIN 49
    #define MY_RF24_CS_PIN 53
    
    #include <MySensors.h>
    
    #define SN "Dimmable_LED"
    #define SV "1.1"
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    int dimmerSetting = 10 ;
    
    MyMessage dimmerMsg(0, V_PERCENTAGE);
    MyMessage lightMsg(0, V_STATUS);
    
    
    /***
    * Dimmable LED initialization method
    */
    void setup()
    {
     // Pull the gateway's current dim level - restore light level upon sendor node power-up
     //request( 0, V_PERCENTAGE );
    }
    
    void presentation()
    {
     // Register the LED Dimmable Light with the gateway
     present( 0, S_DIMMER );
    
     sendSketchInfo(SN, SV);
    }
    
    /***
    *  Dimmable LED main processing loop
    */
    void loop()
    {
    }
    
    void receive(const MyMessage &message) {
    switch (message.type) {
    
     case V_STATUS:                                           // message is from the switch
      if(message.getBool()){
        fadeToLevel( dimmerSetting );                         // turn light on at current dimmer level
      }
      else fadeToLevel( 0 );                                  // fade light to 0 (off)
     break;
    case V_PERCENTAGE:                                        // message is from the dimmer
      dimmerSetting = message.getInt();                       // get the new dimmer setting from the message
      fadeToLevel( dimmerSetting );                           // fade to the new dimmer setting
       send(dimmerMsg.set(dimmerSetting));                    // send switch  state to controller , no ack requested
      break;
      }
     }
    
    
    /***
    *  This method provides a graceful fade up/down effect
    */
    void fadeToLevel( int toLevel )
    {
    
     int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
     while ( currentLevel != toLevel ) {
       currentLevel += delta;
       analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
       wait( FADE_DELAY );
     }
    }
    

Log in to reply
 

Suggested Topics

  • 5
  • 2
  • 2
  • 1
  • 5
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts