How to Change Encoder Values

Hello,
I am using an Library called NewEncoder to code my rotary encoder. I am trying to have the push button switch between changing the brightness and changing the color. I don't have code for the brightness yet. The problem is that when I try to change the limits of the encoder (STARTPOINT and ENDPOINT) they aren't changing, here is my code

#include <FastLED.h>
#include "Arduino.h"
#include "NewEncoder.h"
#define LED_PIN 10
#define NUM_LEDS 50
#define button 4
CRGB leds[NUM_LEDS];
int16_t currentValue;
int16_t prevEncoderValue;
int ENDPOINT = 0;
int STARTPOINT = 20;
int mode = 0;

NewEncoder encoder(3, 2, ENDPOINT, STARTPOINT, 0, FULL_PULSE);

void BrightChange() {
  switch (mode) {
    case 0: ENDPOINT = 0;
    STARTPOINT = 20; break;
    case 1: ENDPOINT = 1;
    STARTPOINT = 100; break;
    }
  }


void makeStripe() {
  switch (currentValue) {
    case 0: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(0, 0, 0);
        FastLED.show();
    } break;
    case 1: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(0, 255, 0);
        FastLED.show();
    } break;
    case 2: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(0, 0, 255);
        FastLED.show();
    } break;
    case 3: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(215, 80, 200);
        FastLED.show();
    } break;
    case 4: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(35, 240, 210);
        FastLED.show();
    } break;
    case 5: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(255, 255, 255);
        FastLED.show();
    } break;
    case 6: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(255, 110, 40);
        FastLED.show();
    } break;
    case 7: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(105, 210, 80);
        FastLED.show();
    } break;
    case 8: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(89, 90, 200);
        FastLED.show();
    } break;     
    case 9: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(25, 11, 4);
        FastLED.show();
    } break; 
    case 10: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(25, 200, 20);
        FastLED.show();
    } break;            
    case 11: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(225, 8, 209);
        FastLED.show();
    } break;      
    case 12: for (int i=0; i<NUM_LEDS; i++){  leds[i] = CRGB(105, 20, 250);
        FastLED.show();
    } break;       
  }
  FastLED.show();
}
void setup() {
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
  NewEncoder::EncoderState state;

  Serial.begin(115200);
  delay(2000);
  Serial.println("Starting");

  if (!encoder.begin()) {
    Serial.println("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting.");
    while (1) {
      yield();
    }
  } else {
    encoder.getState(state);
    Serial.print("Encoder Successfully Started at value = ");
    prevEncoderValue = state.currentValue;
    Serial.println(prevEncoderValue);
  }
  pinMode (button, INPUT_PULLUP);
}
void loop() {

static bool buttonOld = 1;
  bool state = digitalRead(button);
  if (buttonOld != state) {
    delay(200);
    buttonOld = state;
    if (!state) {
    BrightChange();
    Serial.println(mode);
    mode = mode + 1;
    mode = mode % 2;
  }
  }


  NewEncoder::EncoderState currentEncoderState;

  if (encoder.getState(currentEncoderState)) {
    Serial.print("Encoder: ");
    currentValue = currentEncoderState.currentValue;
    if (currentValue != prevEncoderValue) {
      Serial.println(currentValue);
      prevEncoderValue = currentValue;
    } else
      switch (currentEncoderState.currentClick) {
        case NewEncoder::UpClick:
          Serial.println("at upper limit.");
          break;

        case NewEncoder::DownClick:
          Serial.println("at lower limit.");
          break;

        default:
          break;
      }
  }
  BrightChange();
 }

Thank you!

The codeline

NewEncoder encoder(3, 2, ENDPOINT, STARTPOINT, 0, FULL_PULSE);

creates the encoder-object. After creation it is not possible to change the values for startpoint and endpoint.

This would require modifying the library by adding new functions to do so.

Another way to have different resolutions would be to use the function

getAndSet(NewNumber)

getAndSet returns the actual value and sets the value to NewNumber

So something like
some logic that uses upclick/downclick and then does

getAndSet(myEncoderVal +- 5);

could do the job.

I'm totally unfiamilar with your programming-style of using

NewEncoder::EncoderState currentEncoderState;
      switch (currentEncoderState.currentClick) {
        case NewEncoder::UpClick:
          Serial.println("at upper limit.");
          break;

        case NewEncoder::DownClick:
          Serial.println("at lower limit.");
          break;

I have seen it before but I have no idea what the advantage over "standard use of

NewEncoder myEncoder(13, 15, -20, 20, 0, FULL_PULSE);
void PrintEncoderVal() {
  up   = myEncoder.upClick();
  down = myEncoder.downClick();

  if (up || down) {
    currentValue = myEncoder;
    if (currentValue != prevEncoderValue) {
      Serial.print(F("Encoder: "));
      Serial.println(currentValue);
      prevEncoderValue = currentValue;
    } else if (up) {
      Serial.println(F("Encoder at upper limit."));
    } else {
      Serial.println(F("Encoder at lower limit."));
    }
  }
}

is
best regards Stefan

suggest you use the serial monitor to print the progress and actions of your code to debug it

The encoder's low / high endpoints can be dynamically changed using the newSettings() function.

Not according to the library "Read Me".
There is a function

bool newSettings(int16_t newMin, int16_t newMax, int16_t newCurrent, EncoderState &state);

Arguments:

int16_t newMin - new minVal.
int16_t newMax - new maxVal.
int16_t newCurrent - new encoder value.
NewEncoder::EncoderState &state - Reference to an EncoderState object. The state of the encoder after its value was changed will be written into this object.
Returns: - true if newMin < newMax. false otherwise.

@gfvalvo I knew this was available and was playing around with it last night, but I'm unclear as to how the last argument is to be written.

void BrightChange() {
  switch (mode) {
    case 0: ENDPOINT = 0;
    STARTPOINT = 20;
     newSettings(0, 20, currentValue, ????); 
    break;
    case 1: ENDPOINT = 1;
    STARTPOINT = 100;
    newSettings(1, 100, currentValue, ????);
    break;
    }
  }


The fourth argument to newSettings() is a variable of type NewEncoder::EncoderState. It is passed by reference and loaded with the encoder's updated state after the new settings have been applied.

The concept of an encoder state variable was introduced with Version 2.0. It and the reasoning for it is covered in the ReadMe under Version 2.x.

Also, note that the functions @StefanL38 used in his example code above were deprecated starting with Version 2.0. They will cause a compiler warning stating so if used with that version of the library or later. Again, this is covered in the ReadMe.