Hello,
I recently purchased the Rotary Switch Potentiometer kit with board and 10 position switch. I followed instructions here: https://learn.sparkfun.com/tutorials/ro … okup-guide. I used 9 total 10k resistors.
I simplified the provided code to just output which slot is selected to the serial monitor. My code is below. However, I am getting some odd results. When I turn the knob to each position, I’m getting inconsistent results:
Position 1: val = 0
Position 2: val = 1
Position 3: val = 3
Position 4: val = 4
Position 5: val = 6
Position 6: val = 8
Position 7: val = 9
Position 8: val = 9
Position 9: val = 9
Position 10: val = 9
I don’t see any shorts. Any guesses what’s happening?
/* Test rotary switch
// modified from https://github.com/sparkfun/Rotary_Switch_Potentiometer
Demonstrates using the Rotary Switch Potentiometer breakout board
with a microcontroller, to build a 10 position selector switch.
The Rotary Switch Potentiometer is a breakout board that adds 9 resistors to a
10 position rotary switch, to make a custom-taper, stepped potentiometer. This
example uses 9 10KOhm resistors, and connects the rotary switch potentiometer
to an analog input.
The Rotary Switch Potentiometer board was populated with 10K resistors in
every position. It was connected to the RedBoard as follows:
RedBoard pin : Rotary Switch Potentiometer Pin
----------------------------------------------
GND : CCW
A3 aka 17 : W
5V : CW
*/
#define BAUDRATE 115200
#define ROTARY_PIN 3 //A3, analog 3, aka pin 17 on Teensy 4.0
void setup() {
Serial.begin(BAUDRATE);
Serial.println("Begin Test Rotary Switch");
}
void loop() {
uint16_t input;
uint16_t val;
// read the ADC
input = analogRead(ROTARY_PIN);
// Translate ADC value from 0-1023 to 0-9.
// This implements the proportion
// input/1023 = val/9.
// One "step" of the pot is about 113 ADC counts.
// We're adding 65 (1/2 of 113) to the input value, so that the
// input is in the middle of the window, rather than right at the edge, so values
// are stable and solid.
val = (input+56)*9/1023;
Serial.println(val);
}/code]