A little assistance with sketch?



  • Hey, it's me again! lol

    OK, so I have decided that using my gateway as a sensor too is just too hard for me right now, so I have scrapped that idea. I instead wish to use an arduino that I have controlling an LED strip to also control my 6 RF433 power plugs. I have combined the two sketches and it compiles, but it's acting weird....

    So I have one LED strip and 6 power plugs which are operated via RF433. Whenever I turn on one of the power plugs, the LED strip also turns on. Here is the sketch:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <RCSwitch.h>
    
    #define SN "KitchenLED &RF433 Hub"
    #define SV "1.0"
    
    #define NUMBER_OF_PLUGS 6 // Total number of attached plugs
    
    #define SHORTPULSE 316
    #define LONGPULSE 818
    
    #define CODE_1On 4072574
    #define CODE_1Off 4072566
    #define CODE_2On 4072572
    #define CODE_2Off 4072564
    #define CODE_3On 4072570
    #define CODE_3Off 4072562
    #define CODE_4On 1386894
    #define CODE_4Off 1386886
    #define CODE_5On 1386892
    #define CODE_5Off 1386884
    #define CODE_6On 1386890
    #define CODE_6Off 1386882
    
    #define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 15  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    
    RCSwitch mySwitch = RCSwitch();
    
    void setup() {
      mySwitch.enableTransmit(3);
      mySwitch.setRepeatTransmit(15);
    
      // Pull the gateway's current dim level - restore light level upon node power-up
      request( 0, V_DIMMER );
    }
    
    void presentation()
    {
    
      sendSketchInfo(SN, SV);
    
      // Register the LED Dimmable Light with the gateway
      present( 0, S_DIMMER );
    
      for (int sensor = 1 ; sensor <= NUMBER_OF_PLUGS; sensor++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_LIGHT);
      }
    }
    
    
    void loop()
    {
    
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_LIGHT) {
        int incomingLightState =  message.getBool();
        int incomingOutlet = message.sensor;
    
        Serial.print("Outlet #: ");
        Serial.println(message.sensor);
        Serial.print("Command: ");
        Serial.println(message.getBool());
    
        if (incomingOutlet == 1) {
          if (incomingLightState == 1) {
            // Turn on  socket 1
            Serial.println("Turn on Socket 1");
            mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 1
            Serial.println("Turn off Socket 1");
            mySwitch.send(CODE_1Off, 24);
            delay(50);
          }
        }
        if (incomingOutlet == 2) {
          if (incomingLightState == 1) {
            // Turn on  socket 2
            Serial.println("Turn on Socket 2");
            mySwitch.send(CODE_2On, 24);
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 2
            Serial.println("Turn off Socket 2");
            mySwitch.send(CODE_2Off, 24);
            delay(50);
          }
        }
        if (incomingOutlet == 3) {
          if (incomingLightState == 1) {
            // Turn on  socket 3
            Serial.println("Turn on Socket 3");
            mySwitch.send(CODE_3On, 24);
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 3
            Serial.println("Turn off Socket 3");
            mySwitch.send(CODE_3Off, 24);
            delay(50);
          }
        }
        if (incomingOutlet == 4) {
          if (incomingLightState == 1) {
            // Turn on  socket 4
            Serial.println("Turn on Socket 4");
            mySwitch.send(CODE_4On, 24);
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 4
            Serial.println("Turn off Socket 4");
            mySwitch.send(CODE_4Off, 24);
            delay(50);
          }
        }
        if (incomingOutlet == 5) {
          if (incomingLightState == 1) {
            // Turn on  socket 5
            Serial.println("Turn on Socket 5");
            mySwitch.send(CODE_5On, 24);
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 5
            Serial.println("Turn off Socket 5");
            mySwitch.send(CODE_5Off, 24);
            delay(50);
          }
        }
        if (incomingOutlet == 6) {
          if (incomingLightState == 1) {
            // Turn on  socket 6
            Serial.println("Turn on Socket 6");
            mySwitch.send(CODE_6On, 24);
            delay(50);
          }
          if (incomingLightState == 0)  {
            // Turn off socket 6
            Serial.println("Turn off Socket 6");
            mySwitch.send(CODE_6Off, 24);
            delay(50);
          }
        } delay(50);
      }
      {
        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));
    
        }
      }
    
    }
    
    /***
        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 );
      }
    }
    

    Both individual sketches have "message.type == V_LIGHT" so when I merged them this remained the case. I believe that this is the issue, but I am struggling to work out how to change it and what exactly I should change to get the sketch to work properly.

    Hope someone has a spare couple minutes to review this and point me in the right direction.

    Thanks in advance!



  • Two short remarks:

    • last if-branch in receive() additionally needs clarification to only be meant for ChildID 0; this should "cure" the problem you are working on.
    • For the general structure of your code imo it would help much to use arrays to store all the info.


  • @rejoe2 thanks for your reply. I know the sketch is a little messy; where would be the best place on the internet for me to read about ways to tidy it up?

    And I currently have no idea how to associate the last if statement only to child 0. Can you or someone point me in the right direction as to how to achieve this?

    It wasn't all that long ago that I learnt how to merge sketches, so I know I still have much to learn!



  • As you already did some differnenciation wrt. to the ChildID, this seemed not to be to far beyond your actual knowledge. Sth. like

    if (incomingOutlet == 0 && (message.type == V_LIGHT || message.type == V_DIMMER))
    

    should do the trick😉 .
    Wrt. to arrays to avoid all the repeatings in the code, it's not that easy. Assuming, the send-codes to be longint type of data, sth. like

    const uint16_t RF_CODES[NUMBER_OF_PLUGS] = {
    {4072574,4072566},
    {4072572,4072564},
    {4072570,4072562},
    {1386894,1386886},
    {1386892,1386884},
    {1386890,1386882}
    };
    

    might easen addressing sendCode to sth. like

    RF_CODES[incomingOutlet-1][incomingLightState]
    

    Note: This is completely untested...
    Within MySensors, array coding is not really part of the default sketches, so you should have a look at the normal arduino examples.



  • @rejoe2 thank you!!!!!

    Your suggestions look great to me! I will play around with my sketch with your ideas and I'll let you know how I go!

    Your ideas looks really cool, and will definitely make it more simple, makes it less of a chance to make mistakes.



  • @homer said in A little assistance with sketch?:

    You could replace this.....

    > 
    >     if (incomingOutlet == 1) {
    >       if (incomingLightState == 1) {
    >         // Turn on  socket 1
    >         Serial.println("Turn on Socket 1");
    >         mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
    >         delay(50);
    >       }
    >       if (incomingLightState == 0)  {
    >         // Turn off socket 1
    >         Serial.println("Turn off Socket 1");
    >         mySwitch.send(CODE_1Off, 24);
    >         delay(50);
    >       }
    >     }
    >    
    

    With this....

    >         if (incomingOutlet == 1 && incomingLightState == 1) {
    >         // Turn on  socket 1
    >         Serial.println("Turn on Socket 1");
    >         mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
    >         delay(50);
    >        }
    >        else if (incomingOutlet == 1 && incomingLightState == 0)  {
    >         // Turn off socket 1
    >         Serial.println("Turn off Socket 1");
    >         mySwitch.send(CODE_1Off, 24);
    >         delay(50);
    >        }
    

    But for an even neater solution look into 'switch case' - You'll be glad you did!

    https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/



  • Thanks, I will have a look at your suggestions!

    I've had some stuff happen at home so I am a little under the pump, so I am currently putting together a list of things to look into when I have some more time in a week or so, and your suggestions have been included on that list! I appreciate your help!


 

295
Online

8.6k
Users

9.4k
Topics

99.0k
Posts