Help needed in an Elevator Simulator

Hello there, (Re-writing this since replying apparently caused the bot to take the previous one down and I'm impatient)

Anyways, I was having issues with an elevator simulator that I found on Github: GitHub - SpiritasX/Arduino-elevator-simulator
Anyways, I was having issues with the position indicator.
The indicator should work like this:

I O O O floor1
O I O O floor2
O O I O floor3
O O O I floor4

The issue? The indicator doesn't function like that, Instead, it does this:
I O O O floor1
O I O O floor2
I I O O floor3
O O I O floor4

If anyone knows how to fix this, Please let me know.

Code:

#include <elapsedMillis.h>
#include <Bounce2.h>

const uint8_t buttonPins[] = {A2, A3, A4, A5};
const uint8_t pressedLED[] = {2, 3, 4, 5};
const uint8_t movingLED[] = {6, 7, 8, 9};
const uint8_t panicPin = A0;
const uint8_t NPN = 23;
int binaryNum[] = {1, 0, 0, 0};
bool shutdownChecker = false;
int startPos = 0;
Bounce * buttons = new Bounce[4];
Bounce panic = Bounce();

void setup() {
  Serial.begin(9600);
  for(int i = 0; i < 4; i++) {
    pinMode(pressedLED[i], OUTPUT);
    pinMode(movingLED[i], OUTPUT);
    digitalWrite(pressedLED[i], LOW);
    digitalWrite(movingLED[i], binaryNum[i]);
    buttons[i].attach(buttonPins[i], INPUT_PULLUP);
    buttons[i].interval(25);
  }
  panic.attach(panicPin, INPUT_PULLUP);
  panic.interval(25);
  pinMode(NPN, OUTPUT);
  digitalWrite(NPN, HIGH);
}

void binary(int n) {
   Serial.println(binaryNum[1]);
   Serial.println(binaryNum[2]);
     Serial.println(binaryNum[3]);
       Serial.println(binaryNum[4]);
  for(int i = 0; i < 4; i++) {
    binaryNum[i] = n % 2;
    n /= 2;
  }
}

void completeShutdown() {
  shutdownChecker = !shutdownChecker;
  if(shutdownChecker) {
    for(int i = 0; i < 4; i++) {
      digitalWrite(movingLED[i], LOW);
      digitalWrite(pressedLED[i], LOW);
      binaryNum[i] = 0;
    }
    digitalWrite(NPN, LOW);
  } else {
    binaryNum[0] = 1;
    digitalWrite(movingLED[0], HIGH);
    startPos = 0;
    digitalWrite(NPN, HIGH);
  }
}

void moving(int endPos) {
  if(endPos > startPos)
    for(int i = startPos + 1; i < endPos + 1; i++) {
      binary(i + 1);
      for(int i = 0; i < 4; i++)
        digitalWrite(movingLED[i], binaryNum[i]);
      for(int j = 0; j < 2; j++)
        pressedButton();
      if(shutdownChecker)
        return;
      if(digitalRead(pressedLED[i]) == HIGH) {
        for(int j = 0; j < 10; j++)
          pressedButton();
        if(shutdownChecker)
          return;
        digitalWrite(pressedLED[i], LOW);
      } else
        for(int j = 0; j < 5; j++)
          pressedButton();
        if(shutdownChecker)
          return;
    }
  else if(startPos > endPos)
    for(int i = startPos + 1; i > endPos + 1; i--) {
     binary(i - 1);
      for(int i = 0; i < 4; i++)
        digitalWrite(movingLED[i], binaryNum[i]);
      for(int j = 0; j < 2; j++)
        pressedButton();
      if(shutdownChecker)
        return;
      if(digitalRead(pressedLED[i - 1]) == HIGH) {
        for(int j = 0; j < 10; j++)
          pressedButton();
        if(shutdownChecker)
          return;
        digitalWrite(pressedLED[i - 1], LOW);
      } else
        for(int j = 0; j < 5; j++)
          pressedButton();
        if(shutdownChecker)
          return;
    }
  digitalWrite(pressedLED[endPos], LOW);
  startPos = endPos;
}

int pressedButton() {
  elapsedMillis counter;
  while(counter < 500)
    for(int i = 0; i < 4; i++) {
      buttons[i].update();
      panic.update();
      if(buttons[i].rose() && !shutdownChecker)
        digitalWrite(pressedLED[i], HIGH);
      if(panic.rose())
        completeShutdown();
    }
}

void loop() {
  int endPos;
  pressedButton();
   
  for(int i = 0; i < 4; i++) {
    digitalWrite(movingLED[i], binaryNum[i]);
    if(digitalRead(pressedLED[i]) == HIGH && !shutdownChecker) {
      endPos = i;
      moving(endPos);
    }
  }
}

I'm not sure the reason why you do the assignment

    n /= 2;

but it looks to me like you passed 'n' by parameter not by reference. You have a copy of the parameter, not itself. So it should be read but not written. What did you mean to do with 'n' here?

void binary(int n) {
   Serial.println(binaryNum[1]);
   Serial.println(binaryNum[2]);
     Serial.println(binaryNum[3]);
       Serial.println(binaryNum[4]);
  for(int i = 0, int m = n; i < 4; i++) {
    binaryNum[i] = m % 2;
    m /= 2;
  }
}

or something like that?

It's possible C will let you get away with it but IDK.

Like I said, I found it on Github, but I'm not sure how to fix it

Isn't that a bit weird? Who would post non-working code there?

Also, did you try my suggestion?

I have no clue, I tried pasting in what you did, but I got an error: : In function 'void binary(int)':
: error: expected unqualified-id before 'int'
for(int i = 0, int m = n; i < 4; i++) {
^~~
:47:18: error: expected ';' before 'int'
:47:34: error: expected ')' before ';' token
for(int i = 0, int m = n; i < 4; i++) {
^
error: 'i' was not declared in this scope
for(int i = 0, int m = n; i < 4; i++) {
^

exit status 1

Compilation error: expected unqualified-id before 'int'

Sorry, its

void binary(int n) {
   Serial.println(binaryNum[1]);
   Serial.println(binaryNum[2]);
     Serial.println(binaryNum[3]);
       Serial.println(binaryNum[4]);
  for(int i = 0; int m = n; i < 4; i++) {
    binaryNum[i] = m % 2;
    m /= 2;
  }
}

Still errored

with

I was not declared and I expected a ) before ; in line 47

Sorry. This one compiles

void binary(int n) {
  Serial.println(binaryNum[1]);
  Serial.println(binaryNum[2]);
  Serial.println(binaryNum[3]);
  Serial.println(binaryNum[4]);
  for (int i = 0, m = n; i < 4; i++) {
    binaryNum[i] = m % 2;
    m /= 2;
  }
}

It compiled, but the exact same issue is happening, the lights are not functioning properly

What is in 'binaryNum[0]'? Are you indexing from 0 or 1?

I have no clue, The whole thing is so complex for a simulator. Whoever made this was complicating crap

At this point, it might be better to re-write it from scratch lol
but idk how

It's buggy. 'binaryNum[4]' is the fifth element in a four element array. Some random memory is getting stomped on.

Any way to fix it?

That starts from a clear specification of what it is and what it does.

It simulates a 4 floor elevator with 8 leds. 4 for buttons and 4 for indicator lights. It also takes multiple calls at once

I don't see any LED code.

What's the difference between a LED and an indicator light?

It's because of the way it was implemented The Button LEDs are for when you push the button (Ran Out of replies)