Move functions



  • I have merged two sketches together.

    The first one was on switches and the second one was if/else if
    So the first sketch was something like

    Switch
    case1
    case2
    End switch
    
    Sendcommand();
    Send status();
    

    And after moving in the second sketch it became

    Switch
    case1
    case2
    newcase3
    newcase4
    End switch
    
    Sendcommand();
    Send status();
    

    So now - everytime i issue commands that satisfy newcase3 and 4 - these two functions get executed. So i wonder how i can git rid of that... Can i just move them to the end of switch1 and 2 code?


  • Mod

    @moskovskiy82 yes

    You might want/need to add them to the default: case as well, if they should be run even if case1: and case2: didn't happen.



  • @moskovskiy82 I am a bit confused on your question. Can you post your whole sketch and explain more of what you want/don't want to happen?



  • Ok this is the part of sketch in question.
    You can see there are two functions in the end
    sendHeatpumpCommand();
    sendNewStateToGateway();
    They only concern HVAC and send an update to the AC. But they get called each time i change for example brightness for the RGB strip

    void receive(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
         //return;
      }
    
      Serial.print("Incoming message for: ");
      Serial.print(message.sensor);
      int val; //RGB VAR  
      String recvData = message.data;
      recvData.trim();
    
      Serial.print(", New status: ");
      Serial.println(recvData);
      switch (message.type) 
      {
        case V_HVAC_SPEED:
          Serial.println("V_HVAC_SPEED");
    
          if(recvData.equalsIgnoreCase("auto")) 
        {
         FAN_STATE = 0;
         HA_FAN_STATE = "Auto";
        }
          else if(recvData.equalsIgnoreCase("min")) 
          {
             FAN_STATE = 1;
             HA_FAN_STATE = "Min";
            }
          else if(recvData.equalsIgnoreCase("normal")) 
          {
             FAN_STATE = 2;
             HA_FAN_STATE = "Normal";
            }
          else if(recvData.equalsIgnoreCase("max")) 
          {
             FAN_STATE = 3;
             HA_FAN_STATE = "Max";
            }
        break;
        case V_HVAC_SETPOINT_COOL:
          Serial.println("V_HVAC_SETPOINT_COOL");
          TEMP_STATE = message.getFloat();
          Serial.println(TEMP_STATE);
        break;
        case V_HVAC_FLOW_STATE:
          Serial.println("V_HVAC_FLOW_STATE");
          if (recvData.equalsIgnoreCase("coolon")) 
          {
             POWER_STATE = 1;
             MODE_STATE = MODE_COOL;
             HA_MODE_STATE = "CoolOn";
            }
          else if (recvData.equalsIgnoreCase("heaton")) 
          {
             POWER_STATE = 1;
             MODE_STATE = MODE_HEAT;
             HA_MODE_STATE = "HeatOn";
            }
          else if (recvData.equalsIgnoreCase("autochangeover")) 
          {
             POWER_STATE = 1;
             MODE_STATE = MODE_AUTO;
             HA_MODE_STATE = "AutoChangeOver";
            }
          else if (recvData.equalsIgnoreCase("off"))
          {
             POWER_STATE = 0;
             HA_MODE_STATE = "Off";
            }
          initialValueSent = true;
        break;
      case V_RGB:
         {
         Serial.println( "V_RGB command: " );
         Serial.println(message.data);
         long number = (long) strtol( message.data, NULL, 16);
         // Save old value
         strcpy(rgbstring, message.data);
         // Split it up into r, g, b values
         red = number >> 16;
         green = number >> 8 & 0xFF;
         blue = number & 0xFF;
         send_status();
         set_hw_status();
        }
      break;
      case V_LIGHT:
        {
         Serial.println( "V_LIGHT command: " );
         Serial.println(message.data);
         val = atoi(message.data);
         if (val == 0 or val == 1) 
        {
             on_off_status = val;
             send_status();
             set_hw_status();
            }
        }
      break;
      case V_DIMMER: 
        {
        Serial.print( "V_DIMMER command: " );
        Serial.println(message.data);
        val = atoi(message.data);
        if (val >= 0 and val <=100) 
        {
          dimmerlevel = val;
          send_status();
          set_hw_status();
        }
        }
      break;
      case V_VAR1:
        {
        Serial.print( "V_VAR1 command: " );
        Serial.println(message.data);
        val = atoi(message.data);
        if (val >= 0 and val <= 2000) 
        {
          fadespeed = val;
        }
        }
      break;
      }  
      
      sendHeatpumpCommand();
      sendNewStateToGateway();
    }
    

  • Mod

    @moskovskiy82 move them to just before break; in the cases you want them to run?



  • Thank you. Will do so and report back


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.