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!