Multiple timed loops

Been looking around. I have an rtc keeping time and use a timer library to do things according to the set time. I have a timer triggering void temps() every 5 seconds and reading 6 thermistors. What would happen with the arduino scan cycle if I have 2 loops triggerd at once? Call void temps() and void pressure() every 5 seconds.

I know I can add them in the same loop but would like to know what would happen. Will it run the temps loop and then move to pressure loop or will it skip the pressure loop or give incorrect readings?

It depends on how exactly you call temps() and pressure().

In the simplest case the first triggered function is called first, then the other function. If one function should take precedence over the other one, you can implement that as well, by according trigger checks and calling sequence.

Also depends on what kind of sensors you are using. You might be able to tell all the temps to read temperature (Dallas parts?) at the same time and maybe the same for the pressure sensors. Doesn't matter when you actually read the information, only when that information was recorded.

Spread your reads out over the null time in between "record now" command. Also might be able to stagger temp and pressure by a second or two. Are the two reading that critical?

The demo Several Things at a Time may be of interest.

Welsyntoffie: What would happen with the arduino scan cycle if I have 2 loops triggerd at once? Call void temps() and void pressure() every 5 seconds.

The Arduino could probably call them 50 or 100 times every second.

...R

The readings are not critical. I was just wondering about the effect it might have. To call two functions at the same time using one counter. Was thinking it might miss the second function due to the time being elapsed before the first function is complete.

Of course the second function can be missed, if you write such code. Do you already have an idea how you'll do it?

Welsyntoffie: To call two functions at the same time using one counter.

It is not possible to call 2 functions at the same time. They must be called one after the other.

If you design the functions properly they can give the appearance of running in parallel by doing a little of the task in one function followed by a little of the task in the other function and repeating until the tasks are complete.

...R

I am reading 6 thermistors every 5 seconds. All the temps calculate correctly and as expected. Now I want to read a pressure sensor from a slave arduino using I2C. I convert my readings to 8bit in the slave. On the master when I add my code to the same function that calculates the temps so that I get pressure every 5 seconds, the temp readings start to go crazy. I use the smooth method for the temps (array size is 10).

Welsyntoffie:
Now I want to read a pressure sensor from a slave arduino using I2C.

Do you really need a slave Arduino for that?

On the master when I add my code to the same function

You may be able to see that code but we can’t - so how can we give advice? Post your program and explain clearly what it actually does and what you want it to do.

And please use the code button </>so your code looks like thisand is easy to copy to a text editor

…R

Robin2: Do you really need a slave Arduino for that

Yes. My thermistors is working with 5v. I am also reading a 4-20mA pressure sensor using the internal aref. To avoid jumping between internal aref en default aref I use a second arduino to read the pressure sensor.

Regarding the code. Well, it is a big project already and I have alot of my code in tabs to easily debug. I could zip it and attach it but that would give you half a days reading.

Any way. I have a timer repeat every 5 seconds. OnComplete it calls the temp function and then request pressure from slave. It does not seem to have any negative effect. I also moved my thermistor calculation closer to the start of the loop. Also seems to clear up the random crazy calculation.

Welsyntoffie:
Any way. I have a timer repeat every 5 seconds. OnComplete it calls the temp function and then request pressure from slave.

I still don’t see how your timer calls a function. Is it an ISR, or polling inside loop()? Can you post just that piece of code?

Welsyntoffie: To avoid jumping between internal aref en default aref I use a second arduino to read the pressure sensor.

Why would you want to avoid changing the reference? The Arduino won't care. And you are not trying to take readings at short intervals.

Can you write a short program to illustrate your problem and post that?

...R

I have been testing this. All still seems to work fine. My arduino is not mis behaving or missing any data when I call two funtions (sub routines) at the same time.