I have a total of 15 sensors (analog and digital) connected to an arduino mega. For my real-time control application, it's really really important that all data be collected simultaneously at regular intervals in time. Is there any way to ensure that all data is collected at the same time? How can I minimize the total lag time between the first and last sensors?
closed as unclear what you're asking by jfpoilpret, The Guy with The Hat, sachleen Aug 22 '14 at 0:02
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I would suggest looking into threading.
To minimize the time from the first read to the final read, an interrupt and an interrupt timer would probably be good. Have the interrupt timer fire after some interval and place all of your sensor reads in the Interrupt Service Routine (ISR).
I would also suggest staying away from functions that the Arduino platform provides, like
delay(), digitalRead(), digitalWrite(), analogRead(). Those functions will add extra instructions to your sensor reads and waste clock cycles. The Arduino platform sacrifices speed for usability in some cases.
You might also want to look into using a real-time OS if this is a real-time application
The arduino will not support 15 true threads. if you look into The
TimedAction class you might see something that you can use. Basically, timed actions occur after an interval and a specified function is called. However, if you have 15 timed actions all with the same duration, they won't all read at the same time, they will still have to read in some order, one at a time.
If you look into the source code for many of the functions you can see how they work. The Arduino source code comes with the IDE. A good place to start would be Arduino.h. If speed is a concern, you want to reduce as many clock cycles as possible, which means removing as many lines of code as possible. If you can do something without calling
digitalRead() for example, you will save yourself some time.
A real-time operating system is an OS that is used in real-time systems. I find it helpful to think of a RTOS as a timing library that will help you, opposed to installing Linux on your arduino. There is plenty of material around the web on what a real-time system is and what a real-time OS does if you want to learn more.
The important part of an RTOS is that is guarantees that an action be performed before a deadline. So in your case, sensors are read before a given time. There are a couple RTOS's for the Arduino that google will find for you.