#define ENCODER_CLK 2
#define ENCODER_DT 3
#define LED_PIN 7
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 64
#define SERIAL_BAUDRATE 115200
#include <FastLED.h>
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
Serial.begin(SERIAL_BAUDRATE);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
int lastClk = HIGH;
void loop() {
// put your main code here, to run repeatedly:
int newClk = digitalRead(ENCODER_CLK);
static uint8_t hue = 0;
Serial.print("x1");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
Serial.print("x2");
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
if (newClk != lastClk) {
// Houve uma mudança no pino CLK
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Girando no sentido horário ⏩");
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Girando no sentido anti-horário ⏪");
}
}
}