Debounce Logic? - Combo Lock project

I'm working on a small project with a combination lock. I started a few days ago with a post under project guidance but my problem now seems to be in my lack of programming experience. I have a rotary thumbwheel switch with 12 positions wired to a bunch of resistors read in at an analog pin. I wrote a sketch that looks for the switch to change position and when it does print the output. Like so:

int val = 0;
int old_val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = (analogRead(0) / 90);

 if ((val == (old_val - 1)) || (val == (old_val +1)) || (val == old_val)) {
   
   if ((old_val != val)) {
     old_val = val;
     Serial.println(val);
   }
   
 }
 delay(100);
}

The problem is this produces inconsistent results if the switch is moved too slowly. I realize the switch I'm using is not ideal. Hopefully there is a software fix before I abandon this method and go for some thing like a CD4021B shift register. I need to add debounce logic, I think. A piece of code that waits to make sure the switch has stopped moving before printing the result. Tips please!

  val = (analogRead(0) / 90);
 if ((val == (old_val - 1)) || (val == (old_val +1)) || (val == old_val)) {
   
   if ((old_val != val)) {

Can I have some of what you are smoking?

The output from analogRead is a value between 0 and 1023. You divide this by 90, to give a range from 0 to 11. This makes no sense, really.

If you have a digital switch connected to an analog pin, there must be 12 different valued resistors between the pin and each of the 12 pins on the switch. It is unlikely that the resistors give a perfect distribution across the scale. You need an array of low values and an array of high values that bracket the nominal value of the analog pin reading in each of the 12 positions.

Get rid of the if stuff, and just print the value read from the analog pin, every 10 seconds. Turn the switch all the way several times. Determine a range of values for each position. Define band around the average value for each position.

Then, to determine the switch value, see if the analog pin reading falls between the lower value for position n and the upper value for position n. If so, the value of interest is n.

Can I have some of what you are smoking?

No. You cannot. ;)

You're mostly right about my setup, it's actually 11 1k resistors similar to this setup: http://srmonk.blogspot.com/2011/09/arduino-solar-radio.html

It mostly works, and if i wasn't being so picky I'd be happy with it. I can read the correct position while the switch is resting. 90 is the average value for each position, so 90, 180, 270...and so on. I'm probably going to try it your way though.

Do you think it would be worth ditching this setup and using something like a static shift register?

I bet when you twist the knob, your reading occasionally goes crazy. I suggest a digital rotary encoder instead. It will also generally have more detent and can make infinite number of turns like a pad lock.