Double click "Laser On" Double click "Laser Off"

Hello everyone, Thank you all for your time.
I Have my code that when I keep holding the button the laser turn on "HIGH"
when I release my finger from the button the laser turns off "LOW"

But I want when I double click the laser turn on and when I double click again the laser turn off.

Thank you and sorry for my bad English.

int laserPin = 10;
const int btn = 6;// The push button
int previousButtonStateLAZER = HIGH;   // for btn6

void setup(){
  pinMode(btn, INPUT);
  pinMode (laserPin, OUTPUT);
}

void loop(){
  int buttonStateLAZER = digitalRead(btn);
 
  // if the button state has changed,
  if (buttonStateLAZER != previousButtonStateLAZER){
    if( buttonStateLAZER == HIGH ) {
      digitalWrite (laserPin, HIGH);
    }else{
      digitalWrite (laserPin, LOW);
    }
  }
  previousButtonStateLAZER = buttonStateLAZER;
}

sirmajed:
But I want when I double click the laser turn on and when I double click again the laser turn off.

I'm assuming your button is wired correctly with a pull down resistor since you are not using INPUT_PULLUP and you consider HIGH to be pressed...

What you will want to do is detect two subsequent LOW to HIGH transitions (or HIGH to LOW, depending if you want time based on button pressed or button released). The question is do you want your code to wait forever for the second button press or do you want to reset your logic when a timeout has occurred?

You can start counting milliseconds using the millis() function whenever the first button press occurs and if the millisecond count reaches a defined value before the second button press you can reset and start looking for the first button press again.

Thank you.
First, the Laser will be turned off When I click the button two times the laser will turn on forever
Until I press the button two times again and the laser will turn off

I'm lost

sirmajed:
Thank you.
First, the Laser will be turned off When I click the button two times the laser will turn on forever
Until I press the button two times again and the laser will turn off

All I am saying is what if the button is pressed the first time and there is a long delay before the button is pressed a second time? Do you want to wait forever for the second press OR timeout?

Here is an example:

Press Button
-> 1 hour goes by
Press Button
Laser Turns On
Press Button
Press Button
Laser turns Off

OR

Press Button
-> 1 hour goes by. Timeout has occurred and you start looking for 2 button presses again
Press Button
Press Button
Laser Turns On
Press Button
Press Button
Laser turns Off

The code in this link shows how to deal with different click types

...R

Robin2:
The code in this link shows how to deal with different click types

...R

That's pretty nice.