How to stop a rotary encoder reading?

I might be having a senior moment but I swear that there was a Beginners forum here a few weeks ago?

Anyway back to my question which I am struggling to deal with ( maybe another senior moment)
But how do you stop a rotary encoder from reading the position values even while someone maybe turning the shaft?

I have the button of the encoder light up one by one a strip of eight neopixels.
The rotary encoder as I turn the shaft will light up one by one another strip of eighty
neopixels.

My button will detect a long press and lock the button and prevent the strip of eight from changing.

But for the life of me I cannot work out what the line of code is to stop the rotary encoder from changing the eightly led strip?

Post your code.

In code tags.

There isnt any code specifically. You would write that code. All the rotary encoder does is send pulses of zero and five volts to your microcontroller. If you dont program it it do anything useful, it wont.

Pomgonewalkabout:
But for the life of me I cannot work out what the line of code is to stop the rotary encoder from changing the rightly led strip?

if (whatever condition means you should read the encoder) {
     // read the encoder here
}
else {
    // don't read it
}

Here is a example of some code for a Rotary Encoder.
So if I was to press a button how could I stop the encoder from changing the current position value even if the shaft was rotated.

/* Read Quadrature Encoder
  * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
  *
  * Sketch by max wolf / www.meso.net
  * v. 0.1 - very basic functions - mw 20061220
  *
  */  


 int val; 
 int encoder0PinA = 2;
 int encoder0PinB = 3;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;

 void setup() { 
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   pinMode (5, OUTPUT);
   pinMode (6, OUTPUT);
   digitalWrite (5, HIGH);
   digitalWrite (6, LOW );
   Serial.begin (9600);
 } 

 void loop() { 
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
     Serial.print (encoder0Pos);
     Serial.print ("\n");
   } 
   encoder0PinALast = n;
 }
if (!stopped)
{
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
}

Hell I am back again.

My code is in next reply because I have exceeded some 9000 character limit?

I am using the Arduino to control amongst other things an eight led strip of Neopixels (only using five of them for the radio band selection ) and eighty leds set up as five rows of sixteen neopixels for the frequency.

A button on the rotary encoder selects one of the bands and lights up the relevant row.
I then turn the rotary encoder to choose the frequency.

A long press on the button locks up the leds and stops them moving. this position is then reported back to the pi and plays some media.

This bit works but I am struggling really struggling
How after another long button press to get the rotary encoder rotation working again

These two lines

encoder0PinA = false;//LOW;
encoder0PinB = false;//LOW;

After a long button press stop the rotary leds from moving so I thought that if I made them true again after another long button press then the rotary will continue to move the leds as before.

Wrong !

I am learning to code as I go along with this project as as you will no doubt see below!

Cheers

[

 #include <Button.h>        //https://github.com/JChristensen/Button
 #include <Adafruit_NeoPixel.h>
 #include <Servo.h>
 #include <Wire.h>
 #include <Firmata.h>

 int frequencyPIN  = 5;  // neopixel din pin for 80 led frequency
 int FNUMPIXELS = 80; // number of frequency leds
 int bandPin = 6;  //  neopixel din pin for 8 led band
 int BNUMPIXELS = 8;  // number of band selector leds
 int red = 255;
 int green = 255;
 int blue = 255;
 int val;
 int encoder0PinA = 2;
 int encoder0PinB = 4;
 int encoder0Pos = -1; //0
 int encoder0PinALast = LOW;
 int n = LOW;
 Adafruit_NeoPixel bandpixels = Adafruit_NeoPixel(BNUMPIXELS, bandPin, NEO_GRB + NEO_KHZ800);
 Adafruit_NeoPixel frequencypixels = Adafruit_NeoPixel(FNUMPIXELS, frequencyPIN, NEO_GRB + NEO_KHZ800);
 #define BUTTON_PIN 3       
 #define PULLUP true        
 #define INVERT true        
                           
 #define DEBOUNCE_MS 20    

 #define LED_PIN 13         //The standard Arduino "Pin 13" LED.
 #define LONG_PRESS 1000    //We define a "long press" to be 1000 milliseconds.
 #define BLINK_INTERVAL 100 //In the BLINK state, switch the LED every 100 milliseconds.

 Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);    //Declare the button
 int buttonCounter = -1;
 //The list of possible states for the state machine. This state machine has a fixed
 //sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
 //Note that while the user perceives two "modes", i.e. ON/OFF mode and rapid blink mode,
 //two extra states are needed in the state machine to transition between these modes.
 enum {ONOFF, TO_BLINK, BLINK, TO_ONOFF};       
 uint8_t STATE;                   //The current state machine state
 boolean ledState;                //The current LED status
 unsigned long ms;                //The current time from millis()
 unsigned long msLast;            //The last time the LED was switched


 void setup(void)
 {
 pinMode(LED_PIN, OUTPUT);    //Set the LED pin as an output
 frequencypixels.setBrightness(4);
 frequencypixels.begin();
 frequencypixels.clear();
 frequencypixels.show(); 
 
 bandpixels.setBrightness(4);
 bandpixels.begin();
 bandpixels.clear();
 bandpixels.show();
 
 buttonCounter = -1; 
 Serial.begin(115200);
}

