Pages: [1] 2   Go Down
Author Topic: How to Run Multiple Functions Independently  (Read 1798 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Greetings,
I could use some guidance on how to run separate functions simultaneously and independently. I am reading input pulses and using that as a clock for my counter variables. I am not using millis() or delay() nor do I wish to use those functions. I am counting input pulses for my delay periods.

I have the separate functions working and giving proper time delays when run separately but when two run simultaneously the delay time is doubled. If three are run simultaneously the time delay triples. I understand why the time delays increase because of the shared resources.

Can you please point me in some direction as to how I can get multiple functions to run simultaneously and independently so that the execution of one function does not interfere with the execution of other functions?

Here is a block of pseudocode. Many Thanks for any help you can offer!

Code:
void loop() {
function1();
function2();
function3();
}

function1() {
if (Count1 not complete) {
LED1 ON;
increment count1;
}
else {
LED1 OFF;
Reset Count1;
}
}

function2() {
if (Count2 not complete) {
LED2 ON;
increment Count2;
}
else {
LED2 OFF;
Reset Count2;
}
}

function3() {
if (Count3 not complete) {
LED3 ON;
increment Count2;
}
else {
LED3 OFF;
Reset Count3;
}
}
« Last Edit: March 04, 2012, 08:09:02 pm by hypercode » Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1670
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how I can get multiple functions to run simultaneously and independently
You can't. The Arduino only has one CPU so it can only do one thing at a time. The only way to get true simultaneous functions is to run each one on a separate Arduino.

Pete
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well without the timing specs of the three input signals it's hard to say if or if not. What is the maximum pulse frequency of the external pulses being wired to the arduino?

 But basically it's usually possible to perform 'pseudo' independent processes by using either interrupts or millis() or micros() or a combination of both. Independence comes down to can the individual functions be serviced quick enough by the main loop code to satisfy the timing requirements of the external events. In almost all cases the answer is yes, if you avoid blocking commands as you now have embedded into each of your functions.

Just saying:

Quote
I am not using millis() or delay() nor do I wish to use those functions. I am counting input pulses for my delay periods.

May very well be limiting your best or only possible solution. It's the results that counts not the methods used or not used.

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not dead set against using millis() if there is a way to get accurate timing. Is there any way to control millis() with an external pulse input on the Arduino's Pin 8?

I can get accurate time delays from the input pulses on Pin 8. When I use millis() or delay(), it's not accurate. What approach would I take to control the Atmeg328's internal counters/timers so that millis()-based time delays will be accurate?
« Last Edit: March 04, 2012, 10:01:09 pm by hypercode » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not dead set against using millis() if there is a way to get accurate timing. Is there any way to control millis() with an external pulse input on the Arduino's Pin 8?

I can get accurate time delays from the input pulses on Pin 8. When I use millis() or delay(), it's not accurate. What approach would I take to control the Atmeg328's internal counters/timers so that millis()-based time delays will be accurate?

I'm afraid you haven't explained what your are actually trying to accomplish. What is your numerical accuracy requirements specifically? Again what are the timing specification of the external signals you are trying to measure?

Lefty
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I can get accurate time delays from the input pulses on Pin 8. When I use millis() or delay(), it's not accurate. What approach would I take to control the Atmeg328's internal counters/timers so that millis()-based time delays will be accurate?

Can you explain all this? In what way are they not accurate? What amount are they out? What amount is acceptable?

Whether or not you use the internal clock or hook up to an atomic clock on pin 8, there is still some latency introduced by the time taken to execute your code.

Perhaps if you describe what you are trying to do here? Your subject line of "How to Run Multiple Functions Independently" implies that, if you were successful, your problem would be solved. It might be more helpful to describe the problem.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You might find this helpful:

http://www.gammon.com.au/forum/?id=11504

In that I use the internal timers to accurately measure a frequency. The error amount was about 0.04%. Not too bad. Now if you want a lower error rate perhaps specify what that rate is. And what you are currently getting.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any way to control millis() with an external pulse input on the Arduino's Pin 8?
What does that mean?

Have you considered the use of "micros()"?

As others have said; until you tell us what you want to do, not how you think you should (or shouldn't) do it, this is just a guessing game.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With other processors, you can allocate RAM then run a routine in that area of RAM independently of what is happening elsewhere. Can you do that with the Arduino C-style software?

This what I'm trying trying to find out. It's not that I want to do any specific thing where I would write the code to do that specific thing. I'm trying to learn how to use the Arduino software with the Atmeg328.

Thanks for your patience ;^)
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2224
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Even if you can run code in arbitrary locations, the arduino only has one core and therefore can only execute one instruction at a time.

http://arduino.cc/forum/index.php/topic,74503.0.html
http://arduino.cc/forum/index.php/topic,87552.0.html
« Last Edit: March 05, 2012, 09:37:22 am by pYro_65 » Logged


Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Basically no, but the ATMega chip has some neat features that make it pretty good at measuring inputs and generating accurate output pulses and waveforms. The Arduino is a micro controller, not a computer and so a lot can be achieved using the hardware level rather than in software as you would on a computer - the timer/counters being a strong example of this.

As others have said, if you give a bit more detail on what you are doing you will get better advice.

Or If your up to it, read the ATMega328 data sheet for a complete view of what's available.

Duane B

rcarduino.blogspot.com
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
With other processors, you can allocate RAM then run a routine in that area of RAM independently of what is happening elsewhere. Can you do that with the Arduino C-style software?
The AVR cannot execute code from RAM - it is a feature of the Harvard architecture - program and data memories are totally different address spaces.
This isn't an Arduino limitation, it is a limitation of the processor family.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Even if it wasn't, without two processors you never really run things "simultaneously". And even then, if you have one lot of RAM the two processors cannot both write to the same address at once (usefully anyway) so you need to carefully manage your use of resources.

With other processors, you can allocate RAM then run a routine in that area of RAM independently of what is happening elsewhere. Can you do that with the Arduino C-style software?

What other processors? Can you quote one? Generally speaking processors (for safety purposes) delineate memory used for programs (which they don't let you write to) and memory used for data (which you can write to).
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PIC16. You designate areas of memory addresses with a srart addy and a stop addy. Known as "stacks".

pyro65... Thanks for the links. Interesting discussion ;^)

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
With other processors, you can allocate RAM then run a routine in that area of RAM independently of what is happening elsewhere. Can you do that with the Arduino C-style software?

As already said, a micro-controller is a single thread processor, one hardware instruction at a time is executed. But with proper software structure an arduino (any c/c++ program actually) can indeed perform multiple tasks that are independent of each other up to the timing limits for the specific task requirements. This need for 'Independence' is just a limitation in you thinking until you gain the knowledge of program control and structure that one has at their means.

Lefty
Logged

Pages: [1] 2   Go Up
Jump to: