Hi there,
a few month ago I've started a new Arduino project. My goal was to have indirect lighting behind my shelf wich is sound-responsive. I've realized this by feeding one channel of my analog audio cable from my PC into the Arduino. The Arduino then simply reads out the gain, processes it and controls the LEDs (WS2812b). So far im really satisfied with the outcome. At this point I have 4 sound responsive modes and 6 other (non sound responsive) modes. A few days ago I wanted to create another "Party" mode. This Mode should determine a random time, color and mode, and then run this mode with the color until the time is over.
void partyModeColorChooser(int r) {
switch (r) {
case 0: partyColor = CRGB::Red; break;
case 1: partyColor = CRGB::OrangeRed; break;
case 2: partyColor = CRGB::DarkViolet; break;
case 3: partyColor = CRGB::SeaGreen; break;
case 4: partyColor = CRGB::DarkCyan; break;
default: partyColor = CRGB::Green; break;
}
}
void partyLightAnimationChooser(int lightModeSwitch) {
switch (lightModeSwitch) {
case 0: volumeBreath(partyColor, partyColor); break;
case 1: soundmeter(partyColor, partyColor, CRGB::Blue); break;
case 2: sparkle(partyColor); break;
case 3: soundshooter(partyColor); break;
case 4: randcolorstep();
case 5: inout(partyColor, partyColor); break;
}
}
void partymode() {
unsigned long currentTime = millis();
Serial.print("Time: ");
Serial.print(currentTime);
Serial.print(" - ");
Serial.print(startTime);
Serial.print(" > ");
Serial.println(partyLightModeDuration);
if (currentTime - startTime > partyLightModeDuration) {
startTime = currentTime;
fill_solid(led, NUM_LEDS, CRGB::Black);
int r = random(0, 5);
partyLightMode = random(0, 6);
partyLightModeDuration = random(10000, 60000);
partyModeColorChooser(r);
resetRandStep();
}
partyLightAnimationChooser(partyLightMode);
}
My Problem is now that every time when the lightmode is volumeBreath (sound responsive) and the gain is high enough to light the LEDs, millis() gets reseted back to 0. (see log)
Time: 6457 - 0 > 10000
Time: 6488 - 0 > 10000
Time: 6518 - 0 > 10000
Time: 283 - 0 > 10000 --> here the LEDs light up
Time: 283 - 0 > 10000
Time: 283 - 0 > 10000
...
Time: 1244 - 0 > 10000
Time: 1275 - 0 > 10000
Time: 284 - 0 > 10000 --> here the LEDs light up
Time: 282 - 0 > 10000
...
Time: 1047 - 0 > 10000
Time: 282 - 0 > 10000 --> here the LEDs light up
etc.
This bug prevents the system to change Modes as long music is playing. Thats really strange because I dont use any time related functions in volumeBreath. Thats why i can't seem to find the bug.
void volumeBreath(CRGB::HTMLColorCode color1, CRGB::HTMLColorCode color2) {
int gain = maxOf_From_Samples(3, 4);
if (gain > 450) {
fill_gradient_RGB(led, 0, color1, NUM_LEDS, color2);
delay(20);
} else {
for (int i = 0; i < NUM_LEDS; i++)
led[i].fadeLightBy(24);
}
FastLED.show();
}
Here are the functions that are responsible for reading out the gain (average of some samples and max of a number of averages)
int gainReadout(int anz) {
float sum = 0;
for (int i = 0; i < anz; i++) {
sum += (float)analogRead(LEFT_ANALOG_AUDIO_IN) / (float)anz;
delay(1);
}
return (int)sum;
}
int maxOf_From_Samples(int anz, int samples) {
int maxi = 0;
int temp;
for (int i = 0; i < anz; i++) {
temp = gainReadout(samples);
if (temp > maxi)
maxi = temp;
}
return maxi;
}
Like I already said I cant seem to find the bug. It's also worth mentioning that other sound responsive functions like soundmeter doesn't have this problem. Maybe it has something to do with the FastLED library?
I hope you have some ideas. Thanks in advance.
shelfmodes_refractored.ino (11.8 KB)