Thread: Extended While Loop

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    3

    Extended While Loop

    Hi all,

    I have the following code:

    Code:
    while(intIsLow != 1) {
    			SetBit(SET_GLOW);
    			intIsLow = ReadBit(IS_GLOW);
    			}
    The intIsLow variable is set to read a hall sensor which when high means the correct gear is selected. The while loop above simply activates a relay which actuates low gear change in a 2 speed gearbox. When the hall sensor becomes high then stop.

    What I would like to do is extend the While loop with a timer. So after intIsLow == 1 the code in the while is executed for a further 1 second.

    Can anyone advise the easiest way to do this?

    Many thanks

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    Is this program intended for an embedded device? Depending on that (standard library coverage, OS), I suspect a timer might have to be realized differently.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    Hi Adrian, thanks for the reply.

    The program is run on a PLC with 8 inputs & 8 digital outputs. The PLC has its own C compiler and all standard libraries are supported.

    What would be the best way to extend the operation of the while loop by 1 second after intIsLow == 1?

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    I'm a bit confused. Does SetBit(SET_GLOW) have to be called constantly to maintain the desired activity? Your loop example suggest so. In that case I'm not sure how best to approach it, but someone else here might.

    On more complex systems with scheduling, one would avoid such a busy loop.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    SetBit(SET_GLOW) is called for as long as necessary for the actuator to move into low gear until intIsLow is high. It doesn't have to be called constantly, it could be called once without a loop. Then disabled when intIsLow is high. I just thought it'd be easier to throw it into a loop. I don't think calling it every time the loops run causes harm for such a basic task. So far it works well.

    I just need to extend the while or add a delay in which holds the loop open for 1 second.
    Last edited by Jimbo1986; 1 Week Ago at 06:53 AM.

  6. #6
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    Quote Originally Posted by Jimbo1986 View Post
    It doesn't have to be called constantly, it could be called once without a loop.
    I see. Probably better not to hammer the memory in that case.

    Maybe something like this would work for you:
    Code:
    #define _POSIX_C_SOURCE 200809L
    
    #include <time.h>
    
    // ...
    
    const struct timespec query_delay = {0, (1000 * 1000) * 10}; // 10ms
    const struct timespec extension_period = {1, 0}; // 1s
    
    SetBit(IS_GLOW);
    
    while (intIsLow != 1) {
        intIsLow = ReadBits(IS_GLOW);
        nanosleep(&query_delay, NULL); // adjust or remove, according to your needs
    }
    
    nanosleep(&extension_period, NULL);
    UnsetBit(IS_GLOW); // You'll presumably have something like this then

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    Btw, if you don't work with intIsLow outside of the loop, I would just get rid of the variable entirely:
    Code:
    while(ReadBits(IS_GLOW) != 1) {
       // ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using extended ascii
    By Moon River in forum C Programming
    Replies: 4
    Last Post: 09-17-2014, 12:43 AM
  2. ASCII extended
    By arlenagha in forum Linux Programming
    Replies: 1
    Last Post: 05-14-2003, 01:15 PM
  3. extended find
    By stormbringer in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-26-2002, 09:18 AM
  4. Extended Memory
    By Blizard in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-03-2002, 10:35 AM
  5. Combo Box Extended
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 09:04 AM

Tags for this Thread