[solved] LED Strip random color every 7 seconds

Hey there,
I made myself a music reactive LED string and I’d like to change the color every 7 seconds to a random color. I tryed using random seed and the HSV command (like in the code) but I couldn’t figure out how to do it.
With this code the LED string lights up in some kind of green…
Can you help me please?

My code:

#include <FastLED.h>


#define NUM_LEDS 294


#define DATA_PIN 6

int strobePin  = 13;    // Strobe Pin on the MSGEQ7
int resetPin   = 12;    // Reset Pin on the MSGEQ7
int outPin     = A0;   // Output Pin on the MSGEQ7
int i;
int level [7];

unsigned long timestamp;





CRGB leds[NUM_LEDS];


void setup() {

 delay(2000);
 Serial.begin (9600);
 timestamp = millis();

 
  // Define pin modes
  pinMode      (strobePin, OUTPUT);
  pinMode      (resetPin,  OUTPUT);
  pinMode      (outPin,    INPUT);
 
  // Create an initial state for our pins
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, LOW);
  delay        (1);
 
  // Reset the MSGEQ7 as per the datasheet timing diagram
  digitalWrite (resetPin,  HIGH);
  delay        (1);
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, HIGH);
  delay        (1);



  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
   
}


void loop() {

  for (int i = 0; i < 7; i++) {
    digitalWrite       (strobePin, LOW);
    delayMicroseconds  (100);                    // Delay necessary due to timing diagram
    level[i] =         analogRead (outPin);
    digitalWrite       (strobePin, HIGH);
    delayMicroseconds  (100); 
  }

    int PWMvalue1 = map(level[0], 0, 1024, 0, 255);
    int PWMvalue3 = map(level[3], 0, 1024, 0, 255);
    int PWMvalue2 = map(level[6], 0, 1024, 0, 255);


//###############################################################################################################
if ((millis()- timestamp) > 0 && (millis()-timestamp) < 7000)       //Important part
{
  while (i==0)
  {
  fill_solid( leds, NUM_LEDS, CHSV(random8(),255,255));
  FastLED.show ();
  i++;
  }
}

if (millis() > 7000)
{
  timestamp = millis ();
  i==0;
}


if (PWMvalue1>80)
{
      FastLED.setBrightness (PWMvalue1);
      FastLED.show ();
      delay(15);
}

else 
{
   FastLED.setBrightness (20);
   FastLED.show ();
}



}
The call random8() should be, I think, random8(8).

See

Also you never seed the random number generator.

Thank you for your reply!
I tryed random8(eight), but then the led string just turns and keeps red. Witht he randomSeed I always get kinda green color that never changes too. I think the problem is about something else in my code...

I think you are not understanding how the random seed is used. It is used only once in the setup function. After that you use the random functions.

I thought I did exactly what you wrote in your post…

#include <FastLED.h>


#define NUM_LEDS 294


#define DATA_PIN 6

int strobePin  = 13;    // Strobe Pin on the MSGEQ7
int resetPin   = 12;    // Reset Pin on the MSGEQ7
int outPin     = A0;   // Output Pin on the MSGEQ7
int i;
int level [7];

unsigned long timestamp;





CRGB leds[NUM_LEDS];


void setup() {

 delay(2000);
 Serial.begin (9600);
 timestamp = millis();
 randomSeed (analogRead(5));

 
  // Define pin modes
  pinMode      (strobePin, OUTPUT);
  pinMode      (resetPin,  OUTPUT);
  pinMode      (outPin,    INPUT);
 
  // Create an initial state for our pins
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, LOW);
  delay        (1);
 
  // Reset the MSGEQ7 as per the datasheet timing diagram
  digitalWrite (resetPin,  HIGH);
  delay        (1);
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, HIGH);
  delay        (1);



  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
   
}


void loop() {

  for (int i = 0; i < 7; i++) {
    digitalWrite       (strobePin, LOW);
    delayMicroseconds  (100);                    // Delay necessary due to timing diagram
    level[i] =         analogRead (outPin);
    digitalWrite       (strobePin, HIGH);
    delayMicroseconds  (100);
  }

    int PWMvalue1 = map(level[0], 0, 1024, 0, 255);
    int PWMvalue3 = map(level[3], 0, 1024, 0, 255);
    int PWMvalue2 = map(level[6], 0, 1024, 0, 255);


//###############################################################################################################
if ((millis()- timestamp) > 0 && (millis()-timestamp) < 7000)       //Important part
{
  while (i==0)
  {
  fill_solid( leds, NUM_LEDS, CHSV(random8(8),255,255));
  FastLED.show ();
  i++;
  }
}

if (millis() > 7000)
{
  timestamp = millis ();
  i==0;
}


if (PWMvalue1>80)
{
      FastLED.setBrightness (PWMvalue1);
      FastLED.show ();
      delay(15);
}

else
{
   FastLED.setBrightness (20);
   FastLED.show ();
}



}
CHSV(random8(8),255,255)

I think you might find that the hue parameter should go from 0 to 255 and this looks like it is restricting the number to 0 to 8.

Ok, but how can I fix this? Sorry if I seem to be kinda dense, but I'm more or less new to arduino...

I worked on the code and finally found the solution. I just made it too dificult. Thank you for your effort!

Working code:

#include <FastLED.h>


#define NUM_LEDS 294


#define DATA_PIN 6

int strobePin  = 13;    // Strobe Pin on the MSGEQ7
int resetPin   = 12;    // Reset Pin on the MSGEQ7
int outPin     = A0;   // Output Pin on the MSGEQ7
int i;
int level [7];

unsigned long timestamp;





CRGB leds[NUM_LEDS];


void setup() {

 delay(2000);
 Serial.begin (9600);
 timestamp = millis();
 randomSeed (analogRead(0));

 
  // Define pin modes
  pinMode      (strobePin, OUTPUT);
  pinMode      (resetPin,  OUTPUT);
  pinMode      (outPin,    INPUT);
 
  // Create an initial state for our pins
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, LOW);
  delay        (1);
 
  // Reset the MSGEQ7 as per the datasheet timing diagram
  digitalWrite (resetPin,  HIGH);
  delay        (1);
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, HIGH);
  delay        (1);



  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
   
}


void loop() {

  for (int i = 0; i < 7; i++) {
    digitalWrite       (strobePin, LOW);
    delayMicroseconds  (100);                    // Delay necessary due to timing diagram
    level[i] =         analogRead (outPin);
    digitalWrite       (strobePin, HIGH);
    delayMicroseconds  (100);
  }

    int PWMvalue1 = map(level[0], 0, 1024, 0, 255);
    int PWMvalue3 = map(level[3], 0, 1024, 0, 255);
    int PWMvalue2 = map(level[6], 0, 1024, 0, 255);


//###############################################################################################################

if (millis()-timestamp > 7000)
{
  fill_solid( leds, NUM_LEDS, CHSV(random8(255),255,255));
  timestamp = millis ();

}


if (PWMvalue1>80)
{
      FastLED.setBrightness (PWMvalue1);
      FastLED.show ();
      delay(15);
}

else
{
   FastLED.setBrightness (20);
   FastLED.show ();
   delay(15);
}



}