|
| 1 | +/* |
| 2 | + pulseIn |
| 3 | +
|
| 4 | + This sketch allows to test pulseIn() and pulseInLong() functions. |
| 5 | +
|
| 6 | + A PWM signal is generated on 'pwm' pin and then measures are performed on |
| 7 | + 'in' pin for the HIGH and LOW state. |
| 8 | + The min and max value for both are displayed on the Serial console. |
| 9 | +
|
| 10 | + Creation 15 Sep 2017 |
| 11 | + by Frederic Pillon |
| 12 | +
|
| 13 | + This example code is in the public domain. |
| 14 | +
|
| 15 | + https://www.arduino.cc/en/Reference/PulseIn |
| 16 | +*/ |
| 17 | + |
| 18 | + |
| 19 | +// Use pulseInLong()? |
| 20 | +#define USE_PULSEINLONG 1 |
| 21 | + |
| 22 | +static uint32_t pwm = 9; |
| 23 | +static uint32_t in = 4; |
| 24 | +static uint32_t state = HIGH; |
| 25 | + |
| 26 | +void setup() { |
| 27 | + pinMode(in, INPUT); |
| 28 | + Serial.begin(115200); |
| 29 | + analogWrite(pwm, 127); |
| 30 | +} |
| 31 | +static uint32_t min_duration_low = (uint32_t)(-1); |
| 32 | +static uint32_t max_duration_low = 0; |
| 33 | +static uint32_t min_duration_high = (uint32_t)(-1); |
| 34 | +static uint32_t max_duration_high = 0; |
| 35 | +#if USE_PULSEINLONG |
| 36 | +static uint32_t min_durationLong_low = (uint32_t)(-1); |
| 37 | +static uint32_t max_durationLong_low = 0; |
| 38 | +static uint32_t min_durationLong_high = (uint32_t)(-1); |
| 39 | +static uint32_t max_durationLong_high = 0; |
| 40 | +#endif |
| 41 | + |
| 42 | +void loop() { |
| 43 | + uint32_t duration = 0; |
| 44 | + duration = pulseIn(in, state); |
| 45 | +#if USE_PULSEINLONG |
| 46 | + uint32_t durationLong = 0; |
| 47 | + durationLong = pulseInLong(in, state); |
| 48 | +#endif |
| 49 | + if (state == HIGH) { |
| 50 | + Serial.print("PulseIn HIGH state duration:"); |
| 51 | + min_duration_high=min(min_duration_high,duration); |
| 52 | + max_duration_high=max(max_duration_high,duration); |
| 53 | + Serial.print(" min= "); |
| 54 | + Serial.print(min_duration_high); |
| 55 | + Serial.print(" max= "); |
| 56 | + Serial.print(max_duration_high); |
| 57 | +#if USE_PULSEINLONG |
| 58 | + min_durationLong_high=min(min_durationLong_high,durationLong); |
| 59 | + max_durationLong_high=max(max_durationLong_high,durationLong); |
| 60 | + Serial.print(" min (long)= "); |
| 61 | + Serial.print(min_durationLong_high); |
| 62 | + Serial.print(" max (long)= "); |
| 63 | + Serial.print(max_durationLong_high); |
| 64 | +#endif |
| 65 | + state = LOW; |
| 66 | + } |
| 67 | + else { |
| 68 | + Serial.print("PulseIn LOW state duration:"); |
| 69 | + min_duration_low=min(min_duration_low,duration); |
| 70 | + max_duration_low=max(max_duration_low,duration); |
| 71 | + Serial.print(" min= "); |
| 72 | + Serial.print(min_duration_low); |
| 73 | + Serial.print(" max= "); |
| 74 | + Serial.print(max_duration_low); |
| 75 | +#if USE_PULSEINLONG |
| 76 | + min_durationLong_low=min(min_durationLong_low,durationLong); |
| 77 | + max_durationLong_low=max(max_durationLong_low,durationLong); |
| 78 | + Serial.print(" min (long)= "); |
| 79 | + Serial.print(min_durationLong_low); |
| 80 | + Serial.print(" max (long)= "); |
| 81 | + Serial.print(max_durationLong_low); |
| 82 | +#endif |
| 83 | + state = HIGH; |
| 84 | + } |
| 85 | + Serial.println(); |
| 86 | + delay(1000); |
| 87 | +} |
0 commit comments