Go Down

Topic: Need Help Arduino Array (Read 1 time) previous topic - next topic

ash901226

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,switch..case, 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?

guix

#1
Dec 24, 2012, 10:28 am Last Edit: Dec 24, 2012, 10:57 am by guix Reason: 1
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 :)

ash901226

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.

Coding Badly


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

ash901226

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

mybe in pseudo code something like
Code: [Select]

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?

AWOL

The concept is sound, but some of the details need working on
Code: [Select]
ledState[i] != ledState[i];
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

guix

#6
Dec 24, 2012, 11:20 am Last Edit: Dec 24, 2012, 11:22 am by guix Reason: 1
Quote
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.

Code: [Select]

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 );
 }
}

ash901226

#7
Dec 24, 2012, 11:36 am Last Edit: Dec 24, 2012, 11:43 am by ash901226 Reason: 1
Thanks Awol,
so i cant use
Code: [Select]
ledState[i] != ledState[i];
is there any work around?

maybe something like
Code: [Select]
int state!= ledState[i];
digitalWrite (led[i],state);


edit:
is the only part that is wrong with my code is that i use
Code: [Select]
ledState[i] != ledState[i];
whereas i should have use
Code: [Select]
ledState[i] = !ledState[i];

ash901226

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

AWOL

Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

guix


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..

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/structures/

ash901226

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

Jimmy60

Here's a hint, this line:

Code: [Select]
  ledState[i] != ledState[i];

Is basically the same as this line:

Code: [Select]
;

lloyddean

#13
Dec 24, 2012, 07:20 pm Last Edit: Dec 24, 2012, 07:23 pm by lloyddean Reason: 1
See if you understand this, compiled but otherwise untested, example.

Code: [Select]

#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
};


// <http://arduino.cc/playground/Code/TimingRollover>

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);
   }
}

Go Up