Reading constant battery voltage and using that value in sensor formula

Hi, there i am working on a project in which my system is connected to 3.7v~4.2V lithium battery, I have to read constant battery voltage irrespective of the current battery voltage and to do that I am using internal voltage reference (1.1V). My issue is that before adding sensor code my program was reading constant voltage irrespective of any battery level but the moment I add sensor code to it, the battery voltage readings fluctuate.

float val;
float voltage;
void setup(){
 Serial.begin(9600);
 pinMode (led,OUTPUT);
 pinMode (A0, INPUT);
 pinMode (A1, INPUT)
}

void loop() {  
 printVolts();
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
  ADMUX |= B11000000;   
  //We read A1 (MUX0)
  ADMUX |= B00000001;       
  // Start AD conversion
  ADCSRA |= B11000000;
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
  val = ADCL | (ADCH << 8);
  val = val * 5.7; //Multiply by the inverse of the divider
  Serial.print("val:     ");
  Serial.print(val);
  



}


 void printVolts()
{
  int sensorValue = analogRead(A0);
  voltage = sensorValue * (val / 1023.00) ; 

  delay(1000);
  Serial.println("voltage:  ");

Serial.println(voltage); 
}

Please let me know what is the issue with the code, the value 5.7 in the code is the voltage divider resistors value R1=1k, R2=4.7K. The reason behind using these resistor is to reduce the battery voltage level below AREF voltage which is 1.1v respectively.

must be read twice, discard the first result

@polish_girl
Creating a multiple accounts in order to bypass the blocking there is a violation of forum rules.

Your question is absolutely meaningless, as you were already pointed out in your previous thread.
You cannot expect the battery voltage to be constant as the battery discharges. It does not depend on how you measure it - with using an external AREF or through an internal one.

AnalogRead() will reset all of the ADC regisers to what the Arduino likes.
Don't OR the register values, set them to what you want with =.

can you elaborate it?

Do: register = Bxxxxxxxx, NOT register |= Bxxxxxxxx

Also, you should set the DIDR0 register.

Hi, i made the changes and now it reads same value for both, it is unable to go above a specific value

float val;
float voltage;
void setup(){
 Serial.begin(9600);
 pinMode (A0, INPUT);
 pinMode (A1, INPUT);
}

void loop() {  
 printVolts();
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
  analogReference( INTERNAL);
  //We read A1 (MUX0)
  ADMUX = 0b00000000;   
  DIDR0 = 0;  
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
  val = ADCL | (ADCH << 8);
  val = val * 5.7; //Multiply by the inverse of the divider
  Serial.print("val:     ");
  Serial.print(val);
  

}


 void printVolts()
{
  float sensorValue = analogRead(A1);
  voltage =  (val / 1023.) * sensorValue; 

delay(1000);
Serial.println("voltage:  ");
Serial.println(voltage); 
}


Reads max value.

It can read less value, if i cover my sensor with hand but it cannot read the actual sensor value.

@polish_girl In order to measure battery voltage you do not need a voltage divider at all.
You wrote that you read a bunch of links on this topic - but did not understand the main idea. It is necessary not to measure the battery voltage relative to the internal reference 1.1v, but vice versa - to measure the internal reference relative to the battery voltage, using the default reference.
The code for this is in hundreds of places on the Internet.