Need Help Arduino Array

Hello, Hi there All, I'm Ash, Seriously i don't know if the introduction is necessary but it is my custom introduce my self in advance. Anyway i been using arduino for couple of years now but i still feel that i just manage to scratch the surface of its capability. all this years i have manage to make all my program using the if, if...else,, while and finally do...while. i even manage to control arduino using the bit manipulation. All this while i been dreading the fact that i don't know how to use array. To tell you the truth i been reading all kind of book on array and how to use it but in my humble of mind always manage to make a program that skip all the array in the world.

So my question in this post is really what kind of project that i can do that will make use of array and how does array help to simplify the programming stage?

Hello :)

Arrays are really useful, I can't imagine a program without them..or a very small and simple program maybe!

Using a for loop to set or get all values in an array or multidimensional array can be done with 2 or very few lines of code, while without arrays, the same result would be done by manually writing maybe hundreds of lines.

They are also good to keep things organizated, used in combination with enums or structs, or pointers to arrays.. Really they are like the best feature of almost any programming languages.

As for an example, I don't know...the most basic program I write would use arrays anyway :)

hi guix, Yes i guess that array will simplify the program substantially but what project would you recommend? I still havent got a clue about array and i really want to know how to effectively use array in my programming. I learn by doing so please guide me.

Get three (or more) LEDs blinking at different prime-number rates (103ms, 293ms, 487ms).

Hello Coding Badly,
Urm i like your idea. Does the array suppose to keep the time value?

mybe in pseudo code something like

int led[3]={2,3,4};
int ledState[3]={LOW,LOW,LOW};
long timearray[3]={103, 293, 487};
unsigned long currentMillis = millis(); 
  for (i=0;i<3;i++)
  if(currentMillis - previousMillis >timearray[i]) {
    ledState[i] != ledState[i];
    digitalWrite(led[i], ledState[i]);
 previousMillis = currentMillis;

code something like this work? or atleast tell me that i have the basic idea in my head?

The concept is sound, but some of the details need working onledState[i] != ledState[i];

or atleast tell me that i have the basic idea in my head?

Yes, I suggest a few changes (so you can learn even more :))

Use a global constant for the size of the array, this way if you need to modify that number, you only modify it at one place
Use a struct to organize all the data of a LED and use only one array.

const uint8_t MAX_LEDS = 3;

struct LED
  uint8_t pin;
  bool state;
  uint32_t time;

LED leds[ MAX_LEDS ] = 
  { 2, LOW, 103 },
  { 3, LOW, 293 },
  { 4, LOW, 487 },


for ( uint8_t i = 0; i < MAX_LEDS; i++ )
  if( currentMillis - previousMillis > leds[i].time)
    leds[i].state = !leds[i].state;
    digitalWrite( leds[i].pin,  leds[i].state );

Thanks Awol, so i cant use

ledState[i] != ledState[i];

is there any work around?

maybe something like

 int state!= ledState[i]; 
digitalWrite (led[i],state);

edit: is the only part that is wrong with my code is that i use

 ledState[i] != ledState[i];

whereas i should have use

 ledState[i] = !ledState[i];

wow guix, i must say i never seen any programming that use array like yours. could you teach me more about your style.

so i cant use ledState[i] != ledState[i];

You can use it, and the compiler won't complain, but it isn't what you want. Look at guix's code.

ash901226: wow guix, i must say i never seen any programming that use array like yours. could you teach me more about your style.

It's not "my" style :)

Here are 2 useful links, read them and you will learn more than if I try to explain to you with my bad english..

i read that post b4 but i seriously didn’t understand what it means but when you (guix) give your example ok now i see how it works.
i took a couple of minutes to understand your code but when i do it hit me, just think how much time i have wasted doing such long program just to achieve the same outcome

Here's a hint, this line:

  ledState[i] != ledState[i];

Is basically the same as this line:


See if you understand this, compiled but otherwise untested, example.

#define ELEMENT_COUNT(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinLED_1    = 2;
const uint8_t   pinLED_2    = 3;
const uint8_t   pinLED_3    = 4;
const uint8_t   pinLED_4    = 5;

struct element_t
    const uint8_t           _pin;
    const unsigned long     _dt;
    unsigned long           _dtTigger;

element_t   blinker[] =
      { pinLED_1, 103UL, 0 }
    , { pinLED_2, 293UL, 0 }
    , { pinLED_3, 487UL, 0 }
//  , { pinLED_4, 600UL, 0 }    //  uncomment, easily add more elements and code adapts

// <>

bool isBlinkenTime(unsigned long tmsRef, unsigned long tmsTarget)
    return ((long)(tmsRef - tmsTarget) >= 0);

void loop()
    unsigned long    dt = millis(); 

    for ( int i = ELEMENT_COUNT(blinker); i--; )
        if ( isBlinkenTime(dt, blinker[i]._dtTigger) )
            blinker[i]._dtTigger += blinker[i]._dt;

            digitalWrite(blinker[i]._pin, !digitalRead(blinker[i]._pin));

void setup()
    unsigned long   dt = millis(); 

    for ( int i = ELEMENT_COUNT(blinker); i--; )
        pinMode(blinker[i]._pin, OUTPUT);

        blinker[i]._dtTigger = dt + blinker[i]._dt;

        digitalWrite(blinker[i]._pin, LOW);