coarse / fine potmeter function

needed a 16 bit fine control in an experiment and I realized it with two potmeters, one for coarse and one for fine control

After some tweaking I got this more generic function

uint16_t analogR(uint8_t coarsePin, uint8_t finePin, uint8_t bits)   // bits = bits per potmeter 1..8, so the result will be twice that nr of bits.
{
  if (bits > 8) return 0;  
  uint8_t shift = 10-bits;

  // analogRead(coarsePin); // opt. to remove noise by double read 
  uint16_t value = (analogRead(coarsePin) >> shift) << bits;

  // analogRead(finePin);
  return value & (analogRead(finePin) >> shift);
}

I restricted the number of bits per pot to 8 to minimize noise fluctuations.

thoughts for improvement:

  • three pots? four?
  • why restrict both pots to same # bits?
  • how to do odd # bits? e.g. 15 bits so I could use the sign bit for errors.

just to share