// R2 = (R1 * VA0) / (5 - VA0)

const int analogPin = A0;
const float V_IN = 5.0;

const double R1 = 2000;
const double R2 = 2000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  
  // Read the value from A0
  unsigned int ADC_A0 = analogRead(analogPin);

  // Calculate V_A0 from ADC_A0
  double V_A0 = ADC_A0 * (V_IN / 1023.0);

  // Calculate R2 from V_A0 and R1;
  double R2_calc = (R1 * V_A0) / (V_IN - V_A0);

  // Print ADC_A0, V_A0 and R2 to the serial monitor on a single line
  Serial.print("ADC_A0: ");
  Serial.print(ADC_A0);
  Serial.print(", V_A0: ");
  Serial.print(V_A0);
  Serial.print(", R2: ");
  Serial.print(R2_calc);
  Serial.println("");

  // Wait 0.5 seconds
  delay(500);
}

/**
1M: 1021000.87  | e: 0.02100087 | 2.1   %
100k: 105684.31 | e: 0.0568431  | 5.68  %
10k: 10178.57   | e: 0.017857   | 1.78  %
5.1k: 5153.85   | e: 0.03077    | 3.07  %
2k: 2019.65     | e: 0.0095     | 0.95  %
1k: 1022.16     | e: 0.02216    | 2.22  %
330: 330.30     | e: 0.0090909  | 0.9   %
220: 219.09     | e: 0.004136   | 0.41  %
100: 98.46      | e: 0.0154     | 1.54  %
10: 7.85        | e: 0.215      | 21.5  %
*/