I need help getting code to do something every 10 minutes

This is a discussion on I need help getting code to do something every 10 minutes within the C Programming forums, part of the General Programming Boards category; I'm doing a project that requires me to programme a PIC. The PIC gets the date and current time from ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Question I need help getting code to do something every 10 minutes

    I'm doing a project that requires me to programme a PIC. The PIC gets the date and current time from a GPS reciever, this part is complete my problem is now getting the PIC to control a relay. The device is a solar tracker and the relay depending on the date need to go on at a certain time for roughly 20 seconds every ten minutes otherwise the relay should be in an off setting. I have spoken to people and they say a switch statement is the best policy?

    The code should roughly do this

    dd/mm/yyyy
    when dd = 04
    check the value of mm

    if

    mm = 5

    check hh:mm:ss

    if

    mm = 11:15

    set relay to on seting for 20 seconds then to off setting

    if mm = 11:25

    set relay to on seting for 20 seconds then to off setting

    etc, etc every ten minutes

    the device will stop when it comes into contact with a limit switch.

    I really don't know where to start any help or ideas would be great!

    Thanks

    Philip

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If the process does not have to do anything else while waiting, you should use sleep().

    Unless you put a delay in, using a loop to constantly check the time is a Very Bad Idea as this will cause the process to hog as much of your CPU as it can for the entire ten minutes, when it should be waiting quietly instead.
    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
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    thanks for the reply

    My main problem is how I will do this for each month and then at the certain time of the day. I understand how to control the relay but is there a better way to do it every 10 minutes, is that what you mean by the "delay"?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pipsiphone View Post
    My main problem is how I will do this for each month and then at the certain time of the day. I understand how to control the relay but is there a better way to do it every 10 minutes, is that what you mean by the "delay"?
    I just realized I am probably shooting my mouth off since a PIC is essentially an "embedded" device which I am not familiar with.

    So it could be that you will not have the luxury of the sleep() function, and may have to work something out with the kernel you are using, which kernels keep time using processor "ticks", ie, write your own delay function.

    In any case, the same logic applies. If you have a loop which just does something like this (pseudo-code):
    Code:
    while (1) {
          myinterval = 10 minutes;
          time = gettime;
          if (time==(lasttime + myinterval)) {
                          do stuff; 
                          lasttime = now;
          }
    }
    Your PIC's lil' CPU will be runnning maxed out the whole time, because it will constantly be asking for the time (eg, 10000 times/second) until ten minutes is up. That may not matter, except it is a waste of electrical power and could severely reduce the lifetime of the chip. You need to get your process to "sleep" (which is what the sleep function literally does). That requires some kind of communication with the kernel, because it will be the kernel that pauses and restarts the process -- there is no way to do that with user-space code directly, which is why there are functions like sleep() and nanosleep(). I could be wrong.

    But the first thing you want to find out is if you can use the sleep() function in your program, because embedded devices have a restricted instruction set which may make it impossible to implement all the "standard" functions (and that's all I know about embedded programming).

    Anyway, I would guess PIC programming is widespread enough, and this task basic and essential enough, that there must be an easy solution available somewhere. Now you know what you are looking for (a function to set a sleep delay), you can start googling or whatever.

    There are regulars here who do a lot of embedded stuff, me thinks, and one will surely be around today or tomorrow; if you retitle your thread (or start a new one) called "sleep function for PIC programming" there's a better chance of catching their attention.
    Last edited by MK27; 04-19-2009 at 03:21 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help optimizing loops....
    By ninjacookies in forum C Programming
    Replies: 4
    Last Post: 02-24-2005, 02:54 PM
  2. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 12:17 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

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