Create multiple instance in a for() loop: a C++ question
-
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?
-
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?
@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 -
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
-
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)); } -
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
-
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)); }@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.
-
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*' -
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: -
thanks @hek
lemme work with that for a while
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login