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

In summary, the author is doing a project related to counting up the amount of production. He is using Arduino board and C language. He has designed a program only for 1 line, how to make the 2nd and 3rd lines so at the end he can sum up all the amount from these 3 lines. He hopes someone can help him understand what he is doing.
  • #1
creativen
22
0
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
 
Technology news on Phys.org
  • #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:
total = var1 + var2 + var3;
 
  • #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.
 
  • #4
You shouldn't have to wait. Your compiler reads from top to bottom. It will calculate the totals in a split second.
 
  • #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?
 
  • #6
I'm having a hard time understanding what you're trying to do. Sorry I can't be of further help.
 
  • #7
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.
 
  • #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?
 
  • #9
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,
 
  • #10
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).
 
  • #11
On the Arduino site you have this for polling and interrupts.
http://arduino.cc/forum/index.php/topic,73024.0.html

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.
 
  • #12
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.
 
  • #13
I have got the idea!
Thank you!
But now I found errors about the constructor and destructor etc in my C programming, I am posting the new thread to help me in https://www.physicsforums.com/showthread.php?t=583892
 

1. How can I execute more than one command at the same time in C programming?

In C programming, you can execute more than one command at the same time by using the concept of multithreading. This allows for multiple tasks to be run concurrently, improving performance and efficiency.

2. Can you provide an example of executing multiple commands at the same time in C?

Sure, here's an example using the pthread library in C:

pthread_t thread1, thread2;
void *function1();
void *function2();
pthread_create(&thread1, NULL, function1, NULL);
pthread_create(&thread2, NULL, function2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// function1 and function2 will now be executed concurrently

3. What is the difference between executing commands sequentially and executing them concurrently?

Executing commands sequentially means that each command is executed one after the other, in order. This is the default behavior in C programming. Executing commands concurrently allows for multiple commands to be executed at the same time, improving performance and efficiency.

4. Are there any drawbacks to executing commands concurrently in C programming?

Yes, there are a few potential drawbacks to consider. Multithreading can lead to race conditions, where multiple threads try to access and modify the same data at the same time. This can cause unexpected behavior and errors in the program. Additionally, managing multiple threads can be complex and may require additional resources.

5. Are there any best practices for executing multiple commands at the same time in C programming?

Yes, there are a few best practices to follow when using multithreading in C programming. Some tips include: avoiding global variables, using synchronization methods like mutexes to prevent race conditions, and carefully managing resources to avoid memory leaks. It's also important to thoroughly test and debug your code to ensure proper functionality.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
8
Views
863
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
960
  • Programming and Computer Science
Replies
1
Views
718
  • Programming and Computer Science
Replies
12
Views
9K
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
58
Views
3K
  • Programming and Computer Science
3
Replies
86
Views
10K
Back
Top