Added breathing mode and adjusted fading speed

This commit is contained in:
Nis Boerge Wechselberg 2015-11-20 21:38:45 +01:00
parent 587a7ed304
commit d46229bf4b

View file

@ -49,10 +49,13 @@ unsigned char blueValue;
unsigned char decColor; unsigned char decColor;
// Scale factors // Scale factors
float globalScale = 3.0; const float globalScale = 3.0;
float redScale = 1.0; const float redScale = 1.0;
float greenScale = 1.0; const float greenScale = 1.0;
float blueScale = 0.7; const float blueScale = 0.7;
float breathScale = 0.999;
float breathAdjustment = -0.001;
// HTTP configuration // HTTP configuration
const unsigned char port = 80; const unsigned char port = 80;
@ -149,23 +152,30 @@ void loop() {
if (red < 0) { red = 0; } if (red < 0) { red = 0; }
if (green < 0) { green = 0; } if (green < 0) { green = 0; }
if (blue < 0) { blue = 0; } if (blue < 0) { blue = 0; }
if (mode > 1) { mode = 1; } if (mode > 3) { mode = 3; }
if (red > 255) { red = 255; } if (red > 255) { red = 255; }
if (green > 255) { green = 255; } if (green > 255) { green = 255; }
if (blue > 255) { blue = 255; } if (blue > 255) { blue = 255; }
// Update values // Update values
modeValue = mode; modeValue = mode;
if (mode == 1) {
decColor = 0;
redValue = 255; breathScale = 0.999;
greenValue = 0; breathAdjustment = -0.001;
blueValue = 0; decColor = 0;
} else {
if (modeValue % 2 == 0) {
redValue = red; redValue = red;
greenValue = green; greenValue = green;
blueValue = blue; blueValue = blue;
} }
if (modeValue % 2 != 0) {
redValue = 255;
greenValue = 0;
blueValue = 0;
}
persistToEEPROM(); persistToEEPROM();
} }
} else if (req.indexOf("/get/") != -1) { } else if (req.indexOf("/get/") != -1) {
@ -202,9 +212,7 @@ void loop() {
} }
void updateLight() { void updateLight() {
if (modeValue == 0) { if (modeValue % 2 == 1) {
// Normal static color mode
} else if (modeValue == 1) {
// Color cylce mode // Color cylce mode
unsigned short rgbColor[3]; unsigned short rgbColor[3];
rgbColor[0] = redValue; rgbColor[0] = redValue;
@ -224,20 +232,31 @@ void updateLight() {
decColor = incColor; decColor = incColor;
} }
} }
if (modeValue > 1) {
// Breath mode
breathScale += breathAdjustment;
if (breathScale <= 0.001 || breathScale >= 0.999) {
breathAdjustment = 0 - breathAdjustment;
}
}
setLight(redValue, greenValue, blueValue); setLight(redValue, greenValue, blueValue);
delay(15); delay(30);
} }
/* Helper function to set the colors of the led strip */ /* Helper function to set the colors of the led strip */
void setLight(unsigned char red, unsigned char green, unsigned char blue) { void setLight(unsigned char red, unsigned char green, unsigned char blue) {
analogWrite(redPin, red * globalScale * redScale); analogWrite(redPin, red * globalScale * breathScale * redScale);
analogWrite(greenPin, green * globalScale * greenScale); analogWrite(greenPin, green * globalScale * breathScale * greenScale);
analogWrite(bluePin, blue * globalScale * blueScale); analogWrite(bluePin, blue * globalScale * breathScale * blueScale);
} }
void readFromEEPROM() { void readFromEEPROM() {
unsigned char newMode = EEPROM.read(modeAddress); unsigned char newMode = EEPROM.read(modeAddress);
if (newMode > 1) { if (newMode > 3) {
// Uninitialized cells return 255, so reset that // Uninitialized cells return 255, so reset that
modeValue = 0; modeValue = 0;
} else { } else {