Thread: C keep a loop going whilst program continues on

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    147

    C keep a loop going whilst program continues on

    hi im making a c program for the 68hc11 and to out put flashing ect ect on 6 lights on a O/I board i need to use a while loop to keep my lights flashing but i need program to continue on

    heres my code
    Code:
    #include <stdio.h> 	/*standard i/o functions*/
    #include <stdlib.h>
    #include <math.h>
    #include <iof1.h>
    
    
    void main()
    {
        int choice,actizone;
    
    	do{
    		choice = menu(); /* Calls the menu and returns the users choice */
    
            switch(choice)
            {
            case 1: /* Disarm the alarm */
                {
                    arm();
                    break;
                }
            case 2: /* Arm the alarm */
                {
                    
                    break;
                }
            case 3: /* Exit */
                {
                    exit(1);
                    break;
                }
            }
    
    
    	  }while(1);
    }
    
    
    menu()
    {
        int choice, i;
       	printf("\n ===========================================");
        	printf("\n ----=====| Burgalar Alarm System |=====----");
    	printf("\n ================++++++++++++===============");
    	printf("\n 1. Arm Alarm");
    	printf("\n 2. Disarm Alarm");
    	printf("\n 3. Exit");
    	printf("\n -------------------------------------------");
    	printf("\n\n Please input your choice from the menu :> ");
    	scanf("%d", &choice);
        printf("\n");
    
    	 for(i=0;i<1;i++)
         {
    	      if(choice < 1 | choice > 4) // if the menu choice is invalid the program will ask the user to re input it.
    	       {
               printf("\nInvalid choice please re enter :> ");
    	       scanf("%d", &choice);
               i--;
          	   }
         }
    
    
    return choice;
    }
    
    
    
    
    int arm(int argc, char *argv)
    {
       unsigned char mask, port;
       unsigned int run,i;
       unsigned int counters[6] = {0};
    
       PORTG = 63;
       DDRG = 0xff;	//set port to output
    
       run = 1;
    
    
       while (run)
       {
    		mask = 1;	//reset mask to one before each iteration
    		for (i = 0; i < 6; i++)
    		{
    			if ((counters[i] == 500) && (port & mask))
    			{
    				PORTG ^= mask;
    				counters[i] = 0;
    			}
    			counters[i]++;
    			mask <<=1;	//check next bit
    		
    		}
       }
    
    
    
       printf("\n");
       return 0;
    }
    the arm function makes the lights blink but i need them to stay blinking and not to keep my program in a infinate loop sooo i can select other things from the menu ect ect . Any ideas how i can do this
    thankyou

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    if available: fork()
    otherwise pthreads

    The easiest way would be to write separate programs.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you either have to:
    1. Use interrupts.
    2. Check for keypress without WAITING for the keypress.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    if available: fork()
    otherwise pthreads

    The easiest way would be to write separate programs.
    This is a microcontroller, it probably has 32KB of RAM. It is POSSIBLE to make threads in such a system, but I think it is beyond the original posters capabilities at this point in time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i am ment to make a simple burgalar alarm disarmd : all 6 zones (6lights 6 switches) off, armed : all the zones or 1 of them will have the lights flashing at 0.5 seconds constant, and then when a switch is flicked the alarm is triggerd soo the zone will flash at 0.25 seconds.

    is there a way i can make the zone flash without using a infinite loop? so the lights are flashing and my program is not on a halt

    i dont think were ment to use fork

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no idea what you are supposed to use, but there are, essentialy three ways of doing two things "at once" in a computer system:
    1. Interrupts that happen at given intervals (periodic timer interrupts).
    2. "Do not block on anything and keep the code running in a loop"
    3. Use multiple threads/processes (e.g. fork, pthreads or whatever)

    I find #3 unlikely in a 68HC11 system, so it's highly likely that you should use one of the other solutions.

    You may want to confer with your tutor or some such to see what the right solution is in this case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You might be able to 'simulate' an interrupt timer. What I mean is make a global variable oldTime, which is your start time when the alarm first "goes off".

    Now, as your programs runs, it periodically has a line of code that tests the newTime, and when the newTime - oldTime > some time interval, then it calls a function which will blink the lights one time, etc., and return where it left off.

    A large function or loop, may need a couple of these lines of newTime - oldTime checks in them.

    It's important to use these time checks carefully. Too many of them, and your other code will run very slowly, indeed. Not enough, and the lights will blink at too great of an interval.

    Ideally, your board should have a "latch" state on it, so once the alarm state is entered, it needs no further "babysitting" by your software, but will stay "on", light a light bulb, once the switch is turned on.

    If you can inline your function for the blinking light (assuming you find no "stay on" state for your board), it will work better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM