// Converts the position of a 10k lin(B) potentiometer to 0-100%
// Potentiometer connected to A0, 5 volts and ground

int rawValue;
int oldValue;

byte potPercentage;
byte oldPercentage;

float volts;
int ohms;

void setup() {
  // Initialize the serial port
  Serial.begin(9600);
}

void loop() {
  // Read input twice
  rawValue = analogRead(A0);

  // Ignore bad hop-on region of a pot by removing 8 values at both extremes
  rawValue = constrain(rawValue, 8, 1015);

  // Add some deadband
  if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
    oldValue = rawValue;

    // Convert to percentage
    potPercentage = map(oldValue, 8, 1008, 0, 100);

    // Only print if %value changes
    if (oldPercentage != potPercentage) {
      Serial.print("Potentiometer is: ");
      Serial.print(potPercentage);
      Serial.print("% ");
      oldPercentage = potPercentage;

      // Convert % to ohms, assuming the potentiometer is 10k
      ohms = map(potPercentage, 0, 100, 0, 10000);

      // Converts 10-bit analog value to volts
      volts = mapFloat(rawValue, 0, 1023, 0.0, 5.0);

      // Print the values ​​on the serial monitor
      Serial.print("(");
      Serial.print(ohms);
      Serial.print(" ohms) ");
      Serial.print("10-bit analog value: ");
      Serial.print(rawValue);
      Serial.print(" => ");
      Serial.print(volts);
      Serial.println(" volts");
    }
  }
}

float mapFloat(float x, int in_min, int in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}