I have an air assist pump for a laser cutter that needs to run only when the laser is on. The laser itself has an activity LED that only goes on when the laser is firing and turns off when its not firing. During an engraving operation it can flicker on and off many times per second.
I want to detect the voltage going to the laser's built in LED activity light and turn on a relay operating the air pump when the laser's LED activity light first comes on. However, I need to ignore all of the split second "flickering" while the laser is busy engraving and only turn the pump off after the laser has not been firing for a full second. However, if the laser starts firing again after being off for a full second (if for example the laser has been busy moving to a new location on the bed), I need the air pump to come back on... rinse and repeat.
I am very new to arduino and I have tried assembling bits and pieces of code from other examples and I have a basic sketch that will turn on a pin to the air pump's relay when incoming voltage from the Laser's activity LED connected to an analog input exceeds 3v and then off when it drops below 3, but I can't figure out how to add code to ignore the "flickering" unless its been off for a full second.
Copied below is what I have so far. Any help would be greatly appreciated. Thanks!
int ledPin = 13; //internal LED for monitoring function
int relaypin = 2; //output to drive relay that will turn on air pump
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(relaypin, OUTPUT);
}
void loop()
{
float volts;
volts = analogRead(A1); // returns bit (0= 0 volts, 1024=5.0 volts)
volts = volts * 5.0 / 1024.0; // convert bits to volts
Serial.println(volts, 3); // show incoming voltage on A1 pin
// now test the incoming voltage to see if the laser LED activity light is on
if (volts >= 3.0) {
digitalWrite(relaypin, HIGH); // turn on air pump relay
digitalWrite(ledPin, HIGH); // turn on internal LED for monitoring
}
else {
digitalWrite(relaypin, LOW); // turn off air pump relay
digitalWrite(ledPin, LOW); // turn off internal LED for monitoring
}
}
Only glanced at the code , really using floats for your voltage adds to the complexity and the results can mislead as you only have 1part in 1024 - you can use int instead of float , then, as you’ll need to calibrate it use “map” to get the 0-1023 to 0 to 5000mV, varying the 5000 to suit the calibration you end up with .
Then just work in millivolts throughout , only needing integer arithmetic .
Regarding the air pump controll logic I suggest You start the pump when the laser starts. For every turn in loop You check if the laser is on and sets a pump turn off time a few seconds ahead. That way the fan will run for those seconds longer than the laser run. That extra air pump running ought to be okey, maybe even be good for the laser that likely has some built up temperature inside.
Yes, and you can take this a step further, leave your readings in ADC values and then do your comparisons using compiler arithmetic and integers, for example
const int VOLTAGE_THRESHOLD = 1024 * 3.0/Vmax;
...
int voltsADC = analogRead(pin);
...
if (voltsADC >= VOLTAGE_THRESHOLD) {
in place of
if (volts >= 3.0) {
This will eliminate slow floating point comparisons at run time.