1 Pole 12 Position Rotary Switch Wrapping/Looping Causing Error

raschemmel: I'm pretty sure that's phenolic.

For consumer grade stuff, not military/ aviation.

Same as PCBs.

Same as PCBs.

In some cases, but not normally since phenolic is used for it's thermal properties. I would expect PCBs to be of G10 or similar. Don't know but just don't see any reason to use phenolic for a pcb unless there is a need for heat tolerance. Phenolic is the material you see used in old crt type TVs and oscopes for mounting the terminals that power resistors are soldered to. (and of course for some special switches like rotary).

Now that I have this switch working I had a question about fluctuating analog read values…

Whenever I turn the the switch I want to call an animation function based on the input value. If the input value is higher that the previous saved read value I call next function , if the input value is less than the previous saved read value I call previous function.

Here’s my sketch:

int switchPin = A0;    // select the input pin for the potentiometer
int switchValue = 0;  // variable to store the value coming from the sensor


void setup() {
   // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  Serial.println(analogRead(switchPin));
  Serial.println(switchValue);

if(analogRead(switchPin) != switchValue){
  if(analogRead(switchPin) > (switchValue) ){
    goNextAnimation();
    switchValue = analogRead(switchPin);
  } else if(analogRead(switchPin) < (switchValue)){
    goPreviousAnimation();
    switchValue = analogRead(switchPin);
  }
}

  delay(1000);        // delay in between reads for stability
}

void goNextAnimation() {
  Serial.println("goNextAnimation");
}

void goPreviousAnimation() {
  Serial.println("goPreviousAnimation");
}

My problem is that the analogRead(switchPin) values fluctuate ±2…So this condition (analogRead(switchPin) != switchValue) fires most of the time…

Do I need a pull up resistor between the output of the switch and pin 0? Would that eliminate this fluctuation? If yes, what value resistor should I use.

Is there a another method I should be using to read this values?

Do I need a pull up resistor between the output of the switch and pin 0? Would that eliminate this fluctuation? If yes, what value resistor should I use.

NO.

Is there a another method I should be using to read this values?

YES

You can't write code based on any specific analog value. All code relating to analog values read by an arduino must be written with ranges +/- 50 or greater (with some exceptions) . One count is 4.88 mV. Most sensors yield values that fluctuate at least 50mV. I suggest you revise your code to account for this. Your wasting your time trying to write code that cannot tolerate these deviations.

Here’s my revised sketch to account for fluctuations of ±10. Thanks for your feedback.

This works:

int switchPin = A0;    // select the input pin for the potentiometer
int newSwitchValue = 0;  // variable to store the value coming from the sensor
int oldSwitchValue = 0;  // variable to store the value coming from the sensor

void setup() {
   // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  //Serial.println(analogRead(switchPin));
  newSwitchValue = analogRead(switchPin);

  if(newSwitchValue < (oldSwitchValue-10) || newSwitchValue > (oldSwitchValue+10)){  

    Serial.println(newSwitchValue);
    Serial.println(oldSwitchValue);
    
    if(newSwitchValue > oldSwitchValue ){
      if(newSwitchValue > 1000 && oldSwitchValue == 0){
        goPreviousAnimation();
      } else {
        goNextAnimation();
      }
    } else if(newSwitchValue < oldSwitchValue){
      
      if(newSwitchValue == 0  && oldSwitchValue > 1000){
         goNextAnimation();
      } else {
         goPreviousAnimation();
       }
    }
    
    oldSwitchValue = newSwitchValue;
 }
  
    delay(1000);        // delay in between reads for stability
}

void goNextAnimation() {
  Serial.println("goNextAnimation");
}

void goPreviousAnimation() {
  Serial.println("goPreviousAnimation");
}

… another take (with debounce, value ranges and switch position):

int switchPin = A0;                              // select the input pin for the potentiometer
unsigned short switchValue = 0;                  // variable to store the value coming from the sensor
byte switchPosition = 0;                         // variable to store the rotary switch position
byte switchPositionPrevious = 0;
const long debounceInterval = 100;
unsigned long previousMillis = 0;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  switchValue = analogRead(switchPin);

  if (millis() - previousMillis >= debounceInterval) {
    if (switchValue < 50) {                          // switch position 1
      switchPosition = 1;
      Serial.println("switch position = 1");
    }
    if (switchValue > 50 && switchValue < 140) {     // switch position 2
      switchPosition = 2;
      Serial.println("switch position = 2");
    }
    if (switchValue > 141 && switchValue < 230) {    // switch position 3
      switchPosition = 3;
      Serial.println("switch position = 3");
    }
    if (switchValue > 231 && switchValue < 330) {    // switch position 4
      switchPosition = 4;
      Serial.println("switch position = 4");
    }
    if (switchValue > 331 && switchValue < 420) {    // switch position 5
      switchPosition = 5;
      Serial.println("switch position = 5");
    }
    if (switchValue > 421 && switchValue < 510) {    // switch position 6
      switchPosition = 6;
      Serial.println("switch position = 6");
    }
    if (switchValue > 511 && switchValue < 600) {    // switch position 7
      switchPosition = 7;
      Serial.println("switch position = 7");
    }
    if (switchValue > 601 && switchValue < 700) {    // switch position 8
      switchPosition = 8;
      Serial.println("switch position = 8");
    }
    if (switchValue > 701 && switchValue < 790) {    // switch position 9
      switchPosition = 9;
      Serial.println("switch position = 9");
    }
    if (switchValue > 791 && switchValue < 890) {    // switch position 10
      switchPosition = 10;
      Serial.println("switch position = 10");
    }
    if (switchValue > 891 && switchValue < 970) {    // switch position 11
      switchPosition = 11;
      Serial.println("switch position = 11");
    }
    if (switchValue > 971 && switchValue < 1023) {   // switch position 12
      switchPosition = 12;
      Serial.println("switch position = 12");
    }
    previousMillis = millis();
  }
  if (switchPosition != switchPositionPrevious) {
    (switchPosition > switchPositionPrevious) ? goNextAnimation() : goPreviousAnimation();
    switchPositionPrevious = switchPosition;
  }
}

void goNextAnimation() {
  Serial.println("goNextAnimation");
}

void goPreviousAnimation() {
  Serial.println("goPreviousAnimation");
}

@dlloyd Really appreciate you taking the time to suggest this alternative. Debounce, ranges and positions give me more options for future functionality.

I just updated this part of your code so the next / previous functions get called correctly if you are moving from position 12 to 1 or from position 1 to 12.

if (switchPosition != switchPositionPrevious) {
    if(switchPosition == 1 && switchPositionPrevious == 12){
      goNextAnimation();
    } if (switchPosition == 12 && switchPositionPrevious == 1) {
      goPreviousAnimation();
    } else {
      (switchPosition > switchPositionPrevious) ? goNextAnimation() : goPreviousAnimation();
    }

(switchPosition > switchPositionPrevious) ? goNextAnimation() : goPreviousAnimation();

What does the "?" do ? I looked on the Reference page and couldn't find that operator. Also, why is the ":" there ? What does that do ?

 ? goNextAnimation() : goPreviousAnimation();

Its the conditional or ternary operator, where this:

if(y < 10){ 
   var = 30;
}else{
   var = 40;
}

Can be written as this:

var = (y < 10) ? 30 : 40;

Its handy one line code for selecting between two values.
(expression) ? (true-value) : (false-value)

Huh! Well blow me down. I've never seen that before. Interesting ...

Thanks for the explanation.