Skip to content

Instantly share code, notes, and snippets.

@steveheinsch
Last active January 28, 2020 06:29
Show Gist options
  • Save steveheinsch/618d57da39d51ddd3103e2bdc57f54cc to your computer and use it in GitHub Desktop.
Save steveheinsch/618d57da39d51ddd3103e2bdc57f54cc to your computer and use it in GitHub Desktop.
Rotary Encoder demo for FastLED. Fading up/down on power on/off + fading up/down on encoder change
/*
Demo to show fading in/out brightness for LED's using a Rotary Encoder.
Code has comments for where FastLED commands would be. FastLED is not
needed for this. You can just open the serial monitor and watch things change
for this DEMO. This Demo is using an ESP32, and using a specific Encoder library
for it.
1. Press ON - fades from 0 up to powerOnBrightness (120)
2. Press Off - Fades from current brightness to 0
3. Turn Knob Right - Increase brightness by encoderSteps (20) and fade up to it
4. Turn Knob Left - Decrease brightness by encoderSteps (20) and fade down to it
5. If power=0, Disable counting when encoder turns. No need to adjust brightness if power is off!
6. If power=1, Enable counting and always ensure brightness is between minBrightness and maxBrightness (don't exceed set limits)
/*
#include <Arduino.h>
#include <ESP32Encoder.h> // https://github.com/madhephaestus/ESP32Encoder
#include <EasyButton.h> // https://github.com/evert-arias/EasyButton
// Encoder/Button
#define PIN_ENCODER_CLK 18 // Using a cheap KY-040 Rotary Encoder from Amazon
#define PIN_ENCODER_DAT 19
#define PIN_ENCODER_BUTTON 5
EasyButton encoderButton(PIN_ENCODER_BUTTON);
ESP32Encoder encoder;
// Default Parameters, ok to change
uint8_t encoderSteps = 20; // How many steps each encoder rotary tick represents (for +/- brightness)
uint8_t minBrightness = 20; // The mininum brightness we want the lights to be (looks bad below 20)
uint8_t maxBrightness = 250; // The max brigntness, in case you don't want 255
uint8_t powerOnBrightness = 120; // Default to use when powering ON. Will fade from 0 up to this number.
// Internal parameters, leave alone
uint8_t targetBrightness = 0; // The target brightness we want to be at
uint16_t lastEncoderValue = 0; // The last encoder value so we can detect changes
uint8_t brightness = 0; // The current brightness
// Encoder - Button Click - Turn lamp on/off and set the target brightness
void handleEncoderButtonClick() {
power = !power; // toggle power state
if (power == 0) {
encoder.pauseCount(); // If power is off, disable encoder
Serial.println("Power Button Clicked - Off");
targetBrightness = 0;
} else {
encoder.resumeCount();
Serial.println("Power Button Clicked - On");
targetBrightness = powerOnBrightness;
}
}
void setup() {
Serial.begin(115200);
// Rotary Encoder/Momenrary Button
ESP32Encoder::useInternalWeakPullResistors = true;
encoder.attachSingleEdge(PIN_ENCODER_CLK, PIN_ENCODER_DAT);
encoderButton.begin();
encoderButton.onPressed(handleEncoderButtonClick);
}
void readEncoder() {
encoderButton.read();
int16_t encoderValue = encoder.getCountRaw();
// Did it change?
if (encoderValue != lastEncoderValue) {
Serial.print("Enocoder value changed from ");
Serial.print(lastEncoderValue);
Serial.print(" to ");
Serial.println(encoderValue);
// Add/Subtract to/from target brightness, but stay within 0-255.
if (encoderValue > lastEncoderValue) targetBrightness = constrain(targetBrightness + encoderSteps, minBrightness, maxBrightness);
else targetBrightness = constrain(targetBrightness - encoderSteps, minBrightness, maxBrightness);
// Store encoder value to compare for changes on the next loop
lastEncoderValue = encoderValue;
}
}
// You san adjust in here to slow down/speed up how fast it's incrementing/decrementing by using millis() or something
void adjustBrightness() {
if (targetBrightness > brightness) {
brightness++;
Serial.print("Brightness = ");
Serial.println(brightness);
// FastLED.setBrightness(brightness);
} else if (targetBrightness < brightness) {
brightness--;
Serial.print("Brightness = ");
Serial.println(brightness);
// FastLED.setBrightness(brightness);
}
}
void loop() {
// 1. Check if brightness has increased/decreased. Have at top of loop.
readEncoder();
// 2. Your normal FastLED Stuff Here...
// 3. Adjust the brightness if needed. Have at bottom of loop just before FastLED.show()
adjustBrightness();
// 4. Display it all
// FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment