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 » Silly Coding Question

March 30, 2010
by ecornwell
ecornwell's Avatar

Hello,

I've been racking my brain to come up with some nice looking code to do something and I think I'm making it more difficult than it needs to be. So I got to thinking. Aside from readability, are the following segments of code equal?

for (i=0;i<5;i++) {
 myfunc(i);
 delay_ms(100); }

And

myfunc(i);
delay_ms(100);
myfunc(i);
delay_ms(100);
myfunc(i);
delay_ms(100);
myfunc(i);
delay_ms(100);
myfunc(i);
delay_ms(100);

If I understand things correctly, the compiler will do what it needs for the code so the two blocks above are equal. The human component is what separates it correct?

Now for what I'm trying to do... I was playing around with the LED Array and adding different "test patterns." I wanted to do one that was like concentric squares where it starts at [0][0] and runs the border then when it gets to [1][0] goes to [1][1] and keeps going till it hits the "center" and all LED's are lit. I was trying to do it with some for loops but wasn't getting anywhere.

Thanks!

March 30, 2010
by bretm
bretm's Avatar

They're not equivalent because in the first case the variable "i" takes on the values 0, 1, 2, 3, and 4 for each call to myfunc, and in the second case "i" is undefined (zero unless set to something else) and does not change for each call to myfunc.

I haven't built the LED Array project, but my understanding is that the patterns are fed into one end and scroll horizontally. The individual pixels are not individually addressable but are maintained through shift registers.

March 31, 2010
by Rick_S
Rick_S's Avatar

Actually the displayed data is stored in an array that can be directly manipulated. There are no shift registers used just the mcu to drive the array. The "Patterns" don't have to scroll at all. Lookup my posts for my build and the various functions I added. They are referenced in the NK Newsletter #8.

Rick

March 31, 2010
by ecornwell
ecornwell's Avatar

I realized as I got in the shower this morning that I forgot to change the "i" when I copied and pasted. It would be:

myfunc(0);
delay_ms(100);
myfunc(1);
delay_ms(100);
myfunc(2);
delay_ms(100);
myfunc(3);
delay_ms(100);
myfunc(4);
delay_ms(100);
March 31, 2010
by treymd
treymd's Avatar

There is no silly coding only silly planning. They beat that into our heads in software engineering classes, write the pseudocode/algorithm, implement it, bug-check, rinse and repeat. At least that's what my professors had to say.

But what is our first instinct? Figure out how to get it working and then write the pseudocode. before you know it you have 20,000 lines of code full of bugs and you gotta weed them out.

One of these days (hopefully before I actually am being paid to program) I will learn to do it the right way.

March 31, 2010
by bretm
bretm's Avatar

Actually the displayed data is stored in an array that can be directly manipulated.

Well in that case a spiraling square should be pretty straightforward. What kind of problems are you running into, ecornwell?

March 31, 2010
by BobaMosfet
BobaMosfet's Avatar

Although the two are functionally equivalent, the one without a loop is faster because there is no need to

  1. address & access a counter variable (registered or not), and
  2. no test is being before after each iteration of the loop.

If this isn't time-critical, use the loop.

As for logic, remember, flowcharts are your friends. Flowcharts help you get logic out of your head and into a form that lets you repeatedly walk through the logic and change variables and test. It frees your mind to crunch new data, not waste cycles holding onto what can be put on paper. Only a rank-amateur thumbs their nose at flow-charting.

Once the logic works, then you can work on performance.

BM

March 31, 2010
by BobaMosfet
BobaMosfet's Avatar

Correction to the above:

"2. execute a test after each iteration of a loop."

My apologies-- my fingers are not following my brain :D

BM

April 01, 2010
by ecornwell
ecornwell's Avatar

Thank you all for responding. I think I've got an idea what to do. It's been a while since I've done any real coding and it's amazing how much I've lost. I just wish I had more time to play.

April 09, 2010
by BobaMosfet
BobaMosfet's Avatar

Sadly, it seems to be what you knew best, that slips away first.... :P

BM

Post a Reply

Please log in to post a reply.

Did you know that sound travels via pressure waves in the air, and you can make these with a piezoelectric buzzer? Learn more...