Best way to use 2 loop() speeds

Hello,

I’m working on a complex project. It have many modules. It’s main mission is to get data from many sensors and store data to SD and show at display.

The question here appear because I need to get data from different sensors with different speed (frequency), but the standard structure of Arduino code is: declare, setup, one global loop; when I need something like: declare, setup, run specify code on specify CPU cycle. How to make it right at Arduino?

Loop 1 fast at 10kHz speed - read and analyze data from sensor X;
Loop 2 slow once per second - read data from sensor Y, show both X+Y results at display, write both X+Y results to SD.

I can use a milliseconds counter inside main Arduino global loop which will run speedy loop asap but run second slow loop only once per second, but its a little weird.

Does this problem have a better (more native for Arduino) solution?

Have you did a look around to find the Doing multiple things at the same time. Oh here easy to find Demonstration code for several things at the same time

brov:
I can use a milliseconds counter inside main Arduino global loop which will run speedy loop asap but run second slow loop only once per second, but its a little weird.

Not weird. Very common. I see it alla time. One mechanism for deciding it’s high time to do its thing every N milliseconds, another identical except for a different N.

Sometimes loop() will also have something done every time, obvsly at some unknown and perhaps variable frequency depending on all of what else is going on. edit: I like to wiggle an output pin. High before task dispatching timing stuff, low after, low until we are doing anything again. Measuring frequency and duty cycle at that pin can give you a look at how much time the actual computing takss are consuming.

There are libraries that dress this loop speed thing up; I prefer to roll my own in these simple cases, it’s usually less code and is something I can see all of right in front of me. I suggest you write it out this weird way, then decide whether you like a library to do the same thing but with the essentially identical dets at its heart hidden.

HTH

a7

Hello,

I need to control another device status 24/7 by using Arduino as external high-speed >10kHz oscilloscope with compact data logging (store signal only on change, thus don’t save signal all the time, don’t save identical signal level, also to minimize storing data will be nice to store oscillogram as vector segments).

Does Arduino have any completed solutions for a such task?

I don’t really know how Arduino distributes and uses it’s memory and it’s overall performance, thus can you help to estimate the max memory & speed of Arduino Nano/Mega possibilities for the next work:

  1. read sensor data (sensor will return number between 0 and 999’000) at high speed (will be nice up to 15-30kHz or great even up to 1MHz);
  2. store data at RAM to temporal stack for a most longer monitoring period as possible (will be great up to 0.1-0.03sec);
  3. analyze does sensor data changes substantially for this period of time;
  4. if data changes then try to find the pattern and return a pattern frequency and amplitude;
  5. store to SD only one unique pattern sample better at vector segments format.

A) Please, help to estimate the max performance which Arduino Nano/Mega can handle with a such task: the max speed of sensor data measurement by bus and CPU performance (1) and the max time of sensor data measurement storing which will fit into RAM (2) and still have enough performance to analyze data from (1-2) by algorithms (3-4)?
B) Do you know any completed solutions or libraries for Arduino which can help with a such task?
C) Do you know any oscillogram vector segments format which later can be opened at special Windows SW for extended analyzing?
D) Can Raspberry Pi handle a such task better?

Idahowalker
There are 7 pages :\

alto777
The main problem with milliseconds counter - that there are possibility that it will not hit the real Arduino CPU frequency, thus sometimes time in milliseconds can skip or double code working. Even with using a range.

Because of this Arduino commonly uses function "Delay(1000)" which, suppose, create a standalone self-initiated timer to run loop() code next time.

But manual counting of milliseconds may not hit exactly 3000 etc.

Any solutions based on a counting of a real CPU clock rate or based on a 2 Delay() functions?

brov:
Idahowalker
There are 7 pages :\

NOTE for newcomers. The demo is entirely contained in this Post and Reply #1. There is no need to read further unless you are interested. …added 25Sep2014

There are 7 pages :\

So start reading :wink:

You might have issues, I think, processing all the sensors, SD card and display operations while performing a jitter-free 10kHz read/process rate on one of the sensors. You're going to have characterize the time impact of SD and display operations and look to see if they -- and all the other processing you want to do -- can be interleaved into the 100uS time slots a 10kHz rate provides.

I'm confused by your "...that it will not hit the real Arduino CPU frequency..." wrt millis(). Can you clarify?

You should consider using an ESP32 and running tasks on different cores

Dear friends,

Thanks for your answers!

But I'am a 100% noob at Arduino and at such HW developing. :slight_smile:

Make a complex Arduino data-logger with about 10 modules at most simple Arduino - are already not very easy for me, in most because I face to small bugs-problems at most steps (new type screen problems, SD strange problems, etc), so it's not very easy and a time-wasting.
I'm afraid that using of any custom chips as ESP32 which have less support and less samples at internet will be even more complex? Maybe the best way to complete&debug project on Arduino and then just transfer it to ESP32?

BTW maybe right to use a much more speedy RaspberryPi? It have an integrated clock+SD+WiFi+memory. But to use it with a fast analogue sensors it will require some high-speed Analog-to-Digital converter which must handle 10-30kHz or even up to 1MHz - do you know a such converter? Does RaspberryPi have the same amount of a already worked libraries for sensors and dispalys?

