Resistive divider inconsistent results?

I am using a 100k and a 700k resistor to make a resistive divider to be able to measure up to 40 volts on the analog input, but when i measure anything then the voltage i am getting is fluctuating by like +-0.5 volts? i measured the resistors and the are exactly the right value and i dont know what else can be the problem.

here is the full code, its still in progress so some of the parts arent used

#include <Wire.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

unsigned long previousMillis;
unsigned long millisPassed;

float capacity;
float current;
float mA;
float voltage;
float low_volt;
float volt;

int button1;
int buttonPin1=5;
int button2;
int buttonPin2=6;
int button3;
int buttonPin3=7;
int button4;
int buttonPin4=8;
int buzzerpin=2;
int freq=2000;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
pinMode(A3, INPUT);
pinMode(A2, INPUT);
pinMode(11, OUTPUT);
pinMode(2, OUTPUT);
pinMode(button1, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(9600);
}

void loop() {
  button1 = digitalRead(buttonPin1);
  button2 = digitalRead(buttonPin2);
  button3 = digitalRead(buttonPin3);
  button4 = digitalRead(buttonPin4); 
  if(button1!=0){
low_volt = low_volt+0.1;
  }
  if(button2!=0){
low_volt = low_volt-0.1;
  }
  if(button3!=0){
low_volt = low_volt+1;
  }
  if(button4!=0){
low_volt = low_volt-1;
  }
  if(low_volt>volt){
    digitalWrite(11,LOW);
     tone(buzzerpin,freq);
    delay(100);
    noTone(buzzerpin);
    delay(100);
    tone(buzzerpin,freq);
    delay(100);
    noTone(buzzerpin);
    delay(100);
    tone(buzzerpin,freq);
    delay(100);
    noTone(buzzerpin);
    delay(5000);
    }
  int v_sense = analogRead(A3);
  float voltage = map(v_sense,0,1023,0,4720); //4720 is the actual voltage on the 5v pin
  volt = voltage/125; //devide by 125 because i need to devide by 1000 to get value in volts then multiply by 8 to get back to real value because of the devider
  int c_sense = analogRead(A2);
  float current = map(c_sense,0,1023,0,4720);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.print("Voltage: ");
  display.print(voltage/125);
  display.println(" V");
  display.print("Current: ");
  display.print(current/1000);
  display.println(" A");
  display.print("Power:   ");
  display.print((voltage/125)*(current/1000));
  display.println(" W");
  display.print("Capacity:");
  display.print(capacity);
  display.println(" mAh");
  display.print("Low volt:");
  display.print(low_volt);
  display.print(" V");
  display.display();
  Serial.println(low_volt);
  Serial.println(volt);
  millisPassed = millis() - previousMillis;
  mA = current * 1000.0 ;
  capacity = capacity + mA * (millisPassed / 3600000000.0);
  previousMillis = millis();  
  delay(250);
}

It would behoove you to post a wiring diagram. How do you know that the 40V signal is not fluctuating?

i tested it with 5 volts first and measured it with my multimeter I even tried to add a capacitor but it didnt solve the issue since the voltage wasnt fluctuating to begin with, the signal was 4.7 volts and the arduino was measuring somewhere between 4.4 and 5.2 volts

here is the diagram

Have you tried a non-electrolytic type capacitor ... like ceramic 0.1µF to 1µF?

Really, the input impedance is too high (should be <= 10K), but using 70K/10K with a 0.1µF cap from the analog input to ground should work much better than what you have.

Can you also please provide a link to the schematic/datasheet of your Arduino? Some boards have noisy power circuits. If the supply voltage has a lot of noise you will see that noise in the results of your ADC.

I even tried to add a capacitor

The capacitor goes between the analog input and ground. Is that where you put it?

With those high value resistors noise can be an issue but a capacitor should help a lot.

i measured the resistors and the are exactly the right value and i dont know what else can be the problem.

The wrong resistor values (or regular tolerance) will cause an error but it won't cause noise/instability.

The ADC reference (Vcc by default) will introduce noise/instability if it's not "solid". The optional 1.1V reference is more stable but it also has a tolerance so you might have to calibrate for accuracy. And of course, you'd also have to change your voltage divider ratio.

Your meter likely has some filtering/smoothing.

thank you to everyone, actually it most of the fluctuating was gone when i changed the values to 70k and 10k and all of it was gone when i connected a 1000uF capacitor on the power so i guess it wasnt smooth enough, the 0.1uF ceramic capacitor didnt seem to change much. i wouldnt have thought that the resistors would be the issue

szevlin:
thank you to everyone, actually it most of the fluctuating was gone when i changed the values to 70k and 10k and all of it was gone when i connected a 1000uF capacitor on the power so i guess it wasnt smooth enough, the 0.1uF ceramic capacitor didnt seem to change much. i wouldnt have thought that the resistors would be the issue

From the ATmega data sheet:
"The ADC is optimized for analog signals with an output impedance of approximately 10 k [ohms] or less."

Bad practice to compare a voltage to the potentially unstable power supply of the Nano (default Aref).

I would pick a divider a ratio of about 1:40, say 10k:390k, and switch to the more stable 1.1volt Aref in setup().

analogReference (INTERNAL);

Then in loop()

float voltage = analogRead(A3) * 0.04195; // calibrate by changing the last digit(s)

Leo..