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