Creating Instance of a Class with for loop


happy new year everyone!

i'm working on project i'm using own class, become library. ability create instances of class "dynamically" loop , call class loop. searching last night , unable find on internet , wondering if maybe of help. have provide snippets below in order understand i'm doing. example using 4 (4) instances, there as 16 or more.

top of sketch global variables:

code: [select]

#include <softwareserial.h>

const int n = 4; //number of toggles attached
const int = n+1; //the size of array in order ignore position [0]
#define toggledelay 100 //delay toggle ~ 1/10 of second


/* toggle variables */
const int togglepin[as] = {0, 2, 3, 4, 5}; //digital pins toggles attached to
int toggleinput[as]; //value read toggle @ input
int togglelast[as]; //last known toggle state
int togglestate[as]; //display , useable state of toggle

/* serial variables */
int serialstate[as]; //toggle state via serial
boolean serial[as]; //boolean see if state changed serial


arduino setup function:

code: [select]

void setup() {

 //dynamic instance of class
 for(int = 1; <= n; i++) {

  /*

  used create class instance based on variable "i", able have instance
  have custom name, includes variable "i". instance have pass togglepin[i].

 */
}
   
//start serial
serial.begin(9600);

//start functions
for(int = 0; <= n; i++) {

/*

the purpose of last known toggle states master on serial connection,
so function update class global variables known state.
i make dynamic something:

custom[i].startup();

*/

}


}


arduino loop function:

code: [select]

void loop() {
 
 for(int = 1; <= n; i++) {

/*

this same condition startup function in setup function. loop
in custom class.

custom[i].update();

*/
 
}

 
 //delay
 delay(toggledelay);

 for(int = 1; <= n; i++) {

/*

this class updates sketch variable togglelast[i]

togglelast[i] = custom[i].laststate();

*/
 
}


i hope explains i'm trying do.

thanks in advance help,

chris

i think may misusing word dynamic , may throwing me off.  dynamic in sense means instances created , destroyed in heap memory using malloc.  don't think that's want because this:

code: [select]
used create class instance based on variable "i", able have instance
  have custom name, includes variable "i".


you can't think custom names.  have understand once code compiles , uploaded board variable "names" long gone , replaced memory addresses.  names there convenience when writing code.  so, no, can't use  in name anywhere.

but sounds want , looks syntax try use later:

code: [select]
custom[i].update();

is want array of objects.  that's totally doable.  create array of instances of class create array of else. 


Arduino Forum > Using Arduino > Programming Questions > Creating Instance of a Class with for loop


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor