Rotary Encoder

Hi. I am using a rotary encoder to change the LCD menus.However, the program I am using constantly provides information from the rotary encoders.

Commands such as switching between menus.
But the encoder information is constantly coming and the loop repeats itself constantly. How can I get rid of it? I want the value I read from the encoder to stay constant at 40 and not give any output unless I change it.

I want the value I read from the encoder to stay constant at 40 and not give any output unless I change it.

If you don't touch the encoder, the value(s) read from the encoder won't change, if your code is written correctly.

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int encoderSwitchPin = 4; //push button switch

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

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

  pinMode(encoderPin1, INPUT); 
  pinMode(encoderPin2, INPUT);

  pinMode(encoderSwitchPin, INPUT);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  digitalWrite(encoderSwitchPin, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 
  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);


void loop(){ 
  //Do stuff here
    //button is not being pushed
    //button is being pushed
  delay(1000); //just here to slow down the output, and show it will work  even during a delay

void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

  lastEncoded = encoded; //store this value for next time

Where am I supposed to make changes?

First, you need to get the code to compile. Variable encoderSwitchPin2 is not defined in your program.

Your posted code works as expected with my test encoder. The last position is printed out every second.

If the encoder value is changing without touching the encoder you have some sort of hardware or wiring problem. Are you sure you are the A B outputs are connected to pin 2 and 3?

It's possible that the internal pullups are not strong enough. Try external pullups of 4.7K.

If your possible encoder position values are greater than a byte, you should use code ensure that the value can't change while being read. See nick Gammon's interrupt tutorial at Gammon Forum : Electronics : Microprocessors : Interrupts

Also if multi-byte fields are being updated by an ISR then you may need to disable interrupts so that you get the data "atomically". Otherwise one byte may be updated by the ISR while you are reading the other one.

For example:

noInterrupts ();

long myCounter = isrCounter;  // get value set by ISR
interrupts ();

Temporarily turning off interrupts ensures that isrCounter (a counter set inside an ISR) does not change while we are obtaining its value.