Thread: need help with go to statment

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

    need help with go to statment

    so i know that "go to" are not the best for object oriented programing but this is going to solve all my issues, if i can ever get it working

    so first i need to say that i am programming this inside a micro based complier but the code is still c, i will explain all the stuff that may look bazzar.

    and yes I KNOW I KNOW I KNOW i need to factorize my code because right now it is just a ton of copy and past, and i have figured out a way to do that, but i just need a little more time, but i really need to get this to work

    so here is my issue, i am creating flash patterns for a project, yes flash patterns like you would see on a cop car or ambulance etc...i am controlling them using i2c and a digitial i/o expander which is not the problem the code works great(besides the fact i need to reduce the copy and past) so i am controlling the lights with a mechanical encoder which is programmed and working great

    my issue is that when i cange the encoder to a different position i want it to break out of the for loop and go to a different for loop, so the encoder is on an interrupt that sense when its changed positions.( the hardware for that is fine ) i just need to work on the software

    so i figured i would use the "go to' statement so when i change the encoder it could go to the next for loop, but i need it to do that and break from the first for loop to and execute the second on.

    so weird stuff that is applicable to my micro(the PSoC ) is
    i2c which not to insult anybody entelligence but a brief definition is just a way of having multiple devices on the same bus line doing different things, all i2c needs a group two address which is why i have a variable there, and to send data you need to issue the write command and and the other code to go with that, which is why it is at the bottom of the for loop

    so here is the code
    the encoder switch statement is long so i am only posting the first two cases, i know it needs a default case but no need to wast anybody time,
    Code:
    	switch(encoder1)
    		{
    			case 0x00:
    				LCD_PrCString("        ");
    				LCD_Position(1,0);
    				LCD_PrCString("Case0");
    				Police_Primary_LightBar( );
    			
    			break;
    				case 0x01:
    				LCD_PrCString("        ");
    				LCD_Position(1,0);
    				LCD_PrCString("Case1");
    				Police_LightBar_Secondary( );
                                   break;
    
    void Police_Primary_LightBar(void)
    {
    	int j;
    	police_primary:
    	G_Flag_Change_From_Read =0;
    	for(j=0; j<4000; j++)
    	{	 
    		if(G_Change_From_Read) goto police_secondary;
    		if(j<1000)
    		{
    			if(G_Flag1)
    			{
    				Side_Lights_LightBar( );
    				Test_Function4( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0xE0;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    				Side_Lights_LightBar( );
    				Test_Function1( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0xc0;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				Test_Function4();
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0x07;
    			
    		
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    			    Side_Lights_LightBar( );
    				Test_Function1( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x03;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount,     I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    		}
    	else if((j>1000) && (j<2000))
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x3C;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x81;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xC3;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x3c;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    	
    	}
    	else if((j>2000) && (j<3000))
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x55;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x16;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xAA;
    			
    			
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x61;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    	
    	}
    	else
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFF;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x00;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x00;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0xFF;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    		
    	}
    
    }
    
    
    	
    	
    } // end of police light bar primary
    void Police_LightBar_Secondary(void)
    {
    
    	int i;
    	police_secondary;
    	G_Flag_Change_From_Read=0;
    	for(i=0; i<4000; i++)
    	{
    		if(G_Flag_Change_From_Read) goto police_primary;
    		if(i< 2000){
    		if(G_Flag1)
    			{	
    				Side_Lights_LightBar( );
    				Test_Function( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0xE0;
    	
    				
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    				
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0xC0;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				Test_Function( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0x07;
    				
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x03;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    		
    			}
    			if(G_Flag2)
    			{
    				Side_Lights_LightBar( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x28;
    			
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x14;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    		}
    		
    
    			
    			else
    			{
    				if(G_Flag4)
    			{
    				Side_Lights_LightBar( );
    				Test_Function( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0xE0;
    	
    				
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    				
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0xC0;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				Test_Function( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0x07;
    				
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x03;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    		
    			}
    			if(G_Flag1)
    			{
    				Side_Lights_LightBar( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x28;
    			
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x14;
    	
    				I2C_bWriteBytes(I2Caddr, command, 2, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    
    		}
    	}
    
    			
    			
    }// end of police lightbar_Secondary
    
    void PSoC_GPIO_ISR_C(void)
    {
    	G_Flag_Change_From_Read=1;
    }
    void Counter16_1_ISR(void)
    {
    	G_Flag2 = !G_Flag2;
    	
    	num2=rand( );
    }
    void Counter16_2_ISR(void)
    {
    	G_Flag3= !G_Flag3;
    	num3=rand( );
    	grille_function1_Data_ADDR ^= grille_function1_MASK;
    }
    void Counter16_3_ISR(void)
    {
    	G_Flag4= !G_Flag4;
    	//G_Flag4=0;
    	num4=rand( );
    }
    the main loop is a void main(void){ }
    and just has the switch statement in it

    once again when the encoder switches i need it to break from the first for loop and go to the second for loop, in theory this should work but i am not sure if it will

    please help

    thank you

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by begginer View Post
    so i know that "go to" are not the best for object oriented programing but this is going to solve all my issues, if i can ever get it working

    so first i need to say that i am programming this inside a micro based complier but the code is still c, i will explain all the stuff that may look bazzar.
    First of all C is not object oriented programming... it is procedural or functional programming.

    and yes I KNOW I KNOW I KNOW i need to factorize my code because right now it is just a ton of copy and past, and i have figured out a way to do that, but i just need a little more time, but i really need to get this to work
    Well, then, you should clean that up before you start making even more programming mistakes to hide your current ones.


    my issue is that when i cange the encoder to a different position i want it to break out of the for loop and go to a different for loop, so the encoder is on an interrupt that sense when its changed positions.( the hardware for that is fine ) i just need to work on the software

    so i figured i would use the "go to' statement so when i change the encoder it could go to the next for loop, but i need it to do that and break from the first for loop to and execute the second on.
    Tell you what... before I used goto for anything I would rethink my entire code. goto is not an all out evil but it is definately a last resort.

    For what you're doing a far better plan would be to load sequences into an array as data.
    Code:
    int sequences[50][10];  // 50 sequences of 10 patterns each.
    int sequence = 0;          // sequence selector
    int On = 1;                    //  on / off switch
    
    while (On)
      {  for (x = 0; x < 10; x++)
          SendToLightBar(sequences[ sequence ][ x ];  }
    Cycle through the array's values to create flash patterns. To switch from one set of patterns to another simply switch to another bank in the array. One loop, one array, a simple set of on/off commands... as many patterns as you can stuff into memory.

    Your problem is not that your code is messy... your problem is that your code is ill conceived.

    Look closely... I just reduced all that code of yours to 3 lines.
    Last edited by CommonTater; 03-31-2011 at 11:15 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's not the best for non-object oriented programming (like C) either. As a matter of fact, it's generally a bad idea at all times. And it wont solve your problems, only make your code worse and more error prone. So you have your light routines in two separate functions, which is good. But unfortunately, labels, which are the markers you "goto", are scoped at the function level. That is, inside Police_Primary_LightBar, the police_secondary label isn't visible, so you can't go to it.

    If you want to get out of a loop early however, you can just use break, and you will go right to the end of the loop. If you need to bail all the way out of the function and skip any post-loop code, you can use return:
    Code:
    for (i = 0; i < 1000; i++) {
        if (leave_function_now) {
            return;  // leave function immediately
        }
        if (exit_loop_early) {
            break;  // exit the loop immediately
        }
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can do OO style programming in C, it's just that the syntax is a little different. Rather than having an implicit this pointer, you have to pass it in to everything, and you can't quite encapsulate the methods as closely with the class as in C++. It's still OO style if you get it right.
    Having said that, this is nowhere near being OO.

    You can't use goto to jump between functions.

    You don't add gotos to code, you remove them.

    main returns int, not void - ALWAYS.

    The need for refactoring out comon coded here is too great to do anything else before it IMHO.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    main returns int, not void - ALWAYS.
    Actually some microcontrollers allow void main (void) because the program simply never ends and if it did, there would be no place to return a value to...

    In every other case you are correct.
    Last edited by CommonTater; 03-31-2011 at 12:47 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Actually some microcontrollers allow void main (void) because the program simply never ends and if it did, there would be no place to return a value to...
    Yeah, but then a microcontroller would provide a freestanding environment, whereas iMalc's "ALWAYS" should have been qualified with "for hosted environments".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Yeah, but then a microcontroller would provide a freestanding environment, whereas iMalc's "ALWAYS" should have been qualified with "for hosted environments".
    Oh come on now, Lase... play fair...

    If I'd posted an unqualified statement like that you'd be telling me about microcontrollers.

    FWIW... I figured maybe iMalc had never run into that before.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Oh come on now, Lase... play fair...

    If I'd posted an unqualified statement like that you'd be telling me about microcontrollers.
    Hey, I'm just supporting your statement
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Hey, I'm just supporting your statement
    LOL... and here I figured you were just being super-picky like usual.

    My Bad.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    so i think i may have found a better way to break out of the for loop, i use a gpio interrupt which basically just reads the port, and if it senses a change in state, it interrupts and sets my flag condition to 1, so in my loop if that flag is one i break and then update the flag variable back to zero, i did it in ever iteration of the for loop so no matter when the encoder is changed it breaks out of the for loop
    what i think i am missing is the break condition, i want it to break when the flag condition is set to one

    i know my code is probalbly not the best but if someone could show me with my code i would greatly appreciate it

    i call the function the same way with the switch statment in main
    void Police_Primary_LightBar(void)
    {
    int j;
    G_Flag_Change_From_Read=1;



    for(j=0; j<4000; j++)
    {

    if(j<1000)
    {


    if((G_Flag1) && (!G_Flag_Change_From_Read))
    {
    Side_Lights_LightBar( );
    Test_Function4( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0xE0;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    Side_Lights_LightBar( );
    Test_Function1( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0xc0;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    }


    else
    {
    Side_Lights_LightBar( );
    Test_Function4();
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0x07;


    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    Side_Lights_LightBar( );
    Test_Function1( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x03;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );


    }
    }
    else if((j>1000) && (j<2000)&&(!G_Flag_Change_From_Read))
    {
    if(G_Flag1)
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0x3C;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x81;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );


    }
    else
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0xC3;


    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x3c;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    }


    }
    else if((j>2000) && (j<3000) && (!G_Flag_Change_From_Read))
    {
    if(G_Flag1)
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0x55;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x16;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );


    //Break_For_Loop( );
    }
    else
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0xAA;


    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x61;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    //Break_For_Loop( );
    }


    }
    else
    {
    if(G_Flag1)
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0xFF;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0x00;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );


    //Break_For_Loop( );
    }
    else
    {
    Side_Lights_LightBar( );
    I2Caddr=0x20;
    charcount=2;
    command[0]=0x00;
    command[1]=0x00;


    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    I2Caddr=0x20;
    charcount=2;
    command[0]=0x01;
    command[1]=0xFF;

    I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    I2C_ClrWrStatus( );

    }


    }
    G_Flag_Change_From_Read=0;



    }




    } // end of police light bar primary

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    for (i = 0; i < 1000; i++) {
    if (leave_function_now) {
    return; // leave function immediately
    }
    if (exit_loop_early) {
    break; // exit the loop immediately
    }
    }
    what are the conditions for leave_function_now, and exit_loop_early
    are they pre defined in c somewhere or they going to be my own functions i have to make

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by begginer View Post
    what are the conditions for leave_function_now, and exit_loop_early
    are they pre defined in c somewhere or they going to be my own functions i have to make
    No, they're your own functions or logical expressions that dictate when you want to break out of the loop. One example could be:
    Code:
    if((G_Flag1) && (!G_Flag_Change_From_Read)) {
        break;  // leave the loop early when G_Flag1 is true and G_Flag_Change_From_Read is false
    }

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    would the flag condition ever need to be reset to zero?

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What flag condition? I have no idea what you're talking about. You need to be much more specific. I have no idea how this is really supposed to work. I don't know what the actual pattern is supposed to be and what would be correct and incorrect operation, plus, I can't test this since I don't have your embedded board, your compiler or any of your hardware to wire it up. Try it different ways and see what works.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    the g_Flag_Change from read, i have it defined as BOOL G_Flag_Change_From Read=0; before main to make it global ofcourse, and then inside i gpio interrupt i have it set to one, so when teh encoder changes, it sets the flag to 1,

    so inside the for loop i could do
    if((G_Flag1) && (!G_Flag_Change_From_Read)) {
    break; // leave the loop early when G_Flag1 is true and G_Flag_Change_From_Read is false
    }
    but i think that they flag G_Change_From_Read would have to be updated or set back to zero, otherwise it would alwasy be one, rigth i can send u more code if this doesn't make sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statment qustion
    By Yizi in forum C Programming
    Replies: 5
    Last Post: 11-21-2009, 04:53 AM
  2. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  3. C++ If statment question
    By TehClutchKiller in forum C++ Programming
    Replies: 17
    Last Post: 05-28-2008, 07:33 AM
  4. If statment +text
    By Zewu in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2002, 04:31 PM