//https://arduinogetstarted.com/tutorials/arduino-button-debounce
//the number of the pushbutton pin
const int FireButton = 11;
//the number of the Laser pin
const int LASER = 12;
int Fire_State; // variable for reading the pushbutton status
int button_state = 0;
const int DEBOUNCE_DELAY = 1; // the debounce time; increase if the output flickers
// Variables will change:
int lastSteadyState = LOW; // the previous steady state from the input pin
int lastFlickerableState = LOW; // the previous flickerable state from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
void setup() {
//Serial.begin(9600);
//initialize the pushbutton pin as an input:
pinMode(FireButton, INPUT_PULLUP);
pinMode(LASER, OUTPUT);
}
void loop() {
// read the state of the pushbutton value:
Fire_State = digitalRead(FireButton);
//Serial.println(digitalRead(LASER));
//check if the pushbutton is pressed. If it is, the Fire_State is HIGH:
digitalWrite(LASER, LOW);
// If the switch/button changed, due to noise or pressing:
if (Fire_State != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableState = Fire_State;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
//whatever the reading is at, it's been there for longer than the debounce
//delay, so take it as the actual current state:
//if the button state has changed:
if (lastSteadyState == HIGH && Fire_State == LOW) {
digitalWrite(LASER, HIGH);
}
else if (lastSteadyState == LOW && Fire_State == HIGH) {
digitalWrite(LASER, LOW);
}
//save the the last steady state
lastSteadyState = Fire_State;
}
}