void loop(void)
{
   
    ms = millis();               //record the current time
    myBtn.read();                //Read the button
    n = digitalRead(encoder0PinB);
    if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinA) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    if (encoder0Pos<0)encoder0Pos= -1;
    
    if (encoder0Pos>79)encoder0Pos= 79;
    if (encoder0Pos>FNUMPIXELS)encoder0Pos=FNUMPIXELS;
   frequencypixels.clear();
   frequencypixels.setPixelColor(encoder0Pos, frequencypixels.Color(0,150,0)); 
   frequencypixels.show();
   //Serial.println (encoder0Pos);
   //Serial.print ("/");
  // Serial.println (encoder0Pos);
   //Serial.print ("Button Counter = ");
   //Serial.println (buttonCounter);
   //Serial.print ("/");
  }
  encoder0PinALast = n;    
    switch (STATE) {
        
        //This state watches for short and long presses, switches the LED for
        //short presses, and moves to the TO_BLINK state for long presses.
        case ONOFF: 
      bandpixels.setPixelColor(buttonCounter, bandpixels.Color(0,0,0));        
            if (myBtn.wasReleased())
                buttonCounter ++,  Serial.println( buttonCounter);
                bandpixels.setPixelColor(buttonCounter, bandpixels.Color(red,0,0));
      bandpixels.begin();
      bandpixels.show();
                
                if (buttonCounter == -1)
                {
                encoder0Pos == -1;
                 frequencypixels.clear();
                 frequencypixels.show();
                   
               
                }  
                            
                else if (buttonCounter ==0)
               {
              
               if (encoder0Pos < 0) encoder0Pos =0;  
               if (encoder0Pos >14) encoder0Pos =14;
               
               }                
                else if (buttonCounter ==1)
                {
                if (encoder0Pos < 17) encoder0Pos =17;
                if (encoder0Pos >30) encoder0Pos =30; 
                }
                
                else if (buttonCounter ==2)
               {
                if (encoder0Pos <33) encoder0Pos =33;
                if (encoder0Pos >46) encoder0Pos =46;
               }                
                else if (buttonCounter ==3)
                {
                if (encoder0Pos <49) encoder0Pos =49; 
                if (encoder0Pos>62)encoder0Pos = 62;
                }
                else if (buttonCounter ==4)
                {
                if (encoder0Pos <65) encoder0Pos =65
                ; 
                if (encoder0Pos>79)encoder0Pos = 79;
                }
                
                if (buttonCounter >4) 
                {
                 buttonCounter = -1;
                  encoder0Pos = 0; 
                // buttonCounter = 0, bandpixels.clear(),bandpixels.setPixelColor(buttonCounter, bandpixels.Color(0,0,0));
                 bandpixels.clear();
                 bandpixels.show();
                 frequencypixels.clear();
                 frequencypixels.show();  
                 //frequencypixels.clear(); frequencypixels.setPixelColor(encoder0Pos, frequencypixels.Color(0,0,0));
                 //frequencypixels.show();
                }
                
                
                
                
            else if (myBtn.pressedFor(LONG_PRESS))
                  STATE = TO_BLINK,Serial.println( buttonCounter); //buttonCounter = 7, 
            break;
            
        //This is a transition state where we start the fast blink as feedback to the user,
        //but we also need to wait for the user to release the button, i.e. end the
        //long press, before moving to the BLINK state.
        case TO_BLINK:
            if (myBtn.wasReleased())
                
                STATE = BLINK;
            else
                fastBlink();
                //n = false;
                encoder0PinA = false;//LOW;   // These two lines 
                encoder0PinB = false;//LOW;   // stops rotory encoder moving after long press
                Serial.println (" Encoder is false so Frequency = ");
                Serial.println (encoder0Pos);
                //Serial.println( "Hello on");
            break;
            
        //The fast-blink state. Watch for another long press which will cause us to
        //turn the LED off (as feedback to the user) and move to the TO_ONOFF state.
        case BLINK:
            if (myBtn.pressedFor(LONG_PRESS)) {
               //encoder0PinA = true;   // But how do I get it to move
               //encoder0PinB = true;   // again after another long press ?
               //encoder0Pos =encoder0Pos ;
              Serial.println( "Hello Encoder is true so whay are rotary encoder leds not moving again");
              
              STATE = TO_ONOFF;
                digitalWrite(LED_PIN, LOW);
                ledState = false;
            }
            else
                fastBlink();
            break;
            
        //This is a transition state where we just wait for the user to release the button
        //before moving back to the ONOFF state.
        case TO_ONOFF:
            if (myBtn.wasReleased())
                STATE = ONOFF;
               
               //n=true;
               // encoder0PinA = true;    
               // encoder0PinB = true;
              //encoder0PinALast = n; 
             //  frequencypixels.setPixelColor(5, frequencypixels.Color(0,150,0));
             // encoder0Pos =encoder0Pos ;
              // encoder0Pos++;
            break;
    }
}

//Reverse the current LED state. If it's on, turn it off. If it's off, turn it on.
void switchLED()
{
    msLast = ms;                 //record the last switch time
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
}

//Switch the LED on and off every BLINK_INETERVAL milliseconds.
void fastBlink()
{
    if (ms - msLast >= BLINK_INTERVAL)
        switchLED();
       
}

Pomgonewalkabout:
Anyway back to my question which I am struggling to deal with ( maybe another senior moment)
But how do you stop a rotary encoder from reading the position values even while someone maybe turning the shaft?

One way is to have a control signal, or a software variable, such as 'display_hold'. You could make it an integer variable, such as containing 0 or 1.

If you manually set display_hold value to 0, then your program could do something like...

if (display_hold == 0) {

..... put your code to output display values to serial monitor here ...
}

In the above, the idea is to allow values to be printed to serial monitor under general conditions. But when you deliberately change display_hold to '1', then the values will appear to be frozen on the display.