Min and Max value of encoder

Hello, I am using the library called Encoder, I want to set min and max for both encoders. I know you can use constrain but I don't know how to use it. One of the encoder will change the color of the led strip and the other will change the brightness. So I need a min value and max value for each encoder. Can you help me?

My code is,

#include <FastLED.h>
#include <Encoder.h>
#include "Arduino.h"
#include "NewEncoder.h"
#define LED_PIN 10
#define NUM_LEDS 100
#define button 4
CRGB leds[NUM_LEDS];
int mode = 0;
Encoder knobLeft(2, 6);
Encoder knobRight(3, 4);
long newLeft, newRight;

void makeStripe() {
  switch (newLeft) {
    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();
  
  pinMode (button, INPUT_PULLUP);
    Serial.begin(9600);
  Serial.println("TwoKnobs Encoder Test:");
  
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
    
static bool buttonOld = 1;
  bool state = digitalRead(button);
  if (buttonOld != state) {
    delay(200);
    buttonOld = state;
    if (!state) {
    Serial.println(mode);
    mode = mode + 1;
    mode = mode % 2;
}       
}
    
  newLeft = knobLeft.read() / 4;
  newRight = knobRight.read() / 4;
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
    
    }

Like this?

   newLeft = knobLeft.read() / 4;
   if (newLeft > 255) // encoder max
   {
      newLeft = 255;
   }
   if (newLeft < 0) // encoder min
   {
      newLeft = 0;
   }

Than you for responding,

I just tested this and it works but if you keep rotating the rotary encoder past 255, the serial output stays at 255 but you have scroll back the rotary encoder until it starts counting back. So basically the serial monitor stops at 255 but it still counts past that.

How about this?

   newLeft = knobLeft.read() / 4;
   if (newLeft > 255) // encoder max
   {
      knobLeft.write(255);
   }
   if (newLeft < 0) // encoder min
   {
      knobLeft.write(0);
   }
1 Like

It works! Thank you for your help and have a nice day!

1 Like

you could change to the library NewEncoder that is able to set min and max-values at initialisation and the library NewEncoder works much more reliable with encoders that have mechanical switches like the KY-40

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.