ALIENS Pulse Rifle Project - Need a little help :)

So I’m sure many of us here are familiar with the film Aliens! One of my favorite sci-fi films if I say so myself. One of my favorite parts of that movie is the M41A Pulse Rifle carried by the Colonial Marines. I recently acquired this bad boy and am working on adding lights and sounds to it! (Check out the attached image). I decided to code my own light and sound module as a challenge to myself but I could use a little help. Here’s the skinny.

If you look at the code below, I’m currently focusing on the ammo counter and flashing LED for the barrel. (I’ll worry about adding the sound stuff later.) In terms of hardware, I’ve got two momentary switches, a 2-digit LED display, and of course the LED itself. The first switch is used for clip detection, to simulate that the magazine has been inserted. As long as it is HIGH, the counter will drop and the LED will flash when the second switch is being held down. If it is LOW, I cannot “fire” and the display reads “00”. Here’s where I’m having trouble. If I hold down the trigger switch, the count will stop at “00” but the LED keeps faintly flashing on and off.

Now I know why this is happening. It’s because I’m fighting the ammo-- function with ammo++. The display may read “00” but the LED is caught in an endless loop instantly turning on as soon as it turns off. I’m sure there is a far more efficient way to do this. Essentially I want to make sure the LED is completely off any time the counter reads “00”. Any ideas, folks? Thanks!

#include <SevSeg.h>
SevSeg sevseg;
int ammo =  95;
int noAmmo = 0;
int LED = 3;
unsigned long previousMillis = 0;
long interval = 40;
const int triggerPin = 2;
int triggerState = 0;
const int clipSwitch = 1;
int clipState = 0;

void setup() 
{
  byte numDigits = 2;   
  byte digitPins[] = {15,16}; //Digit 1 - 15,  Digit 2 - 16
  byte segmentPins[] = {8, 9, 10, 11, 12, 13, 14, 45}; //  8-A,  9-B,  10-C,  11-D,  12-E,  13-F,  14-G,  45-DP (DO NOT USE PIN 45)
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(10);
  pinMode(triggerPin, INPUT);
  pinMode(clipSwitch, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() 
{
  triggerState = digitalRead(triggerPin);
  clipState = digitalRead(clipSwitch);
  digitalWrite(LED, LOW);
  if (clipState == HIGH)
    {
      sevseg.setNumber(ammo,1);
      sevseg.refreshDisplay();       
      if (triggerState == HIGH)
        {
          unsigned long currentMillis = millis();
          if(currentMillis - previousMillis >= interval)
            {     
              previousMillis = currentMillis;
              sevseg.setNumber(ammo,1);
              ammo--;
              digitalWrite(LED, HIGH);
              while (ammo <= -1)
                {
                  ammo++;
                  digitalWrite(LED, LOW);
                  sevseg.refreshDisplay();  
                } 
            }
            sevseg.refreshDisplay();
        }
    }   
 if (clipState == LOW)
  {
    ammo = 95;
    digitalWrite(LED, LOW);
    sevseg.setNumber(noAmmo,1);
    sevseg.refreshDisplay();
  }
}

Hi,
Welcome to the forum.
OPs pic.
7131adbbe3bb716fbed8b30817eaa1dee23d4da6.jpg
Tom… :slight_smile:

Thanks or that! Normally I would have uploaded it to my Photobucket but figured I try attaching for the sake of trying. :smiley:

Maybe you should test the counter and led status by using AND cause.
I see this is from 2016. Did you achieve the final expected state?
Thx a lot.