Rotary Encoder with Addressable LED Strip

What I have been trying to do is use the Rotary Encoder to change the brightness and when you click it, it would change to a different pattern(which I have not set up and have no clue how to). I would also like to implement a way to limit the Encoder between 0 and 255.

Main thing I want help with is why when I have the rainbow(20); in the loop, it breaks the encoder

Each time I implement the rainbow(20); into the loop, the encoder no longer works for some reason, but the LEDs show correctly. I have everything already wired up correctly and tested.
(Commented the button part out and other patterns I am currently not using.)

Here's the code so far. I hope I am not asking too much. o-o

#include <Adafruit_NeoPixel.h>

#define inputDT 2
#define inputCLK 3
//#define inputButton 4
#define LED_PIN 7
#define LED_COUNT 6

int brightness = 50;
int currentStateCLK;
int previousStateCLK;

String encdir ="";

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGBW + NEO_KHZ800);

void setup() {
  pinMode(inputCLK,INPUT);
  pinMode(inputDT,INPUT);
  Serial.begin(9600);
  previousStateCLK = digitalRead(inputCLK);
  
  strip.begin();
  strip.show();
  strip.setBrightness(brightness);
}

void loop() {
  currentStateCLK = digitalRead(inputCLK);
   if (currentStateCLK != previousStateCLK){ 
     if (digitalRead(inputDT) != currentStateCLK) { 
       brightness --;
       encdir ="CCW";
     } else {
       brightness ++;
       encdir ="CW";
     }
     Serial.print("Direction: ");
     Serial.print(encdir);
     Serial.print(" -- Brightness: ");
     Serial.println(brightness);
   } 
   previousStateCLK = currentStateCLK; 

   rainbow(20);
   //colorWipe(strip.Color(255,   0,   0), 50); // Red
   //colorWipe(strip.Color(  0, 255,   0), 50); // Green
   //colorWipe(strip.Color(  0,   0, 255), 50); // Blue
   //theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
}

void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
    strip.show(); 
    delay(wait);
  }
}

void rainbow(int wait) {
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) {
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show();
    delay(wait);
  }
}

void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;
  for(int a=0; a<30; a++) {
    for(int b=0; b<3; b++) {
      strip.clear();
      for(int c=b; c<strip.numPixels(); c += 3) {
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue));
        strip.setPixelColor(c, color);
      }
      strip.show();
      delay(wait);
      firstPixelHue += 65536 / 90;
    }
  }
}

Thanks to anyone that helps! My main idea for this project is to just ziptie the LEDs onto a bike. :smiley:

Instead of using blocking delay()s, use non-blocking millis() for timing.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

For loops also block. You cannot check the encoder status while locked in a delay or for loop.

I will make an attempt at it... My mind doesn't take coding very well... Like most of the code written isn't mine.

Edit: Yeah... I have no clue whats going on... I skimmed through the three things you sent, and the parts about using a single LED instead of a whole LED strip confuses me even more... I'm like beyond newbie lol.

Edit2: Most of the code I used in it too was from a StrandTest example with the Neopixel library. The encoder code was from some video, which is also the most confusing part in the code for me.

Do make an attempt. If you have trouble, post the code, describe what the code actually does and how that is different from what you want. Then we can try to help.