Reading an digital input pwm signal


hey,

i have dust sensor gp2y1023au0f outputs pwm signal. searched forum , read few posts seems have few options how approach such pulsein() , attachinterrupt(). according https://www.arduino.cc/en/reference/pulsein function works best longer pulses, 10ms-3000ms have tried using attachinterrupt().


the signal try read vary 6.8us - 7000us (us=microseconds). i've attached photo.

i not sure if set code correctly.
does make sense guys? uncertain if reads pwm signal appropriate , appreciate suggestion improve readings.im still new arduino=)

https://www.arduino.cc/en/reference/attachinterrupt

code
code: [select]

volatile int pwm_value = 0;
volatile int prev_time = 0;
 
void setup() {
  serial.begin(115200);
  // when pin d2 goes low , call rising function
  attachinterrupt(0, falling, falling);
}
 
void loop() { }
 
void rising() {
  attachinterrupt(0, rising, rising);
  prev_time = micros();
}
 
void falling() {
  attachinterrupt(0, falling, falling);
  pwm_value = micros()-prev_time;
  serial.println(pwm_value);
  //delay(1000);
}


1) expect see attachinterrupt(...) calls in setup , in setup().  stands, there never call rising(), , falling() call attachinterupt(...) unnecessarily repeat has been done.

2) printing inside interrupt service routine (aka isr) (such falling() ) bad idea.  printing needs interrupts can't happen while inside isr.  can deadlock?

3) return value micros() unsigned long.  therefore, pwm_value , prev_time value not declared properly.  point, compiler handle you, fail of longer pulses.

4) depends upon type of arduino have, on many arduinos resolution of micros() greater 1 microsecond.  example, on uno resolution 4 microseconds.  going have difficulty measuring 6.8 microseconds.  in fact, micros() can never provide information fractions of microsecond.

good concept needs work.


Arduino Forum > Using Arduino > Programming Questions > Reading an digital input pwm signal


arduino

Comments