Go Down

Topic: How to repeat global declaration? (Read 460 times) previous topic - next topic

aminbahar

Hi,

I am searching for a method, on how can we repeat the global declaration of , let say-  bytes.

Here's the code:

Code: [Select]

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

//holders for information you're going to pass to shifting function
byte mod1;
byte mod2;
byte mod3;
byte mod4;
byte mod5;
byte mod6;
byte mod7;
byte mod8;
byte mod9;
byte mod10;

const uint16_t running[] PROGMEM= {
B11111111,B11111111,B11111111,B11111111,
}

void setup() {
............
}


void loop() {
.....
mod1 = pgm_read_byte(&(running[0]));
mod2 = pgm_read_byte(&(running[1]));   .......// I will be using the global declared byte here

............

shiftOut(dataPin, clockPin, mod1); 


}



As you can see, I have the same repetition

mod1, mod2, mod3 to be declared at the top (global)....

rather than manually declare it, is it possible for me to use for loop?

Thank you in advance.



Groove

No, it isn't possible.
Have a look at using arrays.
http://arduino.cc/en/Reference/Array
Per Arduino ad Astra

aminbahar

Thank you Groove.

I've read that.

And I've found the way to shorten the code using

Code: [Select]
    for(int i=noofmodules-1; i>=0; i--){
    byte k= pgm_read_byte(&(running[j+i]));
    Serial.println(k);
    shiftOut(dataPin, clockPin, k);
    }


Go Up