Laser tag countdown approach

Hello,

I am working on a project that will simulate a laser tag game. You’d start with 5 LED lights turned ON, and then once a photo resistor crosses a certain value, it would turn one of the LED lights OFF. Then when the photo resistor crossed that value again, another LED would turn OFF. This would happen until all of the LED lights are turned OFF. At this point, the photo resistor could cross the value again to turn all of the lights back ON (basically, set up to play again).

I’m running into problems with the analogRead command not turning my LED lights off consistently. Sometimes they turn off in a non-normal sequence, but sometimes the board just “stalls” and doesn’t do anything until I reset it. Previously, my code used an IF statement, but the problem with that is that the code runs in the loops so many cycles in a small time, it would be as if the photo resistor repeatedly crossed the target value in a short time, so I tried implementing a while loop to detect when it crosses a single time to turn the LED off only one time per crossing of that threshold value.

Here’s my code, any advice would be great. Thanks!

int button = 2;      // pin to connect the button
int presses = 0;    // variable to store number of presses
int val;     // variable for reading the pin status
int lightPin = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  pinMode(lightPin, OUTPUT);


}

void loop()
{
  int photo = analogRead(lightPin);
  int Photo = map(photo, 550, 685, 0, 1023); // remap read values to be more sensitive
  Serial.println(Photo);
  delay(10);
  while (Photo >= 850) {
      photo = analogRead(lightPin);
      Photo = map(photo, 550, 685, 0, 1023); // remap read values to be more sensitive
    if (Photo < 850) {
      presses = presses + 1;
      break;
    }
    if (presses == 1) {
      digitalWrite (11, LOW);
    };
    if (presses == 2) {
      digitalWrite (10, LOW);
    };
    if (presses == 3) {
      digitalWrite (9, LOW);
    };
    if (presses == 4) {
      digitalWrite (8, LOW);
    };
    if (presses == 5) {
      digitalWrite (7, LOW);
    };
    if (presses == 6) {
      presses = 0;
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
    };

  }
}

Understand what "break;" does.

Understand Debounce and StateChangeDetection examples, and apply the same logic to your sensor. Insert Photo>=850 for the active sensor state in the example code.