Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Create multiple instance in a for() loop: a C++ question

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

Scheduled Pinned Locked Moved Development
10 Posts 4 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • BulldogLowellB Offline
    BulldogLowellB Offline
    BulldogLowell
    Contest Winner
    wrote on last edited by BulldogLowell
    #1

    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?

    BulldogLowellB 1 Reply Last reply
    0
    • BulldogLowellB BulldogLowell

      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?

      BulldogLowellB Offline
      BulldogLowellB Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        meanpenugin
        Hardware Contributor
        wrote on last edited by
        #3

        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

        BulldogLowellB 1 Reply Last reply
        0
        • tbowmoT Offline
          tbowmoT Offline
          tbowmo
          Admin
          wrote on last edited by
          #4

          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));
          }
          
          BulldogLowellB 1 Reply Last reply
          0
          • M meanpenugin

            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

            BulldogLowellB Offline
            BulldogLowellB Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by
            #5

            @meanpenugin

            yes!

            got it from Nick too.

            Thanks!!

            1 Reply Last reply
            0
            • tbowmoT tbowmo

              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));
              }
              
              BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • tbowmoT Offline
                tbowmoT Offline
                tbowmo
                Admin
                wrote on last edited by
                #7

                @BulldogLowell

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

                1 Reply Last reply
                0
                • BulldogLowellB Offline
                  BulldogLowellB Offline
                  BulldogLowell
                  Contest Winner
                  wrote on last edited by BulldogLowell
                  #8

                  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*'

                  1 Reply Last reply
                  0
                  • hekH Offline
                    hekH Offline
                    hek
                    Admin
                    wrote on last edited by
                    #9

                    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:
                    
                    1 Reply Last reply
                    0
                    • BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #10

                      thanks @hek

                      lemme work with that for a while

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      21

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular