Here is my working function:

Voltage divider ratio could be bigger but I had these resistors and it is working.

* correct R1 and R2 value to resistor values you use. For calibration, you can fine-tune these numbers

* you also must define voltagePin with correct pin number

Code:

#define voltagePin 2
unsigned long batteryVoltage(void){
float vout = 0.0;
float vin = 0.0;
float R1 = 21500.0; // !! resistance of R1 !!
float R2 = 9500.0; // !! resistance of R2 !!
int value = 0;
value = analogRead(voltagePin);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
return vin * 1000;
}

Schematic is like this.

12V...R1...VoltagePin...R2...GND

so it means R1 is between 12V and VoltagePin, R2 is between VoltagePin and GND