I need some help in measuring time duration of a voltage peak. The analogRead() function could tell the voltage being received by arduino, but is it possible to measure time on these analog ports?
For example, when analogRead gave value around 90-100 range for 5 seconds, then I want it to activate some output port. When it is outside of these value, or shorter than 5 seconds, then I want no signal coming from the output port.
What I aim is to ignore signal noises received by analogRead that occasionally at the 90-100 range. Signal noise is very short duration (1ms) so I figure if there is a function where it could measure the signal duration, then perhaps I could eliminate these noise.
I need some help in measuring time duration of a voltage peak. The analogRead() function could tell the voltage being received by arduino, but is it possible to measure time on these analog ports?
Save the value of millis() when the signal becomes greater than the lower theshold and less than the upper threshold, then each time through loop() test whether the current value of millis() is equal to or greater than the required period. If so then act accordingly
UKHeliBob:
Save the value of millis() when the signal becomes greater than the lower theshold and less than the upper threshold, then each time through loop() test whether the current value of millis() is equal to or greater than the required period. If so then act accordingly
Hmm, I think this could work. Let me try it and see if it runs properly. Thank you for the suggestion.
I use the following method in sound to light programs
// pin for analog sound sensor
#define soundSensor A0
// set to your threshold level
int threshold = 500;
unsigned long timeNow;
unsigned long timePassed;
void setup() {
}
void loop() {
// measures the time it takes for the signal amplitude to return to the threshold level
// thus measures the peak duration or period of a sine wave
// the longer the time the lower the frequency so can be used to
// crudely measure frequency
if(analogRead(soundSensor) > threshold) {
// timing started
timeNow = millis();
while (analogRead(soundSensor) > threshold) {
// do nothing
}
timePassed = millis() - timeNow;
}
}
Well since its sound waves it only going to halt process for a few millis or even micros.
I would recommend adding a second timing loop within the do nothing loop to provide a break if it gets stuck waiting although since my code triggers on rising amplitude it has to fall again even if sound stops.