#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#include "FastLED.h"
#include "EEPROM.h"
#define EEPROM_SIZE 25
#define CLK_PIN 23
#define DT_PIN 19
#define DIRECTION_CW 0
#define DIRECTION_CCW 1
int numberKeyPresseser = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
#define NUM_LEDS 64
#define LED_PIN 2
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
int numberKeyPresses = 0;
int R, G, B = 0;
bool color_Set = false;
bool led_run = false;
int tryR = 0;
int tryG = 0;
int tryB = 0;
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
Button button1 = { 16, 0, false };
//variables to keep track of the timing of recent interrupts
unsigned long button_time = 0;
unsigned long last_button_time = 0;
void IRAM_ATTR isr() {
button_time = millis();
if (button_time - last_button_time > 250) {
button1.numberKeyPresses++;
if (button1.numberKeyPresses > 3) {
button1.numberKeyPresses = 1;
color_Set = false;
}
button1.pressed = true;
last_button_time = button_time;
}
}
void setup() {
Serial.begin(9600);
pinMode(button1.PIN, INPUT_PULLUP);
attachInterrupt(button1.PIN, isr, FALLING);
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);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(0, R);
EEPROM.get(1, G);
EEPROM.get(2, B);
if (R > 0 || G > 0 || B > 0) {
color_Set = true;
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
} else {
color_Set = false;
R = 0;
G = 0;
B = 0;
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
}
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled_menu();
}
void loop() {
while (color_Set == false) {
RGB_Set();
}
while (led_run == false && color_Set == true) {
led_Run();
}
}
void led_Run() {
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
led_run = true;
}
void led_Update() {
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
delay(50);
}
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(255, 255, 255);
FastLED.show(); // Update the LEDs
delay(50);
}
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
}
void RGB_Set() {
if (button1.pressed) {
Serial.printf("Button has been pressed %u times\n", button1.numberKeyPresses);
button1.pressed = false;
}
while (button1.numberKeyPresses == 1) {
if (tryR == 0) {
tryG = 0;
tryB = 0;
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(255, 0, 0);
FastLED.show(); // Update the LEDs
delay(50);
}
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
tryR++;
}
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(" - numberKeyPresses: ");
Serial.println(R);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - numberKeyPresses: ");
Serial.println(R);
}
}
prev_CLK_state = CLK_state;
if (button1.pressed) {
Serial.printf("Button has been pressed %u times\n", button1.numberKeyPresses);
button1.pressed = false;
EEPROM.put(0, R);
EEPROM.commit();
led_Update();
}
}
while (button1.numberKeyPresses == 2) {
if (tryG == 0) {
tryR = 0;
tryB = 0;
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(0, 255, 0);
FastLED.show(); // Update the LEDs
delay(50);
}
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
tryG++;
}
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(" - numberKeyPresses: ");
Serial.println(G);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - numberKeyPresses: ");
Serial.println(G);
}
}
prev_CLK_state = CLK_state;
if (button1.pressed) {
Serial.printf("Button has been pressed %u times\n", button1.numberKeyPresses);
button1.pressed = false;
EEPROM.put(1, G);
EEPROM.commit();
led_Update();
}
}
while (button1.numberKeyPresses == 3) {
if (tryB == 0) {
tryG = 0;
tryR = 0;
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(0, 0, 255);
FastLED.show(); // Update the LEDs
delay(50);
}
for (int pinNo = 0; pinNo < NUM_LEDS; pinNo++) {
leds[pinNo] = CRGB(R, G, B);
FastLED.show(); // Update the LEDs
}
tryB++;
}
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(" - numberKeyPresses: ");
Serial.println(B);
} else {
Serial.print("ANTICLOCKWISE");
Serial.print(" - numberKeyPresses: ");
Serial.println(B);
}
}
prev_CLK_state = CLK_state;
if (button1.pressed) {
Serial.printf("Button has been pressed %u times\n", button1.numberKeyPresses);
button1.pressed = false;
EEPROM.put(2, B);
EEPROM.commit();
led_Update();
color_Set = true;
}
}
}
void oled_menu()
{
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(60, 0); // set position to display (x,y)
oled.println("MENU"); // set text
oled.setCursor(20, 20); // set position to display (x,y)
oled.println("CHANGE RGB:"); // set text
oled.display(); // OLED Display
}