Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Changing a variable name in a for loop?  (Read 490 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

byte varTimerZ1R[16]= {};
byte varTimerZ1Hour[16]= {};
byte varTimerZ1Min[16]= {};

// Is there a way to change the "1" in varTimerZ1R[T], varTimerZ1Hour[T], and varTimerZ1Min[T] in a for loop to shorten code? I have do do this 26times
// Because varZn is 1-26

           if (varZn == 1){
            varTimerZ1R[T] = varR;
            varTimerZ1Hour[T] = H;
            varTimerZ1Min[T] = M;
           }
           else if (varZn == 2){
            varTimerZ2R[T] = varR;
            varTimerZ2Hour[T] = H;
            varTimerZ2Min[T] = M;
           } 
           else if (varZn == 3){
            varTimerZ3R[T] = varR;
            varTimerZ3Hour[T] = H;
            varTimerZ3Min[T] = M;
           } 
           else if (varZn == 4){
            varTimerZ4R[T] = varR;
            varTimerZ4Hour[T] = H;
            varTimerZ4Min[T] = M;
           } 

I know this will not work but something like this
          for (int x=0; x<27; x++){
            varTimerZ"x"R[T] = varR;
            varTimerZ"x"Hour[T] = H;
            varTimerZ"x"Min[T] = M;
          }

Thanks for your help!!
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I have thought about this and
         for (int x=0; x<27; x++){
            varTimerZ"x"R[T] = varR;
            varTimerZ"x"Hour[T] = H;
            varTimerZ"x"Min[T] = M;
          }
Would not even work because it assigns the same value to all 26 so maybe something like this
            int x = varZn;
            varTimerZ"x"R[T] = varR;
            varTimerZ"x"Hour[T] = H;
            varTimerZ"x"Min[T] = M;

But this will not work! Any ideas?
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 56
Posts: 1907
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Variable names cannot be changed.  This is why arrays have indexes... If the name was changeable at runtime, no need for an index.
Logged

Sydney, Australia
Offline Offline
Sr. Member
****
Karma: 6
Posts: 397
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just use a multi dimensional array - the first dimension is the index to your 26 other arrays

Craig
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So maybe something like this

byte varTimerZR[512]= {};           // array 0 would hold the varZn and the next 16 would hold varR
byte varTimerZHour[512]= {};      // array 16 would be the next varZn "2" and the next 16 would hold H
byte varTimerZMin[512]= {};        // array 32 would hold the next varZn "3" and the next 16 would hold M

// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;

what do you think?
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 56
Posts: 1907
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;

what do you think?

Back in the dark ages of computers, CPU time was very expensive.  We would do peer-reviews of code to determine if there were more efficient ways to use resources.  Rarely done today because code is written on workstations that cost the same if used or just idle.

Just compile and test.  You will NOT wear out your Arduino flash.  Make a change, compile, test.  I call this the Calculus of computer programming.  Enjoy the freedom to learnthrough successive approximation.

Don't ask what others think... You may not want to know.  Post specific questions.

Ray
Logged

Sydney, Australia
Offline Offline
Sr. Member
****
Karma: 6
Posts: 397
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So maybe something like this

byte varTimerZR[512]= {};           // array 0 would hold the varZn and the next 16 would hold varR
byte varTimerZHour[512]= {};      // array 16 would be the next varZn "2" and the next 16 would hold H
byte varTimerZMin[512]= {};        // array 32 would hold the next varZn "3" and the next 16 would hold M

// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;


what do you think?

Not Much !!!

declare an array with 3 dimensions as such

byte varTimers[26][3][16]= {};

then reference them in your for loop by having multiple nested indexes to pull and store the values

Craig
Logged

Offline Offline
God Member
*****
Karma: 26
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, chrisnet, you're not too far off there.  That's how multidimensional arrays are stored in memory.  You have pretty much figured out how to do multidimensional arrays of structures using multiple single dimensional arrays.

But C++, and even C, have structures and multidimensional arrays to make it easier.  You don't need to use structures, though.  You can use 3 arrays.  Here's the syntax:

Code:

byte varTimerZR[27][16];
byte varTimerZHour[27][16];
byte varTimerZMin[27][16];

          for (int x=0; x<27; x++){
            varTimerZR[x][T] = varR;
            varTimerZHour[x][T] = H;
            varTimerZMin[x][T] = M;
          }

I hope you're not using an UNO, though.  You'll run out of memory.
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: