nested four loops

This is a discussion on nested four loops within the C Programming forums, part of the General Programming Boards category; ok so what i am trying to do is have an led flash on and off at one speed and ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    nested four loops

    ok so what i am trying to do is have an led flash on and off at one speed and then flash at at another speed for a set period of time...i am pretty sure that i need two for loops to do this but i am not sure how this works

    i would like to have the first for loop just flash the led four times and then have the second for loop flash the led 8 times

    the leds flashing time is determined by an interrupt and the is working correctly...i just need help getting the for loops to work

    Code:
    int i=0, j=0
    
    for(i=0; i<4; i++){
       if(G_Flag){
          led on;
         }
        else{
         led off;
         }
     i++;
    }if(i=4){
    for(i=4;i<8<i++) {
        if(G_Flag)
        led on;
        else
        led off
       i++
    }
    }
    i=0
    }
    interrupt_ISR
    {
       G_Flag = !G_Flag
    }
    the way i am controlling the leds is not an issue i have that underwraps but i am not sure how to just have the led flash on four four times and the have it flash on again at a different speed for eight times

    i thought that this code would work because once i is equal to four it would go into the second for loop but it doesn't work and i don't understand why
    Last edited by begginer; 02-25-2011 at 09:50 PM.

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You would have to call interrupt_ISR once in each for loop to negate G_Flag. Also, in the second for loop, you typed "if" instead of "for".
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    for(i=0;i<12;i++) {  //12=number of total flashes I want
       if(i<4)  //4=number of long flashes I want
          longFlash
       else
          shortFlash
    }
    Both 4 and 12 in the above could be defined macros:
    #define LONG_NUM 4
    #define FLASHES 12

    or just variables.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    the only problem i have is that i am using a mciro controller and not programming in visual studies so i don't know if i can use the long number of any of thoes macros that you could typically use in c, but may i am not sure

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In that case, "magic" numbers are OK.

    You may need to use two different loops. No if(i == 4) is needed, since i will always equal 4 at the end of the first loop.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

    the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

    the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by begginer View Post
    that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

    the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?
    The first loop could count up to four, and then it would kick over into the second for (you almost got me to write "four" loop.

    But you don't need the test for i == 4 in there. After the first loop, I will always be four. No test needed.

    This is an error, since = is for assignment anyway. Inside an if statement, you need == (and anywhere else you test for equality.

    Code:
    if(i=4){  //wrong. Should be deleted, anyway. 
    
    if(i==4) { //correct syntax for a test of equality. Not needed before the second for loop in your code.
    Last edited by Adak; 02-25-2011 at 10:56 PM.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    lol, i guess that is to many "for" and "four" in one issue but i got it to work...thanks for all the help

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "to many"

    You are KILLING me here!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    "to many"

    You are KILLING me here!
    C'mon Adak... fore loops are fun!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's "to" mulch for me "too" bare!

    Hello!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    It's "to" mulch for me "too" bare!

    Hello!
    I bearly know how to answer such a clipping comment...

    (Stop me when we get to light bulb puns... Illuminating though they may be I tend to fade rather quickly on the current path of it all.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-15-2010, 01:35 AM
  2. How to exit from nested loops?
    By frktons in forum C Programming
    Replies: 40
    Last Post: 07-19-2010, 03:57 PM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 02:47 PM
  4. Output from nested loops
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 07:09 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21