#include "GRGB.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED oled width, in pixels
#define SCREEN_HEIGHT 64 // OLED oled height, in pixels
// Declaration for an SSD1306 oled connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
LiquidCrystal_I2C lcd(0x27, 16, 2); // replace width using oled
GRGB diode(COMMON_CATHODE, 6, 5, 3); // куда подключены цвета (R, G, B)
int dLed = 9; // default fill light
//params
byte dim;
byte H; // vars for storing changed values
byte S; // default is 255
byte V;
byte P; // potentiometer
#include <FastLED.h>
#define NUM_LEDS 16
CRGB leds[NUM_LEDS];
/*
void loop() {
leds[0] = CRGB::Red; FastLED.show(); delay(500);
leds[0] = CRGB::Blue; FastLED.show(); delay(500);
}
*/
////////////////////////////
/////////////////// pins
////////////////////////////
int upButton = 10;
int downButton = 11;
int selectButton = 12;
int menu = 1;
//int LedOut1 = 3;
//int LedOut2 = 4;
//int LedOut3 = 5;
//int LedOut4 = 6;
int Timer = 0;
int Timer_1 = 0;
int Timer2 = 0;
int Timer3 = 0;
int bTime = 500;
int _butDelay = 1500; // but delay = delay for speed up value change
int menuTimer, myTimer2, _butTimer;
int menuTimerMax = 30000; // chosen menu timer and auto set current value
int _delay = 100; // Delay after each action.change if too slow or too fast.
void setup() {
dim = 255;
H = 255; // unstable set
S = 255;
V = 255;
FastLED.addLeds<WS2812, 6>(leds, NUM_LEDS);
lcd.init(); //oled func
lcd.backlight();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
//pinMode(LedOut1, OUTPUT);
//pinMode(LedOut2, OUTPUT);
//pinMode(LedOut3, OUTPUT);
//pinMode(LedOut4, OUTPUT);
pinMode(dLed, OUTPUT);
lcd.setCursor(0, 0);
lcd.print(String("LedControl HSV"));
lcd.setCursor(0, 1);
lcd.print(String("v_1.001"));
diode.setHSV(H, S, V);
// SSD1306_SWITCHCAPVCC = generate oled voltage from 3.3V internally
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Show initial oled buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
oled.clearDisplay();
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.setFont(NULL);
oled.setCursor(0, 0);
oled.print("LedControl");
oled.setCursor(32, 32);
oled.setTextSize(3);
oled.print("HSV");
oled.display();
delay(2000); // Pause for 3 seconds
oled.clearDisplay();
updateMenu();
upMenu2();
}
void executeAction() { //exec action depending on current menu state
switch (menu) {
case 1:
chose1();
return dim;
break;
case 2:
chose2();
break;
case 3:
chose3();
break;
case 4:
chose4();
break;
}
}
//Decrease Parameter by 2 buttons func, ex dimmer = Setchange(dimmer);
int (SetChange(int param)) {
int _param;
_param = param;
///decreaSE
if (!digitalRead(downButton))
{
_param--;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime);
_butTimer = millis();
}
while (!digitalRead(downButton))// decrease dimmer
{ //holding button
if (millis() - _butTimer <= _butDelay ) {
_param--;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime / 2);
}
if (millis() - _butTimer >= _butDelay ) {
_param--;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime / 50);
}
};
///increase
//1 press button
if (!digitalRead(upButton))
{
_param++;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime);
_butTimer = millis();
}
while (!digitalRead(upButton))// decrease dimmer
{ //holding button
if (millis() - _butTimer <= _butDelay ) {
_param++;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime / 2);
}
if (millis() - _butTimer >= _butDelay ) {
_param++;
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(byte(_param));
//upMenu2();
oledChose(_param);
delay(bTime / 50);
}
};
param = _param;
return param;
}
//chose definitions
void chose1() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String("Dimmer ") + byte(dim));
delay(bTime);
while (digitalRead(selectButton)) {
dim = SetChange(dim);
}
analogWrite(dLed, dim);
//if (digitalRead(selectButton)){dim = _dim;}
}
void chose2() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String("Hue ") + byte(H));
delay(bTime);
while (digitalRead(selectButton)) {
H = SetChange(H);
}
}
void chose3() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String("Sat ") + byte(S));
delay(bTime);
while (digitalRead(selectButton)) {
S = SetChange(S);
}
}
void chose4() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String("V -- ") + byte(V));
delay(bTime);
while (digitalRead(selectButton)) {
V = SetChange(V);
}
}
void oledChose(int _param) {
oled.clearDisplay();
oled.setTextSize(3);
oled.setCursor(32, 32);
oled.print(_param);
oled.display();
}
void updateMenu() { // menu update block with cursor
switch (menu) { // switch case depending on "menu" state
case 0: // return menu to default state (1)
menu = 1;
break;
case 1: // dim select case
upMenu();
lcd.setCursor(0, 0);
lcd.print(String(">"));
//upMenu2();
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print(">");
oled.display();
break;
case 2: // h select
upMenu();
lcd.setCursor(8, 0);
lcd.print(String(">"));
//upMenu2();
oled.clearDisplay();
oled.setCursor(0, 16);
oled.print(">");
oled.display();
break;
case 3: // s
upMenu();
lcd.setCursor(0, 1);
lcd.print(String(">"));
//upMenu2();
oled.clearDisplay();
oled.setCursor(0, 32);
oled.print(">");
oled.display();
break;
case 4: // v
upMenu();
lcd.setCursor(8, 1);
lcd.print(String(">"));
//upMenu2();
oled.clearDisplay();
oled.setCursor(0, 48);
oled.print(">");
oled.display();
break;
case 5: // return menu to previous state (4)
menu = 4;
break;
}
}
// update all without cursor part for shortening sketch
void upMenu() { // default menu update
lcd.clear();
lcd.print(String(" DIM") + byte(dim)); // show cur dim value
lcd.setCursor(8, 0);
lcd.print(String(" H") + byte(H)); // show cur HSV value
lcd.setCursor(0, 1);
lcd.print(String(" S") + byte(S));
lcd.setCursor(8, 1);
lcd.print(String(" V") + byte(V));
}
//oled // update all without cursor part for shortening sketch
void upMenu2() { // default menu update
//oled.clearDisplay();
//oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.setFont(NULL);
oled.setCursor(0, 0);
//oled.print(">");
oled.print(" DIM = ");
oled.print(dim);
oled.setCursor(0, 16);
oled.print(" HUE = ");
oled.print(H);
oled.setCursor(0, 32);
oled.print(" SAT = ");
oled.print(S);
oled.setCursor(0, 48);
oled.print(" V = ");
oled.print(V);
//oled.drawLine(75, 0, 75, 18, 1);
oled.display(); // render graphics
}
void loop() {
P = analogRead(0); // получаем 0-255 // P - потенциометр
if (!digitalRead(downButton)) {
menu++; // change menu state
updateMenu();
upMenu2();
delay(10);
}
if (!digitalRead(upButton)) {
menu--; // change menu state
updateMenu();
upMenu2();
delay(10);
}
if (!digitalRead(selectButton)) {
executeAction(); //execute
updateMenu();
upMenu2();
diode.setHSV(H, S, V);
leds.setHSV( H, S, V);
delay(10);
}
//dim = dim;
//H = H; // vars for storing changed values
//S = S; // default is 255
//V = V;
//upMenu2();
delay(100);
}