implemented overall dimming, speed regulation and new smooth animation

This commit is contained in:
Ronald Schaten 2014-04-25 07:54:42 +02:00
parent 9a7069c43f
commit 55b602d2c8

View File

@ -46,21 +46,37 @@ int levelRed = 0x00;
int levelGreen = 0x00; int levelGreen = 0x00;
int levelBlue = 0x00; int levelBlue = 0x00;
int levelWhite = 0x01; int levelWhite = 0x01;
int levelTotal = 0x80;
int mode = MODE_CONSTANT; int mode = MODE_CONSTANT;
int modestep = 0; int modestep = 0;
int animationspeed = 10; int animationspeed = 5;
long steplength = 0; long steplength = 0;
IRrecv irrecv(IR_PIN); IRrecv irrecv(IR_PIN);
decode_results results; decode_results results;
simtronyx_RGB_LED strip(LED_RED, LED_GREEN, LED_BLUE); simtronyx_RGB_LED strip(LED_RED, LED_GREEN, LED_BLUE);
void setRGBW(int red, int green, int blue, int white) {
// dim all channels according to levelTotal
red = map(red, 0x00, 0xff, 0x00, levelTotal);
green = map(green, 0x00, 0xff, 0x00, levelTotal);
blue = map(blue, 0x00, 0xff, 0x00, levelTotal);
white = map(white, 0x00, 0xff, 0x00, levelTotal);
// keep output at least at 0x01 if channel is not turned off
if (levelRed > 0) { red = constrain(red, 0x01, 0xff); }
if (levelGreen > 0) { green = constrain(green, 0x01, 0xff); }
if (levelBlue > 0) { blue = constrain(blue, 0x01, 0xff); }
if (levelWhite > 0) { white = constrain(white, 0x01, 0xff); }
// set output
strip.setRGB(red, green, blue);
analogWrite(LED_WHITE, white);
}
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
pinMode(LED_WHITE, OUTPUT); pinMode(LED_WHITE, OUTPUT);
analogWrite(LED_WHITE, levelWhite); setRGBW(levelRed, levelGreen, levelBlue, levelWhite);
strip.setRGB(levelRed, levelGreen, levelBlue);
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
} }
@ -135,30 +151,16 @@ void loop() {
strip.animateStart(); strip.animateStart();
break; break;
case IR_KEY_SMOOTH: case IR_KEY_SMOOTH:
mode = MODE_SMOOTH; mode == MODE_SMOOTH ? mode = MODE_CONSTANT : mode = MODE_SMOOTH;
strip.animateColorsClear(); strip.animateColorsClear();
strip.animateColorAdd(255, 0, 0, 100); strip.animateColorAdd(0x00, 0x00, 0x00, 100);
strip.animateColorAdd(255, 255, 0, 100); strip.animateColorAdd(levelRed, levelGreen, levelBlue, 100);
strip.animateColorAdd( 0, 255, 0, 100);
strip.animateColorAdd( 0, 255, 255, 100);
strip.animateColorAdd( 0, 0, 255, 100);
strip.animateColorAdd(255, 0, 255, 100);
strip.animateStart(); strip.animateStart();
break; break;
case IR_KEY_DIM: case IR_KEY_DIM: levelTotal = constrain(levelTotal-0x10, 0x01, 0xff); break;
levelWhite -= 0x10; case IR_KEY_BRIGHT: levelTotal = constrain(levelTotal+0x10, 0x01, 0xff); break;
if (levelWhite < 0x00) { case IR_KEY_ALLON: animationspeed = constrain(animationspeed+1, 1, 15); break;
levelWhite = 0x00; case IR_KEY_ALLOFF: animationspeed = constrain(animationspeed-1, 1, 15); break;
}
analogWrite(LED_WHITE, levelWhite);
break;
case IR_KEY_BRIGHT:
levelWhite += 0x10;
if (levelWhite > 0xFF) {
levelWhite = 0xFF;
}
analogWrite(LED_WHITE, levelWhite);
break;
default: default:
Serial.print("proto: "); Serial.print("proto: ");
Serial.print(results.decode_type, DEC); Serial.print(results.decode_type, DEC);
@ -168,8 +170,7 @@ void loop() {
} }
if (mode == MODE_CONSTANT) { if (mode == MODE_CONSTANT) {
strip.animateStop(); strip.animateStop();
analogWrite(LED_WHITE, levelWhite); setRGBW(levelRed, levelGreen, levelBlue, levelWhite);
strip.setRGB(levelRed, levelGreen, levelBlue);
} }
} }
} }
@ -184,12 +185,10 @@ void loop() {
if (steplength <= 0) { if (steplength <= 0) {
if (modestep == 1) { if (modestep == 1) {
modestep = 0; modestep = 0;
analogWrite(LED_WHITE, levelWhite); setRGBW(levelRed, levelGreen, levelBlue, levelWhite);
strip.setRGB(levelRed, levelGreen, levelBlue);
} else { } else {
modestep = 1; modestep = 1;
analogWrite(LED_WHITE, 0x00); setRGBW(0x00, 0x00, 0x00, 0x00);
strip.setRGB(0x00, 0x00, 0x00);
} }
steplength = animationspeed * 10000L; steplength = animationspeed * 10000L;
} }
@ -198,12 +197,14 @@ void loop() {
steplength--; steplength--;
if (steplength <= 0) { if (steplength <= 0) {
switch (modestep) { switch (modestep) {
case 0: levelRed = 0xff; levelGreen = levelBlue = 0x00; modestep = 1; break; case 0: levelRed = 0xff; levelGreen = 0x00; levelBlue = 0x00; modestep = 1; break;
case 1: levelGreen = 0xff; levelBlue = levelRed = 0x00; modestep = 2; break; case 1: levelRed = 0xff; levelGreen = 0xff; levelBlue = 0x00; modestep = 2; break;
case 2: levelBlue = 0xff; levelRed = levelGreen = 0x00; modestep = 0; break; case 2: levelRed = 0x00; levelGreen = 0xff; levelBlue = 0x00; modestep = 3; break;
case 3: levelRed = 0x00; levelGreen = 0xff; levelBlue = 0xff; modestep = 4; break;
case 4: levelRed = 0x00; levelGreen = 0x00; levelBlue = 0xff; modestep = 5; break;
case 5: levelRed = 0xff; levelGreen = 0x00; levelBlue = 0xff; modestep = 0; break;
} }
analogWrite(LED_WHITE, levelWhite); setRGBW(levelRed, levelGreen, levelBlue, levelWhite);
strip.setRGB(levelRed, levelGreen, levelBlue);
steplength = animationspeed * 10000L; steplength = animationspeed * 10000L;
} }
break; break;