NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » led array code

March 27, 2011
by newbie
newbie's Avatar

I am new to c programming. I built the led array and loaded the test program had a few problems corrected them and test work now. I started looking at the educational code and the manual. I get to page 27 and had no idea how to complete the code. I then looked at the completed code and got confused as to what was going on. I understand the array and how it is set up but can somebody walk through the code with met . I know that you are trying to see what the first led is ( on or off) but I don't know how to follow the two if statements.

inline uint8_t ledarray_get(uint8_t i, uint8_t j) { if(i < ROWS && j < COLS) { if((la_data[j] & (1<<i)) != 0) { return 1; } else { return 0; } } else { return 0; } }

This might seem very easy to some but I have been looking at this for a while and just can't seem to get it.

Thanks

Newbie

March 27, 2011
by newbie
newbie's Avatar

Here is the code in correct indent

    inline uint8_t ledarray_get(uint8_t i, uint8_t j) {
      if(i < ROWS && j < COLS) {
        if((la_data[j] & (1<<i)) != 0) {
          return 1;
        } else {
          return 0;
       }
  } else {
return 0;

}

March 28, 2011
by Hexorg
Hexorg's Avatar

read that code like this:

(If "i" is smaller then "ROWS" and "j" is smaller then "COLS") AND (if "i"'s bit in "j"'s row of "la_data" array is set) then return 1, otherwise 0;

Pretty much first we do the sanity check, making sure that "i" and "j" are within the screen boundaries. If they ARE within boundaries, we read the bit according to [i, j] coordinates, and return it.

Nested if {} statements are executed one after the other, as long as previous if {} statement is true.

Hope this helps. Hexorg.

March 29, 2011
by newbie
newbie's Avatar

Sorry for the long delay I just got time to look at the reply. Thanks for the reply. I understand it now . Why is there another else return 0 ? Is it to close the second else? Thanks again

newbie

March 29, 2011
by bretm
bretm's Avatar

Does this formatting make it more clear?

inline uint8_t ledarray_get(uint8_t i, uint8_t j) 
{ 
    if (i < ROWS && j < COLS) 
    { 
        if((la_data[j] & (1<<i)) != 0) 
        { 
            return 1; 
        } 
        else 
        { 
            return 0; 
        } 
    } 
    else 
    { 
        return 0; 
    } 
}

Since "return" causes subsequent statements in the same routine to be skipped, I might have written it like this:

inline uint8_t ledarray_get(uint8_t i, uint8_t j) 
{ 
    if(i >= ROWS || j >= COLS)
        return 0;

    return (la_data[j] & (1<<i)) != 0;
}

Is that more confusing, or less?

March 29, 2011
by Hexorg
Hexorg's Avatar

newbie, here's is a good tutorial about if statements and here is one about functions

if you have some free time, I'd recommend to read all the C - made easy articles.

Actually, do you guys think we should put those tutorial links somewhere on top of the website as a recommendation to read?

April 02, 2011
by newbie
newbie's Avatar

Hello

got side tracked again. Thanks for all your suggestions and help. I see it better with your programming bretm. I can understand both now but it seems to bretm's flows better for me. Also thanks for links Hexorg. I will be looking at them. Thanks

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured in the MIT Undergraduate Research Journal? Learn more...