In one of my projects I have to read the pulse width of several inputs and determine which input that has the best signal (the shorter width, the better signal) and actively switch to that input.
My first approach was to measure this using the built in pulseIn() command but soon realized that this was to slow because I had to read one input at a time.
Now I've tried to understand how port manipulation works to read the state of several inputs at the same time into a byte of data. This is a great way of doing it and the code gets smaller at the same time. The problem I have though is that I have no idea of how to actually read the pulse width of the inputs with this method.
So my question is, does anyone have any good ideas of how to do this?
In one of my projects I have to read the pulse width of several inputs and determine which input that has the best signal (the shorter width, the better signal) and actively switch to that input.
Age old problem of multiple things at once.
Q: is the data synchronous or asynchronous... that is, do ALL of the low-to-high (or converse) occur instanteously for the leading edges? If so, then you are only concerned with the first signal that goes from high-to-low (or conversely.) That is, all the leading edges are aligned, the first transition is the winner.
Now, if the data is async, the leading edges are not aligned but all 4 signals are in their own time-domains, then the problem is more complex because you are going to have to select the most narrow pulse which means every pulse will need to be timed. This does not lend itself to capturing the port buffer word... well, not directly. Depending on the minimum pulse width, you could try interrupts but you are going to have to utilize very efficient interrupt routines or you will lose a transition before you re-enable the interrupts.
If async and repeatedly available, you could monitor each input for pulse-width and select the most appropriate channel if your program logic does not have to change instanteously with each signal; that is, if you can monitor, analyze, select, and react. What you are fighting is the Nyquist rate of change.
Many uC systems use external logic to make the determination for this kind of problem since a single core uC is essentially a state-machine at the lowest level: capable of one decision at a time.
Following from what @mrburnette said (to distinguish between the Rays) I wonder if the 4 signals are repetitive and is it necessary to make the decision all at once - or could you sample each signal once spread over (say) a few millisecs and then decide which one to accept. This is prompted by your use of the term "best signal".
It would help to know what is producing the signals and what you are actually trying to achieve.
Let me start by thanking you all for really great answers, I hope I can make a more understandable explanation of the different stages of my design.
First of all, there should be (for the time being) up to a maximum of 8 inputs with asynchronous pulses that is going to be measured, all inputs represents a source of incoming audio that is feed through a multiplexer (8 to 1) and the inputs determines the best source to use in real-time. The inputs has a signal between 5-7 KHz that is in a square wave form, the higher (or lower if you want) frequency, the better signal.
Although the inputs have the same source that is feed into the final stage, the actual tone that is being measured is not locked in any way between the inputs so individual timing might be a must. Also, all inputs are not active at the same time, only the sources that has a signal at all and so, the controller has to use what sources it has.
Now if my calculations are right, I have to make all the measurements and decide input under 10ms to make a smooth transition between sources and not get noise and other effects when a source is getting weak, this also involves a hysteresis but that is a different matter.
Basically, these are the inputs and outputs that I should need.
Audio x8 --> Multiplexer 8 to 1 --> Output
Audio Quality (pulse) x8 --> uC --> Multiplexer
Input Active x8 --> uC (This might not be necessary if no pulse is measured, thus skipping the inputs)
Please do tell me if more information is necessary.
This bothers me. What do you mean by "getting weak". Usually it means that the amplitude is decreasing, not the frequency.
If the frequency changes, what causes it to change and why does that change make that source less desirable?
When you talk about a multiplexer do you mean that the Arduino can select any one of the 8 signals for sampling, one at a time? That was not the impression I got from your first post which seemed to suggest that you needed to sample a number of signals at the same time.
but soon realized that this was to slow because I had to read one input at a time.
I think a good description of the overall purpose of the project would be a big help.
Robin, sorry for the confusing explanation. I thought it would make things more clear but I ended up in making it worse!
The signal quality measurement is being done in an earlier stage (forgot to mention this, sorry) so the only thing we have to focus on here is the "Audio Quality (pulse) x8".
Basically, it's just 8 inputs with frequencies between 5-7 KHz, and the one with the highest (or lowest) frequency wins.
swoshie:
Basically, it's just 8 inputs with frequencies between 5-7 KHz, and the one with the highest (or lowest) frequency wins.
Kind regards,
Swoshie
Get out a copy of SPICE...
Create RC network (filter) that use the square wave to charge a quality capacitor. Pick a Nano (or other model) with 8 or more Analog inputs. Check the chip version to find which internal Vref are available or use Vcc.
So, you have 8 analog input channels... With identical filters each providing a voltage that represents the pulse width.
swoshie:
Basically, it's just 8 inputs with frequencies between 5-7 KHz, and the one with the highest (or lowest) frequency wins.
I like the idea proposed by Ray in Reply #7
If you want to use the Arduino to measure pulse widths directly I'm still not clear about the process you have in mind. You only dealt with 1 out of 4 parts in Reply #5