Help with 2 LED Dimmer Sketch. Can't compile, not sure why exactly



  • Hi,

    I'm trying to duplicate the LED dimmer sketch, and have one dimmer on pin 3 and the second on pin 4. So far, from what I can see, everything should work but of course, it's not and I can't compile. I'm getting an error about an int not being defined, even though it is.

    Could someone take a second look at this please and see if I went wrong somewhere?

    Thanks!

    /**
     * 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 - 6-December-2015, Derrick Rockwell
     *
     * DESCRIPTION
     *This sketch uses two 30 bulb, 3mm LED holiday light strings. These
     *can usually be found in most dollar stores/hardware stores and are 
     *battery powered. This sketch uses digital pins 3 and 4 to power two 
     *a string of lights each. Ex, one can control white lights the other
     *could control multi-colour lights. 
     */
    
    #define SN "HolidayLEDDeskLights"
    #define SV "1.0"
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define MLED_PIN 3      // Pin Multi-Coloured LED's will attached to
    #define WLED_PIN 4      // Pin White-Coloured LED's will attached to
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    #define CHILD_ID0 3
    #define CHILD_ID1 4
    
    MySensor gw;
    
    static int currentLevelM = 0;  // Current dim level...
    static int currentLevelW = 0;  // Current dim level...
    
    MyMessage dimmerMsgM(MLED_PIN, V_DIMMER);
    MyMessage lightMsgM(MLED_PIN, V_LIGHT);
    MyMessage dimmerMsgW(WLED_PIN, V_DIMMER);
    MyMessage lightMsgW(WLED_PIN, V_LIGHT);
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      Serial.println( SN ); 
      Serial.println( SV );
      gw.begin( incomingMessage );
      // gw.begin( incomingMessageW );
      
      // Register the LED Dimmable Light with the gateway
      gw.present( MLED_PIN, S_DIMMER );
      gw.present( WLED_PIN, S_DIMMER );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( MLED_PIN, V_DIMMER );
      gw.request( WLED_PIN, V_DIMMER );
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    //For Multi Coloured Lights
    void incomingMessage(const MyMessage &message) {
    if (message.type == V_LIGHT || message.type == V_DIMMER) {
          if (message.sensor <= 2){
              //0: All Dimmers
              //1: LED 1
              //2: LED 2
              //  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 );
              
              if ((message.sensor == CHILD_ID0) || (message.sensor == 0)){
                fadeToLevelM ( requestedLevel, MLED_PIN );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(lightMsgM.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmerMsgM.set(requestedLevel) );
              }
              if ((message.sensor == CHILD_ID1) || (message.sensor == 0)){
                fadeToLevelW( requestedLevel, WLED_PIN );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(light2Msg.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmer2Msg.set(requestedLevel) );
              }
            }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
     // For Multi Coloured Lights
    void fadeToLevelM( int toLevel ) {
      int delta = ( toLevel - currentLevelM ) < 0 ? -1 : 1;
      while ( currentLevelM != toLevel ) {
        currentLevelM += delta;
        analogWrite( MLED_PIN, (int)(currentLevelM / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    
    void fadeToLevelW( int toLevel ) {
      int delta = ( toLevel - currentLevelW ) < 0 ? -1 : 1;
      while ( currentLevelW != toLevel ) {
        currentLevelW += delta;
        analogWrite( WLED_PIN, (int)(currentLevelW / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    
    
    
    

  • Contest Winner

    @drock1985

    There where a couple of errors try this code

    /**
     * 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 - 6-December-2015, Derrick Rockwell
     *
     * DESCRIPTION
     *This sketch uses two 30 bulb, 3mm LED holiday light strings. These
     *can usually be found in most dollar stores/hardware stores and are 
     *battery powered. This sketch uses digital pins 3 and 4 to power two 
     *a string of lights each. Ex, one can control white lights the other
     *could control multi-colour lights. 
     */
    
    #define SN "HolidayLEDDeskLights"
    #define SV "1.0"
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define MLED_PIN 3      // Pin Multi-Coloured LED's will attached to
    #define WLED_PIN 4      // Pin White-Coloured LED's will attached to
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    #define CHILD_ID0 3
    #define CHILD_ID1 4
    
    MySensor gw;
    
    static int currentLevelM = 0;  // Current dim level...
    static int currentLevelW = 0;  // Current dim level...
    
    MyMessage dimmerMsgM(MLED_PIN, V_DIMMER);
    MyMessage lightMsgM(MLED_PIN, V_LIGHT);
    MyMessage dimmerMsgW(WLED_PIN, V_DIMMER);
    MyMessage lightMsgW(WLED_PIN, V_LIGHT);
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      Serial.println( SN ); 
      Serial.println( SV );
      gw.begin( incomingMessage );
      // gw.begin( incomingMessageW );
      
      // Register the LED Dimmable Light with the gateway
      gw.present( MLED_PIN, S_DIMMER );
      gw.present( WLED_PIN, S_DIMMER );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( MLED_PIN, V_DIMMER );
      gw.request( WLED_PIN, V_DIMMER );
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    //For Multi Coloured Lights
    void incomingMessage(const MyMessage &message) {
    if (message.type == V_LIGHT || message.type == V_DIMMER) {
          if (message.sensor <= 2){
              //0: All Dimmers
              //1: LED 1
              //2: LED 2
              //  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 );
              
              if ((message.sensor == CHILD_ID0) || (message.sensor == 0)){
                fadeToLevel ( requestedLevel, MLED_PIN, &currentLevelM );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(lightMsgM.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmerMsgM.set(requestedLevel) );
              }
              if ((message.sensor == CHILD_ID1) || (message.sensor == 0)){
                fadeToLevel( requestedLevel, WLED_PIN, &currentLevelW );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(lightMsgW.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmerMsgW.set(requestedLevel) );
              }
            }
        }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
     // For Multi Coloured Lights
    void fadeToLevel( int toLevel, int pin, int *currentLevel ) {
      int delta = ( toLevel - (*currentLevel) ) < 0 ? -1 : 1;
      while ( (*currentLevel) != toLevel ) {
        (*currentLevel) += delta;
        analogWrite( pin, (int)((*currentLevel) / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    


  • Hi @BartE

    Thanks for your help. The code compiles, and I think I see what was preventing it (as to the why i'm still not sure). The devices show up in domoticz, and they seem to try and communicate but there is no interaction from the lights (no one/off/dimming). And when I switch one or the other off, it affects the other.

    Will keep looking into this, thanks again!



  • Huh, I think I just got it. Had to remove the if (message.sensor <=2) string, compiled, and works great.

    Thanks again @BartE , major creds to you.

    Final Code (Or, so far final)

    /**
     * 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 - 6-December-2015, Derrick Rockwell
     *
     * DESCRIPTION
     *This sketch uses two 30 bulb, 3mm LED holiday light strings. These
     *can usually be found in most dollar stores/hardware stores and are 
     *battery powered. This sketch uses digital pins 3 and 4 to power two 
     *a string of lights each. Ex, one can control white lights the other
     *could control multi-colour lights. 
     */
    
    #define SN "HolidayLEDDeskLights"
    #define SV "1.0"
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define MLED_PIN 3      // Pin Multi-Coloured LED's will attached to
    #define WLED_PIN 4      // Pin White-Coloured LED's will attached to
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    #define CHILD_ID0 3
    #define CHILD_ID1 4
    
    MySensor gw;
    
    static int currentLevelM = 0;  // Current dim level...
    static int currentLevelW = 0;  // Current dim level...
    
    MyMessage dimmerMsgM(MLED_PIN, V_DIMMER);
    MyMessage lightMsgM(MLED_PIN, V_LIGHT);
    MyMessage dimmerMsgW(WLED_PIN, V_DIMMER);
    MyMessage lightMsgW(WLED_PIN, V_LIGHT);
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      Serial.println( SN ); 
      Serial.println( SV );
      gw.begin( incomingMessage );
      
      // Register the LED Dimmable Light with the gateway
      gw.present( MLED_PIN, S_DIMMER );
      gw.present( WLED_PIN, S_DIMMER );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( MLED_PIN, V_DIMMER );
      gw.request( WLED_PIN, V_DIMMER );
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    //For Multi Coloured Lights
    void incomingMessage(const MyMessage &message) {
    if (message.type == V_LIGHT || message.type == V_DIMMER) {
          // if (message.sensor <= 2)
          {
              //  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 );
              
              if ((message.sensor == CHILD_ID0) || (message.sensor == 0)){
                fadeToLevel ( requestedLevel, MLED_PIN, &currentLevelM );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(lightMsgM.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmerMsgM.set(requestedLevel) );
              }
              if ((message.sensor == CHILD_ID1) || (message.sensor == 0)){
                fadeToLevel( requestedLevel, WLED_PIN, &currentLevelW );
                // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                gw.send(lightMsgW.set(requestedLevel > 0 ? 1 : 0));
                gw.send(dimmerMsgW.set(requestedLevel) );
              }
            }
        }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
     // For Multi Coloured Lights
    void fadeToLevel( int toLevel, int pin, int *currentLevel ) {
      int delta = ( toLevel - (*currentLevel) ) < 0 ? -1 : 1;
      while ( (*currentLevel) != toLevel ) {
        (*currentLevel) += delta;
        analogWrite( pin, (int)((*currentLevel) / 100. * 255) );
        delay( FADE_DELAY );
      }
    }```
    
    EDIT: II always speak too soon. The Multi-coloured lights are dimming, just not the white ones. It's all or nothing.


  • Ok, figured out and it's my stupid. Didn't put the second LED on a PWM pin, therefore, no modulation!


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts