I have this state machine to detect three levels of punch impacts on a wall-mounted PU-mat (no scientific project), using two thresholds and two yellow and red LEDs (later there will be three thresholds and three LEDs). The trouble is that with a strong impact, the lower threshold is crossed first, hence both LEDs light up, but only the second one should. Is there some method to code for waiting if after thresholdA is crossed thresholdB is also crossed right after, to then light up only the red LED?
How should I solve this issue? Thanks for some ideas.
All right, operator precedence. Had not thought of that. However, the yellow LED still comes on for a very brief moment sometimes.
Maybe I need some kind of peak detection to see if in a certain timeframe (maybe 50 milliseconds?) the value continues to rise or starts falling? Something along those lines maybe, but that's only for one threshold and one LED, quite different from the state machine approach...
if (mAVEMA > mAVPeak)
{
mAVPeak = mAVEMA; // New peak
}
if (mAVEMA < thresholdA) // Below A
{
if (mAVPeak > thresholdA) // Above A
{
digitalWrite(pinLEDY, HIGH);
timeNow = millis();
mAVPeak = 0;
}
if (millis() - timeNow >= timeLEDOn)
digitalWrite(pinLEDY, LOW);
}
Sorry, but could you explain? On the way to the red state, the yellow state will inevitably be passed. I increased the sampling rate to 800 MHz, and that's better, but still no cigar.
Not really. A rising signal that crosses the strong threshold must, at some point, have crossed the intermediate threshold, and the reverse when the strong signal falls off.
It sounds like the real problem is measurement timing, and keeping track of whether the signal is rising or falling.
For that, you need to retain a bit of the measurement history, one or more "last measured" points and their measurement times for comparison. Look up "peak detection algorithms" for some ideas.
Of course all this depends on carefully defining what you want the indicators to represent.
I think you're right. It's the rising/falling that can help to differentiate. What I cobbled together here ADXL345 punch level detection - #3 by Lagom works, but only for one threshold - but I want to have three, eventually.
take the time of when the lowest threshold is crossed by the rising signal
then wait to see if a higher threshold is crossed
then wait to see if an even higher threshold is crossed
and then wait some more to see if yet another higher threshold is crossed.
And finally set the corresponding LED pin to HIGH. So, a bit like with peak detection in an audio signal, I'd need a while loop, long enough to find potentially higher thresholds, short enough to be ready for the next punch. From my sampling rate and the serial monitor x-axis tick marks, I guesstimate that a 50 milliseconds while loop should suffice. Not sure how to code this, but that seems like a viable approach?
Peak detection with a sampling window worked rather well the two times I used it, and nearly all examples I saw used that, so I haven't seen an array approach yet (apart from getHighest() with this library. But in my case here, I'm not after peaks, but a number of thresholds that are crossed before a peak occurs, successive thresholds the signal may cross while it rises.
Are there some multiple threshold finding examples with continuously updating arrays one can learn from?