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 » .h File for "for loops" and other basic C commands like that?

March 02, 2012
by Neilman30
Neilman30's Avatar

I'm looking for a header file (.h) that contains the code needed so that I can use for loops with my micro-controller. If anyone knows where I can download a file like this please reply!

March 02, 2012
by pcbolt
pcbolt's Avatar

@Neilman30

You don't need a header file for the "for" loops. It is part of the standard "C" language. The nerdkits lcd.c file has some examples of the syntax you'd use.

March 03, 2012
by Neilman30
Neilman30's Avatar

I know it's part of the standard C language but it said it didn't recognize the for loops in command prompt but it's fine I just changed them to while loops.

March 03, 2012
by Neilman30
Neilman30's Avatar

I do have another question though. Can the micro-controller handle multiple conditions in the while loop.

while (a > 1 && a < 5) { 
    Code Here... 
}
March 03, 2012
by RevMoses
RevMoses's Avatar

if you try the temperature sensor code from the PDF that came with your kit, you will see a for loop very similar to the following in the main method

   adc0_init();
    // take 100 samples and average them!
    temp_avg0 = 0.0;
    for(i=0; i<100; i++) 
    {
      last_sample0 = adc_read();
      this_temp0 = sampleToFahrenheit(last_sample0);

      // add this contribution to the average
      temp_avg0 = temp_avg0 + this_temp0/100.0;
    }
March 03, 2012
by pcbolt
pcbolt's Avatar

Hi Neilman30 -

You should be able to use multiple conditions in your while loop. Just to be safe, put each condition in its own parenthesis, like this:

while ((a > 1) && (a < 5)){

In regards to the "for" loop, did you initialize "i" previously in the code? If not you should get an error, but usually it's an "uninitialized variable" error. I've never seen the compiler complain about "for loops" before (unless the syntax is wrong).

Post a Reply

Please log in to post a reply.

Did you know that a microcontroller can measure an RC time constant? Learn more...