Added Modes

Support of multiple illumination modes. Currently:
- RGB control mode
- HSV control mode
- Rainbow Animation mode
This commit is contained in:
José Valdiviesso 2019-06-10 22:17:30 +01:00
parent 214db7728d
commit 00aa9281a7
1 changed files with 166 additions and 83 deletions

View File

@ -1,31 +1,35 @@
#include <FastLED.h> #include <FastLED.h>
FASTLED_USING_NAMESPACE
#define NUM_LEDS 292 //how many leds #define NUM_LEDS 292 //how many leds
#define DATA_PIN 21 //data min, hint: 21 #define DATA_PIN 21 //data min, hint: 21
#define Diff 8; #define Diff 8;
#define HSVDiff 4; #define HSVDiff 2;
//Brightness & RGB pins //Brightness & RGB pins
#define BriPls 2 #define BriPls 2
#define BriMin 3 #define BriMin 3
#define BriRst 4 #define BriRst 4
#define RedPls 5 #define btn11 5
#define RedMin 6 #define btn12 6
#define RedRst 7 #define btn13 7
#define GrnPls 8 #define btn21 8
#define GrnMin 9 #define btn22 9
#define GrnRst 10 #define btn23 10
#define BluPls 16 #define btn31 16
#define BluMin 14 #define btn32 14
#define BluRst 15 #define btn33 15
#define ExtraOne 18 #define btnDefault 18
#define ExtraTwo 19 #define btnModeBack 19
#define ExtraTre 20 #define btnModeNext 20
#define FRAMES_PER_SECOND 120
int BriArr[] = {0, 2, 4, 8, 16, 32, 64, 128, 255}; int BriArr[] = {0, 2, 4, 8, 16, 32, 64, 128, 255};
int BriVal = 5; int BriVal = 5;
@ -37,7 +41,11 @@ int HueVal = 0;
int SatVal = 255; int SatVal = 255;
int ValVal = 255; int ValVal = 255;
int State = 1; int mode = 1;
int nModes = 3; //total number of modes
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
//buttons //buttons
long tm = 0; long tm = 0;
long debounce = 200; long debounce = 200;
@ -49,40 +57,36 @@ CRGB leds[NUM_LEDS];
void updateStripRGB(){ void updateStripRGB(){
fill_solid(leds,NUM_LEDS,CRGB(RedVal,GrnVal,BluVal)); fill_solid(leds,NUM_LEDS,CRGB(RedVal,GrnVal,BluVal));
FastLED.show();
} }
void updateStripHSV(){ void updateStripHSV(){
fill_solid(leds,NUM_LEDS,CHSV(HueVal,SatVal,ValVal)); fill_solid(leds,NUM_LEDS,CHSV(HueVal,SatVal,ValVal));
FastLED.show();
} }
void updateStripBri(){ void updateStripBri(){
FastLED.setBrightness(BriArr[BriVal]); FastLED.setBrightness(BriArr[BriVal]);
FastLED.show();
} }
void setPins(){ void setPins(){
pinMode(BriPls, INPUT_PULLUP); pinMode(BriPls, INPUT_PULLUP);
pinMode(BriMin, INPUT_PULLUP); pinMode(BriMin, INPUT_PULLUP);
pinMode(BriRst, INPUT_PULLUP); pinMode(BriRst, INPUT_PULLUP);
pinMode(RedPls, INPUT_PULLUP); pinMode(btn11, INPUT_PULLUP);
pinMode(RedMin, INPUT_PULLUP); pinMode(btn12, INPUT_PULLUP);
pinMode(RedRst, INPUT_PULLUP); pinMode(btn13, INPUT_PULLUP);
pinMode(GrnPls, INPUT_PULLUP); pinMode(btn21, INPUT_PULLUP);
pinMode(GrnMin, INPUT_PULLUP); pinMode(btn22, INPUT_PULLUP);
pinMode(GrnRst, INPUT_PULLUP); pinMode(btn23, INPUT_PULLUP);
pinMode(BluPls, INPUT_PULLUP); pinMode(btn31, INPUT_PULLUP);
pinMode(BluMin, INPUT_PULLUP); pinMode(btn32, INPUT_PULLUP);
pinMode(BluRst, INPUT_PULLUP); pinMode(btn33, INPUT_PULLUP);
pinMode(ExtraOne, INPUT_PULLUP); pinMode(btnDefault, INPUT_PULLUP);
pinMode(ExtraTwo, INPUT_PULLUP); pinMode(btnModeBack, INPUT_PULLUP);
pinMode(ExtraTre, INPUT_PULLUP); pinMode(btnModeNext, INPUT_PULLUP);
} }
void setup() { void setup() {
Serial.begin(9600); delay(1000);
delay(500);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
updateStripRGB(); updateStripRGB();
updateStripBri(); updateStripBri();
@ -147,67 +151,79 @@ void decHue(){
void rstHue(){ void rstHue(){
HueVal = 0; HueVal = 0;
} }
//Mode Functions
void modeNext(){
mode++;
if(mode>nModes){mode = 1;}
}
void modeBack(){
mode--;
if(mode<1){mode = nModes;}
}
void modeRst(){
mode = 0;
}
//READ PRESSED BUTTON //READ PRESSED BUTTON
int getBtn(){ int getBtn(){
//Serial.println(digitalRead(BriMin)); //Serial.println(digitalRead(BriMin));
if(digitalRead(BriPls) == LOW && millis() - tm > debounce){ if(digitalRead(BriPls) == LOW && millis() - tm > debounce){
Serial.println("Bri+"); //Bri+
tm = millis(); tm = millis();
return 1; return 1;
}else if(digitalRead(BriMin) == LOW && millis() - tm > debounce){ }else if(digitalRead(BriMin) == LOW && millis() - tm > debounce){
Serial.println("Bri-"); //Bri-
tm = millis(); tm = millis();
return 2; return 2;
}else if(digitalRead(BriRst) == LOW && millis() - tm > debounce){ }else if(digitalRead(BriRst) == LOW && millis() - tm > debounce){
Serial.println("Bri Rst"); //Bri Rst
tm = millis(); tm = millis();
return 3; return 3;
}else if(digitalRead(RedPls) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn11) == LOW && millis() - tm > debounce){
Serial.println("Red+"); //Red+
tm = millis(); tm = millis();
return 4; return 4;
}else if(digitalRead(RedMin) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn12) == LOW && millis() - tm > debounce){
Serial.println("Red-"); //Red-
tm = millis(); tm = millis();
return 5; return 5;
}else if(digitalRead(RedRst) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn13) == LOW && millis() - tm > debounce){
Serial.println("Red Rst"); //Red Rst
tm = millis(); tm = millis();
return 6; return 6;
}else if(digitalRead(GrnPls) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn21) == LOW && millis() - tm > debounce){
Serial.println("Grn+"); //Grn+
tm = millis(); tm = millis();
return 7; return 7;
}else if(digitalRead(GrnMin) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn22) == LOW && millis() - tm > debounce){
Serial.println("Grn-"); //Grn-
tm = millis(); tm = millis();
return 8; return 8;
}else if(digitalRead(GrnRst) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn23) == LOW && millis() - tm > debounce){
Serial.println("Grn Rst"); //Grn Rst
tm = millis(); tm = millis();
return 9; return 9;
}else if(digitalRead(BluPls) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn31) == LOW && millis() - tm > debounce){
Serial.println("Blu+"); //Blu+
tm = millis(); tm = millis();
return 10; return 10;
}else if(digitalRead(BluMin) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn32) == LOW && millis() - tm > debounce){
Serial.println("Blu-"); //Blu-
tm = millis(); tm = millis();
return 11; return 11;
}else if(digitalRead(BluRst) == LOW && millis() - tm > debounce){ }else if(digitalRead(btn33) == LOW && millis() - tm > debounce){
Serial.println("Blu Rst"); //Blu Rst
tm = millis(); tm = millis();
return 12; return 12;
}else if(digitalRead(ExtraOne) == LOW && millis() - tm > debounce){ }else if(digitalRead(btnDefault) == LOW && millis() - tm > debounce){
Serial.println("Ext 1"); //Ext 1
tm = millis(); tm = millis();
return 13; return 13;
}else if(digitalRead(ExtraTwo) == LOW && millis() - tm > debounce){ }else if(digitalRead(btnModeBack) == LOW && millis() - tm > debounce){
Serial.println("Ext 2"); //Ext 2
tm = millis(); tm = millis();
return 14; return 14;
}else if(digitalRead(ExtraTre) == LOW && millis() - tm > debounce){ }else if(digitalRead(btnModeNext) == LOW && millis() - tm > debounce){
Serial.println("Ext 3"); //Ext 3
tm = millis(); tm = millis();
return 15; return 15;
}else{ }else{
@ -215,6 +231,76 @@ int getBtn(){
} }
} }
void staticRGBMode(int mData){
switch(mData){
case 4:
incRed();
break;
case 5:
decRed();
break;
case 6:
rstRed();
break;
case 7:
incGrn();
break;
case 8:
decGrn();
break;
case 9:
rstGrn();
break;
case 10:
incBlu();
break;
case 11:
decBlu();
break;
case 12:
rstBlu();
break;
default:
break;
}
updateStripRGB();
}
void staticHSVMode(int mData){
switch(mData){
case 4:
incHue();
break;
case 5:
decHue();
break;
case 6:
rstHue();
break;
default:
break;
}
updateStripHSV();
}
void rainbowMode(int mData){
fill_rainbow( leds, NUM_LEDS, gHue);
gHue += 2;
}
void modeAction(int mData){
switch(mode){
case 1:
staticRGBMode(mData);
break;
case 2:
staticHSVMode(mData);
break;
case 3:
rainbowMode(mData);
break;
}
}
void loop() { void loop() {
resp = getBtn(); resp = getBtn();
@ -222,64 +308,61 @@ void loop() {
case 1: case 1:
incBri(); incBri();
updateStripBri(); updateStripBri();
modeAction(0);
break; break;
case 2: case 2:
decBri(); decBri();
updateStripBri(); updateStripBri();
modeAction(0);
break; break;
case 3: case 3:
rstBri(); rstBri();
updateStripBri(); updateStripBri();
modeAction(0);
break; break;
case 4: case 4:
incRed(); modeAction(resp);
updateStripRGB();
break; break;
case 5: case 5:
decRed(); modeAction(resp);
updateStripRGB();
break; break;
case 6: case 6:
rstRed(); modeAction(resp);
updateStripRGB();
break; break;
case 7: case 7:
incGrn(); modeAction(resp);
updateStripRGB();
break; break;
case 8: case 8:
decGrn(); modeAction(resp);
updateStripRGB();
break; break;
case 9: case 9:
rstGrn(); modeAction(resp);
updateStripRGB();
break; break;
case 10: case 10:
incBlu(); modeAction(resp);
updateStripRGB();
break; break;
case 11: case 11:
decBlu(); modeAction(resp);
updateStripRGB();
break; break;
case 12: case 12:
rstBlu(); modeAction(resp);
updateStripRGB();
break; break;
case 13: case 13:
incHue(); modeRst();
updateStripHSV(); modeAction(0);
break; break;
case 14: case 14:
decHue(); modeBack();
updateStripHSV(); modeAction(0);
break; break;
case 15: case 15:
rstHue(); modeNext();
updateStripHSV(); modeAction(0);
break; break;
default: default:
modeAction(0);
break; break;
} }
} FastLED.show();
FastLED.delay(1000/FRAMES_PER_SECOND);
}