Hello,
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);
Serial.begin(115200);
}
void loop()
{
// If there is any pulse in the next three seconds, the laser is ON
delay(50);
boolean isOn = pulseIn(PWM_PIN, HIGH, 3000000UL) > 0;
if (isOn != WasOn)
{
// State change detected
WasOn = isOn;
if (isOn)
Serial.println("LASER ON");
else
Serial.println("LASER OFF");
}
}
My previous post that is now closed is here: pulseIn for detecting CO2 laser status - #9 by the-dude