#include <LEDFader.h>
#include <GyverButton.h>
int mode, brightness = 255, cycle, fadeAmount = 5;
unsigned long currentTime, loopTime;
boolean brightDirection = true, wasStep = false;
int MainLPin = 3; //PWM = D3 = Main-Light (Yellow Wire)
int MiniLPin = 10; //PWM = D10 = 2 Mini-Light (Green Wire)
int SoftLPin = 12; //NoPWM = D12 = Soft-Light (White Wire)
int BackLPin = 11; //PWM = D11 = Back-Light (Blue Wire)
int BtnPin = A0; //BTN = A0 Main Button (Black Wires)
GButton butt1(BtnPin);
LEDFader led = LEDFader(BackLPin);
/*
Modes:
=One Btn Click change mode
=Holding Btn - Brightness +/-
=Double Click - All Off in 1-5 Modes
0 - All off
1 - Main Light On
2 - Soft Light On
3 - 2 Mini-lights On
4 - Main Light & Soft Light On
5 - 2 Mini-lights & Soft Light On
*/
void setup() {
led.fade(255, 2500);
mode = 0;
cycle = 0;
currentTime = millis();
loopTime = currentTime;
pinMode(MainLPin,OUTPUT);
pinMode(MiniLPin,OUTPUT);
pinMode(SoftLPin,OUTPUT);
pinMode(BackLPin,OUTPUT);
butt1.setDebounce(90);
butt1.setTimeout(300);
butt1.setType(HIGH_PULL);
butt1.setDirection(NORM_OPEN);
butt1.setTickMode(AUTO);
butt1.setStepTimeout(30);
digitalWrite(MainLPin, LOW);
digitalWrite(MiniLPin, LOW);
digitalWrite(SoftLPin, LOW);
digitalWrite(BackLPin, LOW);
}
void loop() {
if (mode == 0){ //Mode:0 ALL OFF
digitalWrite(MainLPin, LOW);
digitalWrite(MiniLPin, LOW);
digitalWrite(SoftLPin, LOW);
digitalWrite(BackLPin, LOW);
if (butt1.isSingle()) mode++;
}
if (mode > 0){ //Turn On Back Light Fading mode for all modes except 0
led.update();
if (led.is_fading() == false) {
// Fade from 255 - 0
if (led.get_value() == 255) {
led.fade(0, 3000);
}
// Fade from 0 - 255
else {
led.fade(255, 3000);
}
}
}
if (mode == 1){ //Turn On Main Light
digitalWrite(MiniLPin, LOW);
digitalWrite(SoftLPin, LOW);
if (cycle == 1) brightness = 255;
if (butt1.isSingle()) mode++;
if (butt1.isDouble()){
mode = 0;
brightness = 255;
}
if (butt1.isStep()) {
wasStep = true;
if (brightDirection) {
brightness -= 5;
} else {
brightness += 5;
}
brightness = constrain(brightness, 3, 255);
}
if (butt1.isRelease()) {
if (wasStep) {
wasStep = false;
brightDirection = !brightDirection;
}
}
analogWrite(MainLPin, brightness);
}
if (mode == 2){ //Turn On Soft Light
digitalWrite(MainLPin, LOW);
digitalWrite(MiniLPin, LOW);
digitalWrite(SoftLPin, HIGH);
cycle = 1;
if (butt1.isSingle()) mode++;
if (butt1.isDouble()){
mode = 0;
brightness = 255;
}
}
if (mode == 3){ //Turn On 2 Mini-Lights
digitalWrite(MainLPin, LOW);
digitalWrite(SoftLPin, LOW);
if (cycle == 1) brightness = 255;
if (butt1.isSingle()) mode++;
if (butt1.isDouble()){
mode = 0;
brightness = 255;
}
if (butt1.isStep()) {
wasStep = true;
if (brightDirection) {
brightness -= 5;
} else {
brightness += 5;
}
brightness = constrain(brightness, 3, 255);
}
if (butt1.isRelease()) {
if (wasStep) {
wasStep = false;
brightDirection = !brightDirection;
}
}
analogWrite(MiniLPin, brightness);
}
if (mode == 4){ //Turn On Main Light & Soft Light
digitalWrite(MainLPin, HIGH);
digitalWrite(MiniLPin, LOW);
digitalWrite(SoftLPin, HIGH);
if (butt1.isSingle()) mode++;
if (butt1.isDouble()){
mode = 0;
brightness = 255;
}
}
if (mode == 5){ //Turn On 2 Mini-lights & Soft Light On
digitalWrite(MainLPin, LOW);
digitalWrite(MiniLPin, HIGH);
digitalWrite(SoftLPin, HIGH);
if (butt1.isSingle()) mode++;
if (mode == 6){
mode = 0;
cycle = 1;
}
if (butt1.isDouble()){
mode = 0;
brightness = 255;
}
}
}