#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
#include <FastLED.h>
// FastLED settings
#define LED_PIN 5 // Set the pin where your LED strip is connected
#define NUM_LEDS 75 // Set the number of LEDs in your strip
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
// Rotary encoder settings
#if defined(ESP8266)
#define ROTARY_ENCODER_A_PIN D6
#define ROTARY_ENCODER_B_PIN D5
#define ROTARY_ENCODER_BUTTON_PIN D7
#else
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 21
#define ROTARY_ENCODER_BUTTON_PIN 25
#endif
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
// Global variable to track the state of the special effect
bool specialEffectEnabled = false;
#define MAX_COLOURS 10
CRGB* colours[MAX_COLOURS] = {};
// Define the Cogapp colours
//CRGB cogappPurple = CRGB(231, 218, 255)
//CRGB cogappGreen = CRGB(237, 255, 218)
//CRGB cogappBlue = CRGB(218, 233, 255)
void cycleThroughColours() {
for( int colorStep=0; colorStep<256; colorStep++ ) {
int r = colorStep; // Redness starts at zero and goes up to full
int b = 255-colorStep; // Blue starts at full and goes down to zero
int g = 0; // No green needed to go from blue to red
// Now loop though each of the LEDs and set each one to the current color
for(int x = 0; x < NUM_LEDS; x++){
leds[x] = CRGB(r,g,b);
}
// Display the colors we just set on the actual LEDs
FastLED.show();
delay(10);
}
}
void rotary_onButtonClick() {
static unsigned long lastTimePressed = 0;
if (millis() - lastTimePressed < 500) { // Debounce the button press
return;
}
lastTimePressed = millis();
// Enable or disable the special effect
specialEffectEnabled = true;
cycleThroughColours();
specialEffectEnabled = false;
Serial.print("Button pressed ");
Serial.print(millis());
Serial.println(" milliseconds after restart");
}
void rotary_loop() {
if (rotaryEncoder.encoderChanged()) {
int newPos = rotaryEncoder.readEncoder();
int hueValue = newPos % 256; // Hue value for FastLED
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hueValue, 255, 255); // Change the colour based on the encoder position
}
FastLED.show();
// Print the hue value to the console
Serial.print("Encoder Value: ");
Serial.print(newPos);
Serial.print(", Hue Value Sent to FastLED: ");
Serial.println(hueValue);
}
if (rotaryEncoder.isEncoderButtonClicked()) {
rotary_onButtonClick();
}
}
void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
void setup() {
Serial.begin(115200);
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(0, 1000, true); // minValue, maxValue, circleValues
rotaryEncoder.setAcceleration(250);
// FastLED setup
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
rotary_loop();
delay(50);
}