PulseIn - PWM monitoring

I'm currently using the pulseIn function to monitor when my laser engraver controller starts and stops firing the laser using pwm. PulseIn reads a pulse (either HIGH or LOW ) on a pin. For example, if value is HIGH , pulseIn() waits for the pin to go from LOW to HIGH , starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout. The timeout is 5 seconds.

I recently purchased a new laser that uses mirrors (galvo) in addition to the x and y movements you would typically find on a flat bed laser. The code I used for my previous machine is below and @johnwasser was a tremendous help getting that one developed. Now my dilemma, the previous code still works for this new laser, but the laser fires so quickly that I get a bunch of on and off's in succession.

What I would ideally like is for the code to wait for 1 second (at the current codes "laser off" stage) and if a "laser on" is triggered within that 1 second, consider the laser still on. If no "laser on" is received after the 1 second, then consider the laser truly off and send a "laser off".

const byte PWM_PIN = 3;

boolean WasOn = false;

void setup()
  pinMode(PWM_PIN, INPUT);

void loop()
  // If there is any pulse in the next three seconds, the laser is ON
  boolean isOn = pulseIn(PWM_PIN, HIGH, 3000000UL) > 0;
  if (isOn != WasOn)
    // State change detected
    WasOn = isOn;

    if (isOn)
      Serial.println("LASER ON");
      Serial.println("LASER OFF");

My previous post that is now closed is here: pulseIn for detecting CO2 laser status - #9 by the-dude

You want a retriggerable monoflop?

pulseIn() is not required. Assume laser is ON. Take the starting time and update it whenever a HIGH is detected before a 1 s interval. If the interval elapses the laser is OFF and goes ON with the next HIGH input.

Hey Doc, yeah the code would essentially be a mimicking a retriggerable monoflop, in a way. I have to use pulseIn() unfortunately due to some electrical noise and the fact I can only monitor the pwm signals from the controller.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.