I'm going to use the acceleration sensor and RGB LED to output different colors depending on the range of values of the sensor.
However, there is a problem where the led changes too quickly as the sensor values change rapidly.
So I'd like to print out the LED only when the sensor's value stays in a certain range for more than a second, but I don't know how to do it.
Can I find a similar example or something to refer to?
Thank you!
yscheol:
So I'd like to print out the LED only when the sensor's value stays in a certain range for more than a second, but I don't know how to do it.
I'm not sure that this is the answer to your problem, if not it may help you to clarify what you want.
To do something only when a value has been exceeded for a period of time you need to think "upside down" and reset the clock every time the value is below the threshold. Like this
if (value < threshold) {
lastTimeValueWasLow = millis();
}
if (millis() - lastTimeValueWasLow >= 1000) {
// value was above the threshold for 1 second
}
How many ranges? What are the limits of each range? What does the RGB do if the acceleration has not been in the current range for over a second?
If it were me, I would be more interested in the maximum acceleration over the last second. To do that I would keep a circular buffer of the last ten tenths of a second. When each new reading comes in, add it to the buffer and display based on the highest value in the buffer.