#include "FastLED.h"
#define CLK_PIN 23
#define DT_PIN 22
#define SW_PIN 16
#define DIRECTION_CW 0
#define DIRECTION_CCW 1
int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
#define NUM_LEDS 2
#define LED_PIN 2
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
int count = 0;
int R, G, B = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
Serial.begin(9600);
pinMode(SW_PIN, INPUT_PULLUP);
pinMode(2, OUTPUT);
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
prev_CLK_state = digitalRead(CLK_PIN);
FastLED.addLeds<WS2812, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
buttonState = digitalRead(SW_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
Serial.println("PRESSED");
count = count + 1;
Serial.println(count);
}
}
lastButtonState = buttonState;
while (count == 1) {
CLK_state = digitalRead(CLK_PIN);
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
if (digitalRead(DT_PIN) == HIGH) {
R = R - 10;
if (R < 0) {
R = 0;
}
direction = DIRECTION_CCW;
} else {
R = R + 10;
if (R > 255) {
R = 255;
}
direction = DIRECTION_CW;
}
for (int pinNo = 0; pinNo < NUM_LEDS + 1; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
Serial.print("Rotary Encoder:: direction: ");
if (direction == DIRECTION_CW) {
Serial.print("CLOCKWISE");
Serial.print(" - count: ");
Serial.println(R);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - count: ");
Serial.println(R);
}
}
prev_CLK_state = CLK_state;
buttonState = digitalRead(SW_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
Serial.println("SET R");
led_Update();
count = count + 1;
Serial.println(count);
}
}
lastButtonState = buttonState;
}
while (count == 2) {
CLK_state = digitalRead(CLK_PIN);
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
if (digitalRead(DT_PIN) == HIGH) {
G = G - 10;
if (G < 0) {
G = 0;
}
direction = DIRECTION_CCW;
} else {
G = G + 10;
if (G > 255) {
G = 255;
}
direction = DIRECTION_CW;
}
for (int pinNo = 0; pinNo < NUM_LEDS + 1; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
Serial.print("Rotary Encoder:: direction: ");
if (direction == DIRECTION_CW) {
Serial.print("CLOCKWISE");
Serial.print(" - count: ");
Serial.println(G);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - count: ");
Serial.println(G);
}
}
prev_CLK_state = CLK_state;
buttonState = digitalRead(SW_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
Serial.println("SET G");
led_Update();
count = count + 1;
Serial.println(count);
}
}
lastButtonState = buttonState;
}
while (count == 3) {
CLK_state = digitalRead(CLK_PIN);
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
if (digitalRead(DT_PIN) == HIGH) {
B = B - 10;
if (B < 0) {
B = 0;
}
direction = DIRECTION_CCW;
} else {
B = B + 10;
if (B > 255) {
B = 255;
}
direction = DIRECTION_CW;
}
for (int pinNo = 0; pinNo < NUM_LEDS + 1; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
Serial.print("Rotary Encoder:: direction: ");
if (direction == DIRECTION_CW) {
Serial.print("CLOCKWISE");
Serial.print(" - count: ");
Serial.println(B);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - count: ");
Serial.println(B);
}
}
prev_CLK_state = CLK_state;
buttonState = digitalRead(SW_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
Serial.println("SET B");
count = 0;
Serial.println(count);
}
}
lastButtonState = buttonState;
}
}
void led_Update()
{
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
delay(50);
leds[pinNo] = CRGB(0, 0, 0);
FastLED.show(); // Update the LEDs
delay(50);
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
}