[Solved]weird thing happen: analogRead problem

I got A1 connected to a battery, and have a LCD print out the voltage, but the voltage keep going up and down, down and up, from 4.2 - 3.4 -4.2 - 3.4, etc. why? this is totally not comprehensible by myself.

here is the part of coding for reading a voltage:

  int batteryVolt = analogRead(A1); 
  battery = batteryVolt*(5.0/1024.0)*0.9;  

  nowVolt=battery;

here are all the code:

#include <Wire.h> 
#include "ClickButton.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address/ pins


ClickButton ba(9, LOW, CLICKBTN_PULLUP);
ClickButton bb(8, LOW, CLICKBTN_PULLUP);

int sec=0;
int Min=0;
int hr=0;
int Switch=1;
int pwm=0;
int Pwm=255;
float shunt=0;
int mah=0;
int ncc=0;
float battery=0;

int realMa=0;

float firstVolt=0.0;
float nowVolt=0.0;

unsigned long shuntTotal=0;
unsigned long nowtimer=0;
unsigned long pretimer=0;

unsigned long nptimer=0;
unsigned long pptimer=0;
//==========================

void setup()
{  
  lcd.begin(16,2);               // initialize the lcd   
  ba.multiclickTime = 50;
  bb.multiclickTime = 50;    
  pinMode(7, OUTPUT);
  pinMode(A0, INPUT);  //Shunt
  pinMode(A1, INPUT);  //battery
}

//=====================
void loop()
{
  ba.Update();
  bb.Update();  

  pwm=constrain(pwm, 0, 9); 
  Pwm=map(pwm, 0,9, 255,0); 

  nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    sec++;
    shuntTotal+=realMa;
    pretimer=nowtimer;    
  }



  if(sec==60){ 
    Min++; 
    sec=0; 
  }
  if(Min==60){ 
    hr++; 
    Min=0; 
  }  


  //=================================

  char buffer[8];
  sprintf(buffer, "%01d:%02d:%02d", hr, Min, sec);
  lcd.setCursor (0,0); 
  lcd.print (buffer);   

  lcd.setCursor (8,0); 
  lcd.print (firstVolt);     

  lcd.setCursor (11,0); 
  lcd.print (">"); 


  lcd.setCursor (15,0); 
  lcd.print ("V");       

  nptimer=millis();
  if(nptimer-pptimer>=300) {

    char bufferx[17];
    sprintf(bufferx, "%04dC %04dmA %01d", mah,realMa, pwm);
    lcd.setCursor (2,1); 
    lcd.print (bufferx);

    lcd.setCursor (12,0); 
    lcd.print (nowVolt);   

    pptimer=nptimer;    

  }



  if(Switch==0){
    lcd.setCursor (0,1); 
    lcd.print ("P:"); 
  }

  if(Switch==1){
    lcd.setCursor (0,1); 
    lcd.print ("S:"); 
  } 

  //================================  

  int shuntVolt= analogRead(A0);   
  shunt = shuntVolt*5.0/1024.0*1000.0*0.89; 
  mah=shuntTotal/3600; 

  int batteryVolt = analogRead(A1); 
  battery = batteryVolt*(5.0/1024.0)*0.9;  

  nowVolt=battery;  

  if(shunt>50) {
    realMa=shunt*pwm/9.0; 

  }

  if(pwm==0){
    realMa=0;  
    digitalWrite(7, LOW);
  }
  if (shunt<50)ncc++;
  if(ncc>20){
    realMa=0; 
    ncc=0; 
    digitalWrite(7, LOW);
  }    


  if(realMa>50 && firstVolt==0.0) firstVolt=battery;

  if(nowVolt < (firstVolt*0.7) && realMa<50) Switch=0;

  if(realMa>50) digitalWrite(7, HIGH);




  //================================    

  if(Switch==1) analogWrite(3, Pwm);
  else {
    analogWrite(3, 255);
    digitalWrite(7, LOW);   
    realMa=0; 
  }

  //================================  

  if(ba.clicks==1) pwm++;
  if(bb.clicks==1) pwm--;  

  if(ba.clicks==-1) Switch=!Switch;
  if(bb.clicks==-1) {
    sec=0;
    Min=0;
    hr=0;
    shuntTotal=0;
    mah=0;
    Switch=0;

  }

}

from 4.2 - 3.4 -4.2 - 3.4, etc. why?

Are you saying you're getting negative results?

AWOL:

from 4.2 - 3.4 -4.2 - 3.4, etc. why?

Are you saying you're getting negative results?

No, all positive, from 4.2 to 3.4 to 4.2 to 3.4. No other number but these two. And the reading is fine before I increase pwm. And first volt reading sometimes is incorrect too.

battery = batteryVolt*(5.0/1024.0)*0.9;

Why the 0.9?
You haven't got resistors in there have you?
If so what value?

There in no need to set a pin as an input of you are using analogue read.

How is it wired? You might be getting ground bounce.

Grumpy_Mike:

battery = batteryVolt*(5.0/1024.0)*0.9;

Why the 0.9?
You haven't got resistors in there have you?
If so what value?

There in no need to set a pin as an input of you are using analogue read.

How is it wired? You might be getting ground bounce.

0.9 to make the reading accurate, because my 5 volt pin is less than 5 volt. I know, analog input doesn't need setup. I put them there to see if it could solve the problem.

How is it wired? You might be getting ground bounce.

Schematic and photo please.

For debugging just print the actual values returned by analogRead().

...R

Robin2:
For debugging just print the actual values returned by analogRead().

...R

actually, the lcd is printing the actual values returned.

Grumpy_Mike:

How is it wired? You might be getting ground bounce.

Schematic and photo please.

thank you for remaining me. I think the problem is something you mentioned: ground bounce; I am switching the battery using PWM very fast, and the voltage got ground bounce very fast, thus I got 3.4 to 4.2 to 3.4 to 4.2 reading. it makes sense now.