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);
}
}