Burst Fire an LED using a switch

I want to fire an LED like a rifle. You pull the trigger, it shoots once, you have to release the trigger and pull it again to shoot a second time.

To achieve this, I have a switch on a controller sending a signal to a receiver that is connected to my arduino. I cannot change the switch out for anything. It is either always on or always off. If the switch is on, I want the LED to fire once for half a second and then shut off until the switch is turned off and turned back on. (I'm trying to avoid spray and pray when firing at a target).

I don't understand the reason why my code does not work. Currently, the LED does not fire at all if the switch is on or off. Before this rendition, It would constantly fire when the switch was on and turn off when the switch was off. Neither situation is what I want to happen.

Could someone tell me where my error is or if I need to go in a different direction? If so, could you point me in that direction?

(I'm not very good at comments so ask if something is confusing please)

int ch1; // channel
bool on1 = false;

void setup() {

pinMode(5, INPUT); // Set input pins
Serial.begin(9600);
pinMode(13, OUTPUT);

}

void loop() {
  ch1 = pulseIn(5, HIGH, 25000); // Read the pulse width of each channel

  Serial.print("Channel 1:"); // Print the value of pin 5 input
  Serial.println(ch1);
  
  if(on1 = false && ch1>1700) //if switch is off from previous loop, but has been swithched on during current loop
  {
    Serial.println("Laser Engaged"); //status of laser
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    on1 = true;
    Serial.println(on1); // is on1 true or false
  }else if (on1 = true && ch1>1700) //If laser has been on for more than one loop, laser is disengaged
  {
    Serial.println("Laser Disengaged");
    digitalWrite(13, LOW);
    on1 = true;
    Serial.println(on1);
  }else if (on1 = true && ch1<1700)//if swtich has been turned off, resets on1 to false
  {
    Serial.println("Laser Disengaged");
    digitalWrite(13, LOW);
    on1 = false;
  }else
  {
    Serial.println("Laser Disengaged");
    digitalWrite(13, LOW);
    on1 = true;
  }
  delay(100);
}

Not sure what other problems you might have, but when you do this:

if(on1 = false && ch1>1700)

you are setting on1 to false, not checking its value. You mean to do a comparison:

if(on1 == false && ch1>1700)

You might be over thinking it. Check out the state change detection example on the IDE. If a state change occurs and the new state is on then blink the LED. That way it has to cycle back to off before it can be on again.

No state change do nothing.

State change and new state is on blink LED

State change and new state is off do nothing

have you tried something simple like this?

void setup() {
pinMode(5, INPUT); // Set input pins
pinMode(13, OUTPUT);
}

void loop() {
  
if(digitalRead(5)) // button is pressed // fire the lazer
    Serial.println("Laser Engaged"); //status of laser
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
     delay(500); // minimum cycle time
  while(digitalRead(5)); // wait till button is released
  

}

zhomeslice- I tried your method and it did not work. The LED is always on. I don't really understand the principle idea behind your code so I couldn't tinker with it much.

Jimmy60 - That was excellent advice and it almost works. My controller is PWM so the value when the switch is off is between 960 and 980 and between 1960 and 1980 when its on. I Found this using pulseIn(). I can't use a digitalread() because it randomly chooses what's high and low causing the state to change rapidly and leaving the LED blinking on and off. Now when I run my code it stays off when the switch is off and stays on when the switch is on. It seems to be bypassing the state change detection.

Blue Eyes - Thank you for the advice. I haven't quite fully grasped how the logic statements work in this language yet. Still didn't fix my issue though.

Jimmy60 my new code is:

// this constant won't change:
const int  buttonPin = 5;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int LBS = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = pulseIn(buttonPin, HIGH, 25000);
  Serial.println(buttonState);
  // compare the buttonState to its previous state
  if (buttonState != (LBS-100<=LBS<=LBS+100)) {
    // if the state has changed, increment the counter
    if (buttonState > 1700) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(13, LOW);
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
      digitalWrite(13, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  LBS = buttonState;
}

I figured it out. For anyone who may have the same issue I had in the future, here is the working code. It is the StateChangeDetection example modified.

// this constant won't change:
const int  buttonPin = 5;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int LBS = 0;     // previous state of the button
void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = pulseIn(buttonPin, HIGH, 25000);
  Serial.println(buttonState);
  // compare the buttonState to its previous state
  if (buttonState >= LBS+50 || buttonState <= LBS-50) {
    // if the state has changed, increment the counter
    if (buttonState > 1700) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(13, LOW);
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
      digitalWrite(13, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
      digitalWrite(13, LOW);
      delay(100);
  }
  // save the current state as the last state,
  //for next time through the loop
  LBS = buttonState;
}