Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Executing more than one command in C programming at the same time, newbie in C.

  1. Feb 23, 2012 #1
    Hello all,
    I'm doing a project related to counting up the amount of production.
    I'm using Arduino board and C language.
    I am counting the amount of production in 3 line in the factory.
    So far I have design the program only for 1 line, how to make the 2nd and 3rd lines so at the end I can sum up all the amount from these 3 lines.
    So from me, I guess I should know how to make program that is able to be execute once at a time....Do you have any idea since I am so newbie in C...

    Thank you Bro and Sis :D
  2. jcsd
  3. Feb 23, 2012 #2
    You're going to have to make your post a little more understandable.

    However, with what I did get, once you've got your three factory lines program, you can make a variable simply called total, or sum, or whatever, and set it like this:

    Code (Text):

    total = var1 + var2 + var3;
  4. Feb 23, 2012 #3
    Var1 , var2 and var3 is the amount of production at line 1,2,3
    Before we get the amount, there is calculation for each line.
    What I mean to ask, how the calculation is executed simultaneously without waiting?
    Because in fact what I learn in C, we should finish the calculation in line 1 first then executing for the 2nd calculation, etc.

    I hope you get what I meant.
  5. Feb 23, 2012 #4
    You shouldn't have to wait. Your compiler reads from top to bottom. It will calculate the totals in a split second.
  6. Feb 23, 2012 #5
    Hmm, for example: the calculation for line 1 has done and the amount is displayed in the 7segment, then calculation for line 2 is in progress, while it is in progress, there is suddenly certain amount of production detected in line 3.
    At this condition, we have some choice:
    1. continue to count on line 2 and wait till it is displayed (it means it will cost some seconds(delayed seconds), coz delayed second is needed to display smoothly)
    2. jump to count on line 3 and then back to line two, anyway I think it will be messy.

    So far only this possiblity I can guess since I am newbie in C.
    Could you give some experience idea :D?
  7. Feb 23, 2012 #6
    I'm having a hard time understanding what you're trying to do. Sorry I can't be of further help.
  8. Feb 23, 2012 #7


    User Avatar
    Science Advisor
    Gold Member

    I think I understand what you are asking:
    Your question is basically what happens if the calculation takes so long that something has been produced while the computer was calculating the result.
    The answer is that is is hardly ever a problem if you are monitoring a real process simply because the calculation only takes a a few tens of nanoseconds.

    When it IS a problem (and it can be if you are doing a more complicated calculation somewhere in your program that takes a long time) you have to look into using multiple processors and parallel programming; and this is not something you would do in Arduino (or even plan C, you need to add on some MPI or similar). There are whole books written about how to handle problems like this.
  9. Feb 23, 2012 #8
    To Chemicist, thank you anw..:D
    To f95toli, thank you!
    Anw in the real process I make, I intentionally add about max 1000miliseconds for the calculation in order to make the display better, then It means that it will make a problem in the other line of calculation.
    What possibly I can do with this programming in order to avoid the error in real process?

    can you suggest some reading about multiple processor/parallel programming?
  10. Feb 23, 2012 #9


    User Avatar
    Gold Member

    You don't need to program multiple or parrallel processors for real time processing, unless you are maybe programming for CERN.

    What you are asking about is polling or interrupts. That is done every time you turn on your computer. How do you think your computer knows you have pressed a key on your keyboard.
    Your computer has 2 choices
    - it either the polls at certain relatively equal intervals of time the keyboard and asks has a key been pressed anf if so sent the key to me so I can do something with the character,and go back to do other stuff, or if not I can keep on doing other stuff.
    - the computer receives an interrrpt from the keyboard which says hey, a key has been pressed, and the computer says OK, let me suspend for a moment what I am doing right now to get the character, and then I will go back to what I was doing before.

    It is easier to on programs written for older CPU's such as the Z80, 6052, 8086 or those written for DOS operating systems. And a lot of game programming uses polling or interrupts to detect movement of the mouse or keystrokes.

    Gt a C book and look at some code for handling hardware polling and interrupts,
  11. Feb 23, 2012 #10


    User Avatar
    Homework Helper

    It's possible to do multi-threaded code with a single cpu, but you need a multi-threaded operating system to do this.

    It's still not clear what your trying to accomplish with the program you're currently working on. You could calculate the values for all 3 lines, then do the delay. You could use a ticker (clock) count to do the delay so the timing would stay the same regardless if the code took a bit more or less time on each cycle (as long as the code doesn't take more than one cycle).
  12. Feb 23, 2012 #11


    User Avatar
    Gold Member

    On the Arduino site you have this for polling and interrupts.

    Depending upon how quickly your data arrives, the Arduino may not be up to the task in your situation and you may loose data from your "production". Or you have to make sure you do not update the display while acquiring the data.

    I fail to understand why you need as long a time as 1 sec to update the display.
  13. Feb 24, 2012 #12


    User Avatar
    Homework Helper

    Is it possible to use separate interrupt routines for each line? If so, then have each interrupt routine increment a separate counter. In the main code loop, you would need to temporarily disable interrupts, copy an interrupt updated count to a temp variable, then enable interrupts. Repeat this twice more to get all 3 counts, add up the three copies of the counts, then update the LCD display.
  14. Mar 4, 2012 #13
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook