Limit digitalwrite time

Hey, you glorious, iridescent landsharks...I, once again, need your help.

I've been building a laser diode that fires on the push of a button, and I'm using a delay (see code below) to stop it from firing too long after the button is pressed AND THEN IMMEDIATELY RELEASED.

However, what I need now is a way to prevent it from firing too long if the button is held down, and so far, I'm comin' up empty.

Here's the code. Please let me know what, if anything, can be done. Thanks!

int pinButton = 4; //the pin where we connect the button
int LASER = 7; //the pin we connect the laser

void setup() {
  pinMode(pinButton, INPUT_PULLUP); //set the button pin as INPUT
  pinMode(LASER, OUTPUT); //set the LASER pin as OUTPUT
}

void loop() {
  int stateButton = digitalRead(pinButton); //read the state of the button
  if(stateButton == 1) { //if pressed
     digitalWrite(LASER, LOW); //write 1 or HIGH to LASER pin
  } else { //if not pressed
     digitalWrite(LASER, HIGH);  //write 0 or low to LASER pin
     delay(250);
  }
}

Change your variable stateButton into a static variable. That way it will remember the value when it leaves the loop function. Its like a global variable but the variable scope is the loop only. e.g.

static int stateButton = 0; //this will be executed only the first time

Now you can change the state and count up every time you enter the loop and the button is pressed. When the button is pressed too long you do not enable the laser. If the button is released you clear the counter.

This is just a hint, if you need more help, please feel free to ask.

I've been building a laser diode that fires on the push of a button, and I'm using a delay (see code below) to stop it from firing too long after the button is pressed AND THEN IMMEDIATELY RELEASED.

So to clarify?... you want the light to be on for the length of the button press if the button is released sooner than 250ms from the press, but only be on for 250ms if the button is held longer than 250?

Tags in this forum are square brackets not angles by the way...

Dude, you're totally right. Thanks for that.

As to your request for clarification, yes; essentially, no matter whether they just quick-press the button or hold it down, I'd like to be able to control how long the diode stays lit.

Klaus_K:
Change your variable stateButton into a static variable. That way it will remember the value when it leaves the loop function. Its like a global variable but the variable scope is the loop only. e.g.

This is interesting, but I'm still a relative newbie; would you be able to give an example (or link to an example) of it in action? I'm a visual learner. :slight_smile:

Look at the state change example in the IDE

Looks like state change might be exactly what I'm looking for, but BOY do I not understand it.

Can anyone show me an example of how state change can be used to fire a pin for a predetermined amount of time on a button press?

FYI: I appreciate you guys immensely.

I think this will do it

prevButtonState = buttonState;
buttonState = digitalRead(buttonPin); // assume LOW when button pressed
if (buttonState == LOW and buttonState != prevButtonState and laserON == false) {
   laserOn = true;
   laserStartTime = millis();
}

if (millis() - laserStartTime >= laseOnInterval) {
      laserOn = false;
}
    
if (laserOn == true) {
  dgitalWrite(laserPin, HIGH);
}
else {
  digitalWrite(laserPin, LOW);
}

...R

JordanG:
Looks like state change might be exactly what I'm looking for, but BOY do I not understand it.

Try harder.

JordanG is taking a 48 hour rest from the forum.

And another 48 hours for wasting my time.

ballscrewbob:
And another 48 hours for wasting my time.

what does this mean? Did you temporarily ban him as a global moderator? Was it due to DMs? Or inappropriate posts (that I didn't see)?

JordanG:
Dude, you're totally right. Thanks for that.

As to your request for clarification, yes; essentially, no matter whether they just quick-press the button or hold it down, I'd like to be able to control how long the diode stays lit.

Think what you might do via hardware(cap, resistor)
It is an alternative.