Hi there,
I' ve a small problem making some timer-based code. Let me explain: I' ve an Arduino mega board and I would like to receive and sample some pwm signals (I' m going to use pulseIn) so storage in two variables the min&max values I get. I' ve written this really simple code
So here, how you can see, the pulseIn function is looped 50 times. That' s the matter: I would like to get and sample the pulses, -> storage the min and max value and if these values don' t change for a time (for example 10 seconds) then exit the loop... I really don' t know how I could compare Input_min while time is going forward. It might be a stupid question, but please help me!
Thank you,
Pentium
Pentium:
Hi there,
I' ve a small problem making some timer-based code. Let me explain: I' ve an Arduino mega board and I would like to receive and sample some pwm signals (I' m going to use pulseIn) so storage in two variables the min&max values I get. I' ve written this really simple code
So here, how you can see, the pulseIn function is looped 50 times. That' s the matter: I would like to get and sample the pulses, -> storage the min and max value and if these values don' t change for a time (for example 10 seconds) then exit the loop... I really don' t know how I could compare Input_min while time is going forward. It might be a stupid question, but please help me!
Thank you,
Pentium
If using your design, I would have an extra couple of variables. I would store the "last changed" min and max, and compare those with the incoming values each iteration. If they differ, then set a third variable to the current time and set the "last changed" min and max to the current min and max.
Then I compare the "last changed" time with the current time, and if the difference is greater than or equal to 10,000 (10 seconds) then either set i to be more than the limit of the loop, or execute a "break" instruction to drop out of the loop.
majenko:
If using your design, I would have an extra couple of variables. I would store the "last changed" min and max, and compare those with the incoming values each iteration. If they differ, then set a third variable to the current time and set the "last changed" min and max to the current min and max.
Then I compare the "last changed" time with the current time, and if the difference is greater than or equal to 10,000 (10 seconds) then either set i to be more than the limit of the loop, or execute a "break" instruction to drop out of the loop.
Mmm...how can I store the "last changed"? I mean, that value should be stored in the loop cycle so it would be always equals with the incoming value. I' m a bit confused ahah
Mmm...how can I store the "last changed"? I mean, that value should be stored in the loop cycle so it would be always equals with the incoming value. I' m a bit confused ahah
That is remembering the min and max only when they change. At the time they change it sets a timestamp. Then, on every iteration, it checks the difference between the timestamp and the current time. If it exceeds a certain timeout (10,000 ms), then exit the loop.