Pages: [1]   Go Down
Author Topic: Need Help Arduino Array  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

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 smiley
« Last Edit: December 24, 2012, 04:57:26 am by guix » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12774
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

mybe in pseudo code something like
Code:
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The concept is sound, but some of the details need working on
Code:
ledState[i] != ledState[i];
Logged

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

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley)

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:
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 );
  }
}
« Last Edit: December 24, 2012, 05:22:58 am by guix » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Awol,
so i cant use
Code:
ledState[i] != ledState[i];
is there any work around?

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


edit:
is the only part that is wrong with my code is that i use
Code:
ledState[i] != ledState[i];
whereas i should have use
Code:
ledState[i] = !ledState[i];
« Last Edit: December 24, 2012, 05:43:19 am by ash901226 » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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/
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 362
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a hint, this line:

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

Is basically the same as this line:

Code:
;
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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);
    }
}
« Last Edit: December 24, 2012, 01:23:37 pm by lloyddean » Logged

Pages: [1]   Go Up
Jump to: