Dimmer led turns on fully and only after dimms



  • I’d like to turn on my mysensor driver led strips on a certain brightness.
    When I do that the led strips turns on on 100% brightness then immediately dimmed down to the wanted level. Is this intentional? Or am I doing something wrong?
    Full sketch for the arduino:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 141
    #define CHILD_ID 123
    #define CHILD_ID2 124
    
    // Enable and select radio type attached
    
    #include <MySensors.h>
    
    #define SN "KitchenCabinet2"
    #define SV "1.5"
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define LED_PIN2 5      // 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...
    static int16_t currentLevel2 = 0;  // Current dim level...
    MyMessage dimmerMsg(CHILD_ID, V_DIMMER);
    MyMessage lightMsg(CHILD_ID, V_LIGHT);
    
    MyMessage dimmerMsg2(CHILD_ID2, V_DIMMER);
    MyMessage lightMsg2(CHILD_ID2, V_LIGHT);
    /***
       Dimmable LED initialization method
    */
    
    
    void setup()
    {
    }
    
    void presentation()
    {
      // Register the LED Dimmable Light with the gateway
      sendSketchInfo(SN, SV);
      present(CHILD_ID, S_DIMMER);
      present(CHILD_ID2, S_DIMMER);
    }
    
    void loop()
    {
      static bool first_message_sent = false;
      if ( first_message_sent == false ) {
        analogWrite( LED_PIN, 0);
        analogWrite( LED_PIN2, 0);
        Serial.println( "Sending initial state..." );
        send(lightMsg.set(currentLevel));
        send(dimmerMsg.set(currentLevel));
    
        send(lightMsg2.set(currentLevel2));
        send(dimmerMsg2.set(currentLevel2));
        first_message_sent = true;
      }
    }
    
    void receive(const MyMessage &message)
    {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
        if (message.sensor == CHILD_ID) {
          //  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( "led1 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) );
        }
    
        if (message.sensor == CHILD_ID2) {
          //  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( "Led2 Changing level to " );
          Serial.print( requestedLevel );
          Serial.print( ", from " );
          Serial.println( currentLevel2 );
    
          fadeToLevel2( requestedLevel );
    
          // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
          send(lightMsg2.set(currentLevel2 > 0));
    
          // hek comment: Is this really nessesary?
          send( dimmerMsg2.set(currentLevel2) );
        }
      }
    }
    
    /***
        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 );
      }
    }
    
    void fadeToLevel2(int toLevel ) {
      int delta = ( toLevel - currentLevel2 ) < 0 ? -1 : 1;
    
      while (currentLevel2 != toLevel ) {
        currentLevel2 += delta;
        analogWrite(LED_PIN2, (int)(currentLevel2 / 100. * 255) );
        delay(FADE_DELAY );
      }
    }
    

  • Mod

    @adam-novak do you mean when the node boots initially, or when it receives a change in level?
    What message are you sending?
    What does the debug output show?



  • nope, not when the node boots.
    so steps:
    have the lights turned off.
    turn the lights on with brightness lower than 100 percentage.

    results:

    1. leds turned on with brightness 100 percentage.
    2. immediately after that leds are dimmed to the given level.

    I'm using home assistant so not really sure what do you mean by what message am I sending.



  • @adam-novak said in Dimmer led turns on fully and only after dimms:

    analogWrite(LED_PIN2, (int)(currentLevel2 / 100. * 255) );

    I would rewrite this to

    analogWrite(LED_PIN2, (int)(currentLevel2 * 255L / 100) );


  • You could add some debug code to see what messages arrive from home assistant, like

    void receive(const MyMessage &message)
    {
      Serial.print ( "received message for sensor " );
      Serial.print ( message.sensor );
      Serial.print ( ", type " );
      Serial.println ( message.type );
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
    

    and this to see if the fading routine works well:

      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        Serial.print ( "...fadeToLevel: currentLevel=" );
        Serial.print ( currentLevel );
        Serial.print ( ", pwm value=" );
        Serial.print (  (int)(currentLevel / 100. * 255) );
        Serial.println ( );
        analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
    

    post your debug output



  • This is the output:
    69082 TSF:MSG:READ,0-0-150,s=151,c=1,t=2,pt=0,l=1,sg=0:1
    69087 TSF:MSG:ACK REQ
    69091 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1
    received message for sensor 151, type 2
    led1 Changing level to 100, from 0
    70106 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    70114 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=3,pt=2,l=2,sg=0,ft=0,st=OK:100
    70121 TSF:MSG:READ,0-0-150,s=151,c=1,t=3,pt=0,l=2,sg=0:16
    70126 TSF:MSG:ACK REQ
    70130 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=3,pt=0,l=2,sg=0,ft=0,st=OK:16
    received message for sensor 151, type 3
    led1 Changing level to 16, from 100
    70984 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    70993 TSF:MSG:SEND,150-150-0-0,s=151,c=1,t=3,pt=2,l=2,sg=0,ft=0,st=OK:16

    fading seems fine, that part is not posted from the logs



  • it seems to me that HA sends to messages. first to turn on 100% than dim to the desired level. ?



  • Yes that seems correct to me. So you could not check for over if the message types, or check why home assistant sends the message twice.
    Probably it sends one for the dimmer value and I've for the switch. So if you only check on the dimmer it should be okay



  • @electrik checked HA source as well, and you are absolutely right.
    I'll just check for the dim message, thanks for the help.


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 17
  • 3
  • 8

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts