Help - Saving variable from previous loop

Hello, I'm new to arduino and this is my first project. It's a very simple and basic, I just need for each combination of inputs to produce a ceartin combination of outputs. In one of the combination I need to know what was the previous input, and this is the part where my code is failling. there is my code:(the important part is in the last line of the code)

int inPin2 = 2;
int inPin3 = 3;   // the number of the input pin
int outPin4 = 4;
int outPin5 = 5; 
int outPin6 = 6; // the number of the output pin
int index=0;
int state2 = HIGH;
int state3 = HIGH; // the current state of the output pin
int reading2;
int reading3;// the current reading from the input pin
int previous2 = LOW;
int previous3 = LOW;// the previous reading from the input pin


long time = 0;        
long debounce = 200;  

void setup()
{
  pinMode(inPin2, INPUT);
    pinMode(inPin3, INPUT);
  pinMode(outPin4, OUTPUT);
  pinMode(outPin5, OUTPUT);
  pinMode(outPin6, OUTPUT);
  
}

void loop()
{
  reading2 = digitalRead(inPin2);
  reading3 = digitalRead(inPin3);

      if(reading2==HIGH){
      if(reading3==HIGH){
        if(previous2==HIGH){index=1;}
        else{(index=5);}
    }
      else{index=2;}
      }
      else{
  if(reading3==HIGH){index=3;}
  else{index=4;}
      }
  
  switch (index){
    //occasion1
case 1:
  time = millis();
digitalWrite(outPin4,HIGH);
digitalWrite(outPin5,LOW);
  
  digitalWrite(outPin6,LOW);
   break;

//occasion 2
case 2:
  time = millis();
digitalWrite(outPin4,HIGH);
digitalWrite(outPin5,LOW);
digitalWrite(outPin6,LOW);
 break;  

  //occasion 3
case 3:
  time = millis();
digitalWrite(outPin4,LOW);
digitalWrite(outPin5,HIGH);
digitalWrite(outPin6,LOW);
   break;
 //occasion 4
case 4:
  time = millis();
digitalWrite(outPin4,LOW);
digitalWrite(outPin5,LOW);
digitalWrite(outPin6,HIGH);
   break;  
    //occasion 5
    case 5:
  time = millis();
digitalWrite(outPin4,LOW);
digitalWrite(outPin5,HIGH);
digitalWrite(outPin6,LOW);
  }
//the part that not working
previous2 = reading2;
}

What the program does is assign to previous the current condition of the input-pin("reading2") and not the previous one and I dont understand why. And if anyone can also explain me what "time=millis();" does. I copied this from some other program and it solved other problem I had which was that the bored produced noises.

Thank you!

First of all, you should change this code because you could just declare the input pins like this:

int inputpins[2] = {2,3};
int outputpins[3] = {4,5,6};

/*
Now, whenever you want to adress or read from any of the pins, you can do:

digitalRead(inputpins[0]);  // if you remember the 0th element is pin 2, array addressing starts from 0
OR
digitalWrite(outputpins[0],HIGH]; // pin 4 is the 0th element of outputpins

Of course before you do that you need to pinMode them : -

for (int pins = 0;pins == 1;pins++) {
       pinMode(inputpins[pins],INPUT);
}

for (int pins2 = 0;pins2 == 2;pins++) {
        pinMode(outputpins[pins2],OUTPUT
}

This makes your code cleaner and more readable.

*/
time = millis();

millis() returns the number of milliseconds since the Arduino has been ON or RESET. micros() returns microseconds of the same.

Moreover you can have the pins for the buttons as interrupts, so that the program does not miss a single press:

attachInterrupt(0,whateverfunctionyouwanttohandlethepin2button(),RISING);
attachInterrupt(1,whateverfunctionyouwanttohandlethepin3button(),RISING);

//However, this might mess up millis() and the funtion must not have a delay() in it.
previous2 = reading2;

I think that must be the other way around. Check that.

Hope that helped!

time = millis();is doing nothing for you as you don't use the value assigned to the time variable anywhere.

Don't worry about changing how you assign the input pins yet, or using interrupts. Get the basic code working first then tidy it up.

What does the program do now that makes you say that it is not working ? What should it do ?

Try printing the values of previous2 and reading2 just after you read the state of inPin2 at the start of the loop() function. Are the values what you would expect ? How are the inputs to the pins wired ? Have you got any resistors attached to ensure that the pins are in a known state before being activated ? What have you got attached that makes the input pins go HIGH ?