Read Digital input only once, despite being the loop.

hello, am making a small elevator project where the micro-controller should read combination of digital input pins to know where the elevator is and if any button has been pressed. The digital information are provided from an external circuit using an 74ls148 encoder and it works fine when i tested it seperately. when connected to the arduino uno, the micro controller is able to read the data from the encoder, but when the encoded data is changed and the micro controller reads the input pins again in the loop, it get the same previous input data, even if i disconnect the encoder, it read same previous data which is not true. How can i make the arduino uno read the new data on the input pins when the loop starts again?
Below is the code for the main loop i am using.

 void loop(){
   
    digitalWrite(A5, LOW);
    digitalWrite(A4, HIGH);
    REINPUT:
    
  int ThisLevel=LReader(); //LReader reads pins 2,3,8 and return and integer
  int Selection=SReader(); //RReader reads pins 5,6,7 and return and integer
  int SecondSelect;
  if (ThisLevel==6){
    Serial.println("Elevator not in position");
    digitalWrite(A5, HIGH);
    
  // tone(9,2000,1000);
    delay(1500);
  }
  else if(Selection==6){
    goto REINPUT;
  }
  else{
   SayActivated();
    int FirstSelect=SReader();
     int tempselect;
    if(FirstSelect==4 || FirstSelect==5){
      if(FirstSelect==5){
        Serial.println("Emergency button activated");
        digitalWrite(A5,HIGH);
        return;
      }
     Door();
     goto REINPUT;
    }
    else if(FirstSelect=Selection){
      Door();
      goto REINPUT;
    }
    else{
        SayLevelSelected(FirstSelect);

      for (int counter=0;counter<=199;counter++){
        SecondSelect=SReader();
        delay(500);
        if(SecondSelect==FirstSelect){
          SayCancelled();
          delay(500);
          goto REINPUT;
        }
        
          else {tempselect=FirstSelect;
          }
      }
      int Select=FinalSelect(tempselect);
      int w=Compare(Select,ThisLevel);
      MotorExecute(w);
      Deactivate();
    }
  }}

There are no digitalReads in the code you posted.
Why don't you post the entire thing?

bhavish009:
hello, am making a small elevator project where the micro-controller should read combination of digital input pins to know where the elevator is and if any button has been pressed. The digital information are provided from an external circuit using an 74ls148 encoder and it works fine when i tested it seperately. when connected to the arduino uno, the micro controller is able to read the data from the encoder, but when the encoded data is changed and the micro controller reads the input pins again in the loop, it get the same previous input data, even if i disconnect the encoder, it read same previous data which is not true. How can i make the arduino uno read the new data on the input pins when the loop starts again?
Below is the code for the main loop i am using.

 void loop(){

digitalWrite(A5, LOW);
    digitalWrite(A4, HIGH);
    REINPUT:

You said it reads  same data even when disconnected so how does it changes?
Put this in it  own loop to test it before you descramble your spaghetti  code.

do{
  int ThisLevel=LReader(); //LReader reads pins 2,3,8 and return and integer
  int Selection=SReader(); //RReader reads pins 5,6,7 and return and integer
Serial.print(ThisLevel);
Serial.print(Selection);
delay(2000);
}while(1);

After that remove the do / while and  try this:

Selection = 6;

and some other Serial  to track you code - does it really stays in Selection == 6 "loop" back to the label?

int SecondSelect;
  if (ThisLevel==6){
    Serial.println("Elevator not in position");
    digitalWrite(A5, HIGH);
   
  // tone(9,2000,1000);
    delay(1500);
  }
  else if(Selection==6){
    goto REINPUT;          // test this first setting Selection = 6
  }
  else{
  SayActivated();
    int FirstSelect=SReader();
    int tempselect;
    if(FirstSelect==4 || FirstSelect==5){
      if(FirstSelect==5){
        Serial.println("Emergency button activated");
        digitalWrite(A5,HIGH);
        return;
      }
    Door();
    goto REINPUT;
    }
    else if(FirstSelect=Selection){
      Door();
      goto REINPUT;
    }
    else{
        SayLevelSelected(FirstSelect);

for (int counter=0;counter<=199;counter++){
        SecondSelect=SReader();
        delay(500);
        if(SecondSelect==FirstSelect){
          SayCancelled();
          delay(500);
          goto REINPUT;
        }
       
          else {tempselect=FirstSelect;
          }
      }
      int Select=FinalSelect(tempselect);
      int w=Compare(Select,ThisLevel);
      MotorExecute(w);
      Deactivate();
    }
  }}

Sorry i did not put the complete code.
i am posting the part where the error is occuring below. The arduino reads the input pins only once, and keeps reading the same input when the loop restarts and the combination on the input pins have changed.

  int L0=digitalRead(2);
  int L1=digitalRead(3);
  int L2=digitalRead(8);
  int S0=digitalRead(5);
  int S1=digitalRead(6);
  int S2=digitalRead(7);
  
  void setup(){
 Serial.begin(9600);
  
  pinMode(2, INPUT); //L0
  pinMode(3, INPUT); //L1
  pinMode(5, INPUT); //S0
  pinMode(6, INPUT); //S1
  pinMode(7, INPUT); //S2
  pinMode(8, INPUT); //L2

 Serial.println("Setup complete");
  // Serial.println(F("using ls148 directly, SReader and LReader are complemented"));
}



int SReader(){
  Serial.println(F("SReader:"));
 
  if(S2==LOW && S1==LOW && S0==LOW){
    return 7; 
  }
  else if(S2==LOW && S1==LOW && S0==HIGH){
    return 6; 
  }
  else if(S2==LOW && S1==HIGH && S0==LOW){
    return 5; 
  }
  else if(S2==LOW && S1==HIGH && S0==HIGH){
    return 4; 
  }
  else if(S2==HIGH && S1==LOW && S0==LOW){
    return 3; 
  }
  else if(S2==HIGH && S1==LOW && S0==HIGH){
    return 2; 
  }
  else if(S2==HIGH && S1==HIGH && S0==LOW){
    return 1; 
  }
  else if(S2==HIGH && S1==HIGH && S0==HIGH){
    return 0; 
  }
}



int LReader(){
  Serial.println(F("LReader:"));
  if(L2==LOW && L1==LOW && L0==LOW){
    return 7; 
  }
  else if(L2==LOW && L1==LOW && L0==HIGH){
    return 6; 
  }
  else if(L2==LOW && L1==HIGH && L0==LOW){
    return 5; 
  }
  else if(L2==LOW && L1==HIGH && L0==HIGH){
    return 4; 
  }
  else if(L2==HIGH && L1==LOW && L0==LOW){
    return 3; 
  }
  else if(L2==HIGH && L1==LOW && L0==HIGH){
    return 2; 
  }
  else if(L2==HIGH && L1==HIGH && L0==LOW){
    return 1; 
  }
  else if(L2==HIGH && L1==HIGH && L0==HIGH){
    return 0;
  }
}

void loop(){
  int ThisLevel=LReader();
  Serial.println(ThisLevel);
  int Selection=SReader();
  Serial.println(Selection);
  Serial.println();
  delay(1000);
}

below the the results i get on the serial monitor and it does on like this.

using ls148 directly, SReader and LReader are complemented
LReader:
0
SReader:
7

LReader:
0
SReader:
7

Your digitalReads are outside of the loop. They are read only once when the chip powers up, not every time loop iterates, so obviously you'll get the same value you store in your ints.

Thanks a lot, it seems to work now. i did not thought that the digitalRead should be in the loop else it would have been read only once. Thanks again