// Physically limited range encoder mapping simulation
// This maps the most current observation to 0-100 in the observed range
// https://wokwi.com/projects/393017345335968769
// for https://forum.arduino.cc/t/rotary-encoder-with-windows-game-device-calibration-tool/1238392/5
const byte PotPin = A0; // Attach to pot wiper
int biasValue; // powerup setting
void setup() {
// put your setup code here, to run once:
pinMode(PotPin, INPUT); // Attach to pot wiper;
Serial.begin(115200);
biasValue = analogRead(PotPin);
Serial.println("move the pot to a random position and restart the arduino.");
}
int maxObserved = -10000;
int minObserved = 10000;
int lastObserved = -10000;
void loop() {
// put your main code here, to run repeatedly:
int inputVal = analogRead(PotPin) - biasValue;
if (inputVal != lastObserved) {
if (inputVal > maxObserved) maxObserved = inputVal;
if (inputVal < minObserved) minObserved = inputVal;
int mapVal0 = map(inputVal, 0, 1024, 0, 100);
int mapVal = map(inputVal, minObserved, maxObserved, 0, 100);
// for a 100-point, 0-99 result try this:
// int mapVal = map(inputVal, minObserved, maxObserved+1, 0, 100);
Serial.print("min:");
Serial.print(minObserved);
Serial.print(" obs:");
Serial.print(inputVal);
Serial.print(" max:");
Serial.print(maxObserved);
Serial.print(" map0:");
Serial.print(mapVal0);
Serial.print(" map:");
Serial.print(mapVal);
Serial.println();
lastObserved = inputVal;
}
}