Create multiple instance in a for() loop: a C++ question


  • Contest Winner

    my goal is to use a for() loop later in the sketch to check for messages, incrementing through the NUMBER_OF_VALVES. I want a scalable program based on a single #define

    So, I want to be able to create multiple instances of the MyMessage class, based on the #define:

    void (*msg1valve[ NUMBER_OF_VALVES + 1 ] )();
    void (*var1valve[ NUMBER_OF_VALVES + 1 ] )();
    void (*var2valve[ NUMBER_OF_VALVES + 1 ] )();
    
    MyMessage msg1valve0(0,V_LIGHT);
    MyMessage var1valve0(0,V_VAR1);
    MyMessage var2valve0(0,V_VAR2);
    
    MyMessage msg1valve1(1,V_LIGHT);
    MyMessage var1valve1(1,V_VAR1);
    MyMessage var2valve1(1,V_VAR2);
    
    MyMessage msg1valve2(2,V_LIGHT);
    MyMessage var1valve2(2,V_VAR1);
    MyMessage var2valve2(2,V_VAR2);
    //etc... etc...
    

    how can I do that programmatically versus copying the constructor many times?

    Something like this:

    for (int i = 0; i <=MUMBER_OF_VALVES; i++)
    {
      MyMessage msg1valve[i](i,V_LIGHT);
      MyMessage var1valve[i](i,V_VAR1);
      MyMessage var2valve[i](i,V_VAR2);
    }
    

    any suggestions?


  • Contest Winner

    @BulldogLowell so I tried this... suggestion by Nick Gammon:

    MySensor gw;
    //
    void (*msg1valve[ NUMBER_OF_VALVES + 1 ] )();
    void (*var1valve[ NUMBER_OF_VALVES + 1 ] )();
    void (*var2valve[ NUMBER_OF_VALVES + 1 ] )();
    //
    void setup() 
    { 
      for (int i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        msg1valve[i] = new MyMessage (i,V_LIGHT);
        var1valve[i] = new MyMessage (i,V_VAR1);
        var2valve[i] = new MyMessage (i,V_VAR2);
      }
    

    still doesn't compile:

    Sprinkler_V2.0_MySensors_V1.4.1.ino: In function 'void setup()':
    Sprinkler_V2.0_MySensors_V1.4.1.ino:103: error: cannot convert 'MyMessage*' to 'void ()()' in assignment
    Sprinkler_V2.0_MySensors_V1.4.1.ino:104: error: cannot convert 'MyMessage
    ' to 'void ()()' in assignment
    Sprinkler_V2.0_MySensors_V1.4.1.ino:105: error: cannot convert 'MyMessage
    ' to 'void (*)()' in assignment


  • Hardware Contributor

    Try this...

    #include <SoftSPI.h>
    #include <MySensor.h>  
    MySensor gw;
    
    
    #define NUMBER_OF_VALVES  4
    MyMessage *msg1valve[ NUMBER_OF_VALVES + 1 ] ; 
    MyMessage *var1valve[ NUMBER_OF_VALVES + 1 ] ;
    MyMessage *var2valve[ NUMBER_OF_VALVES + 1 ] ;
    
    
    void setup() 
    { 
      for (int i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        msg1valve[i] = new MyMessage (i,V_LIGHT);
        var1valve[i] = new MyMessage (i,V_VAR1);
        var2valve[i] = new MyMessage (i,V_VAR2);
      }
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    }
    

    Edward


  • Admin

    Why instantiate so many variables up front? IMHO it's a waste of memory.

    You can do like in the temperature demo sketch:

    #define MAXSENS 3
    MyMessage msg1valve(0,V_LIGHT);
    MyMessage var1valve(0,V_VAR1);
    MyMessage var2valve(0,V_VAR2);
    
    void sendMsg() {
           for (int i=0; i<MAXSENS; i++) {
           gw.send(msg1valve.setSensor(i).set(VALUE));
           gw.send(var1valve.setSensor(i).set(VALUE));
           gw.send(var2valve.setSensor(i).set(VALUE));
    }

  • Contest Winner

    @meanpenugin

    yes!

    got it from Nick too.

    Thanks!!


  • Contest Winner

    @tbowmo said:

    Why instantiate so many variables up front? IMHO it's a waste of memory.

    You can do like in the temperature demo sketch:

    #define MAXSENS 3
    MyMessage msg1valve(0,V_LIGHT);
    MyMessage var1valve(0,V_VAR1);
    MyMessage var2valve(0,V_VAR2);
    
    void sendMsg() {
           for (int i=0; i<MAXSENS; i++) {
           gw.send(msg1valve.setSensor(i).set(VALUE));
           gw.send(var1valve.setSensor(i).set(VALUE));
           gw.send(var2valve.setSensor(i).set(VALUE));
    }
    

    it is an irrigation controller that will extend to 16 valves.

    I am creating multiple devices so that an irrigation controller can turn on/off any zone, so I need NUMBER_OF_VALVES Vera devices.

    I had to working pretty well with the old MySensors version, I had to dust it off and update it so I wanted to make the program as extensible as possible and post it to the forum... almost done.


  • Admin

    @BulldogLowell

    Still, instantiate 16+ variables up front, instead of just 1, where you set sensor child number when you send it?


  • Contest Winner

    So maybe its the jet lag... I cannot see what I am doing wrong here...

    #include <MySensor.h>  
    #include <SPI.h>
    MySensor gw;
    
    
    #define NUMBER_OF_VALVES  4
    MyMessage *msg1valve[ NUMBER_OF_VALVES + 1 ] ; 
    MyMessage *var1valve[ NUMBER_OF_VALVES + 1 ] ;
    MyMessage *var2valve[ NUMBER_OF_VALVES + 1 ] ;
    
    MyMessage msg(0,1);
    
    void setup() 
    { 
      for (int i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        msg1valve[i] = new MyMessage (i,V_LIGHT);
        var1valve[i] = new MyMessage (i,V_VAR1);
        var2valve[i] = new MyMessage (i,V_VAR2);
      }
      
    }
    void loop() 
    {
      gw.process();
      gw.send(msg1valve[0].set(true));
      while(true)
      {
      }
    }
    

    compiler complains:

    Arduino: 1.0.6 (Mac OS X), Board: "Arduino Uno"
    sketch_nov14a.ino: In function 'void loop()':
    sketch_nov14a:28: error: request for member 'set' in 'msg1valve[0]', which is of non-class type 'MyMessage*'


  • Admin

    This compiles.

    #include <MySensor.h>  
    #include <SPI.h>
    MySensor gw;
    
    
    #define NUMBER_OF_VALVES  4
    MyMessage msg1valve[ NUMBER_OF_VALVES + 1 ] ; 
    MyMessage var1valve[ NUMBER_OF_VALVES + 1 ] ;
    MyMessage var2valve[ NUMBER_OF_VALVES + 1 ] ;
    
    MyMessage msg(0,1);
    
    void setup() 
    { 
    }
    void loop() 
    {
      gw.process();
      gw.send(msg1valve[0].set(true));
      while(true)
      {
      }
    }
    //compiler complains:
    

  • Contest Winner

    thanks @hek

    lemme work with that for a while


Log in to reply
 

Suggested Topics

21
Online

11.2k
Users

11.1k
Topics

112.5k
Posts