If you are a complete beginner facing a complex project, it's better to list all your basic requirements in excruciating detail, and allow us to suggest approaches, rather than for you to assume and guess what you need to do, tell us only the bare outline of what you're doing, and ask how you can do those things.

I need to make an enough complex sensors logger:

  • 4 popular digital sensors which must be called just about 1 times per second (I already make them work);
  • 1 digital or analogue speedy sensor, the most slow speed which is acceptable for it is 10-15kHz, the most fast speed which will be great to achieve is up to 1MHz (important details at: https://forum.arduino.cc/index.php?topic=727164 );
  • OLED screen for their measurement displaying (work after some problems, unnecessary details at: https://forum.arduino.cc/index.php?topic=727063);
  • SD-card logging 1 per second but to 2 standalone files - one for a main log, and second for a fast analogue sensor oscillogram storing (still does not work: https://forum.arduino.cc/index.php?topic=727116);
  • Clock module to save logs with data-time;
  • WiFi module for logs transfer to PC/Notebook/Smartphone;
  • Keyboard for WiFi and logger modes setup via menu;
  • Maybe later additional block for a measurement device charging and also logging via 5V USB up to 5A.

I will be much more happy with some integrated main board which will have: more powerful CPU + WiFi + clock + SD.

For the "once per second" processes, you can almost cut and paste the Blink Without Delay example sketch, and just substitute your process for the LED update.

1 digital or analogue speedy sensor

This is far, far too vague. You're not even sure if it's analog or digital? How can you proceed with something like that, let alone give advice... and please define "speedy"... another completely vague term.

Overall I need to make an enough complex sensors logger:

  • 4 popular digital sensors which must be called just about 1 times per second (I already make them work);
  • 1 digital or analogue speedy sensor, the most slow speed which is acceptable for it is 10-15kHz, the most fast speed which will be great to achieve is up to 1MHz (important details at: https://forum.arduino.cc/index.php?topic=727164 );
  • OLED screen for their measurement displaying (work after some problems, unnecessary details at: https://forum.arduino.cc/index.php?topic=727063);
  • SD-card logging 1 per second but to 2 standalone files - one for a main log, and second for a fast analogue sensor oscillogram storing (still does not work: https://forum.arduino.cc/index.php?topic=727116);
  • Clock module to save logs with data-time;
  • WiFi module for logs transfer to PC/Notebook/Smartphone;
  • Keyboard for WiFi and logger modes setup via menu;
  • Maybe later additional block for a measurement device charging and also logging via 5V USB up to 5A.

I will be much more happy with some integrated main board which will have: more powerful CPU + WiFi + clock + SD.

Which platform you can recommend for this task?

brov:
I will be much more happy with some integrated main board which will have: more powerful CPU + WiFi + clock + SD.

Yes, yes you will.

Who is making you do this? It sounds like you are required to make something at a certain level of proficiency, like in school? Something goes missing in translation here, just curious.

I think you will find plenty of stuff for other processor boards on the internet, but experimenting with the various parts may be easier to begin with on the Arduino. Plus we can help. :wink:

a7

aarg:
This is far, far too vague. You're not even sure if it's analog or digital? How can you proceed with something like that, let alone give advice... and please define "speedy"... another completely vague term.

Just follow the link which I give, friend :wink:
Will this fast sensor be analog or digital - I don't know now because I must test all existed modules for Arduino with similar sensors at work and then maybe use one of them for beginning, but later maybe create my own more fast sensor. So it can be various.

alto777:
Yes, yes you will.
Who is making you do this? It sounds like you are required to make something at a certain level of proficiency, like in school? Something goes missing in translation here, just curious.
I think you will find plenty of stuff for other processor boards on the internet, but experimenting with the various parts may be easier to begin with on the Arduino. Plus we can help. :wink:

I just like a complex+useful+original tasks. :slight_smile:
I already figure out how Arduino works by 3 days and about 10 modules. :wink:
Can anyone SPECIFY THE EXACT platform recommendation for this overall task (details above and at thread https://forum.arduino.cc/index.php?topic=727164)?

Do you have a proper spec?

It's enough proper for me:

TheMemberFormerlyKnownAsAWOL:
Do you have a proper spec?

Blackfin:
You might have issues, I think, processing all the sensors, SD card and display operations while performing a jitter-free 10kHz read/process rate on one of the sensors. You're going to have characterize the time impact of SD and display operations and look to see if they -- and all the other processing you want to do -- can be interleaved into the 100uS time slots a 10kHz rate provides.
I'm confused by your "...that it will not hit the real Arduino CPU frequency..." wrt millis(). Can you clarify?

UKHeliBob:
You should consider using an ESP32 and running tasks on different cores

aarg:
If you are a complete beginner facing a complex project, it's better to list all your basic requirements in excruciating detail, and allow us to suggest approaches, rather than for you to assume and guess what you need to do, tell us only the bare outline of what you're doing, and ask how you can do those things.

alto777:
Yes, yes you will.
Who is making you do this? It sounds like you are required to make something at a certain level of proficiency, like in school? Something goes missing in translation here, just curious.
I think you will find plenty of stuff for other processor boards on the internet, but experimenting with the various parts may be easier to begin with on the Arduino. Plus we can help. :wink:

Friends, please, help with platform choosing (Mega2560/ESP32/RaspberryPi) and estimate their possible performance at my other thread:
https://forum.arduino.cc/index.php?topic=727164.