SOLVED - Totaly newb - Running the code at random?

Hi all,

First off let me preface this by saying I am a beginner at coding with Arduino and electronics in general.

I have created a custom fish tank and have used the FastLED library to create a lava flow effect

EDIT

You can see it in action here:

Final working Code

#include <FastLED.h>

#define LED_PIN 2

#define COLOR_ORDER GRB

#define CHIPSET WS2811

#define NUM_LEDS 16

#define SPARKING 80 // was 120

#define COOLING  30 // was 55

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 28 //was 30


boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

bool gReverseDirection = false;

CRGBPalette16 gPal;


CRGB leds[NUM_LEDS];





void setup() {

  delay(3000); // sanity delay

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness( BRIGHTNESS );
  
}




static byte heat[NUM_LEDS];

 
void Fire2012WithPalette()

{


gPal = CRGBPalette16( CRGB::Red, CRGB::Orange, CRGB::Yellow);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}


void loop() {
  if(millis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      FastLED.clear(true);
      start = millis();
      timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
      //timeout = random(1000L *  5, 1000L * 10);// for testing
    }
    else {
      currentlyShowing = true;
      Fire2012WithPalette(); // your code, here
      start = millis();
      timeout = 1000L * 60L * 2; // 2 minutes
      //timeout = 1000L * 5; // for testing

    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);

  }
}

Doing this on every loop:

random16_add_entropy( random());

Doesn't add any entropy at all.

So you want it to shut off completely after ~2 minutes? Then spring to life again at ~2 minutes later? The 'shutdown' effect is going to be more important visually than the actual times. Make it exactly 2 minutes for now and concentrate on making it look good. At some point it's going to have to stop generating new drips of 'lava' and allow the current ones to cool to black.

Your "[edit - link removed - Moderator]" does show a video. But after it wants to know if I'd like to meet up with Asian women?

Watch out.

-jim lee

MorganS: Doing this on every loop:Doesn't add any entropy at all.

So you want it to shut off completely after ~2 minutes? Then spring to life again at ~2 minutes later? The 'shutdown' effect is going to be more important visually than the actual times. Make it exactly 2 minutes for now and concentrate on making it look good. At some point it's going to have to stop generating new drips of 'lava' and allow the current ones to cool to black.

Basically yes, shut if off after 2 mintues, then start again at random between 1 min and an hour.

In terms of the entropy code, I have no idea what it does, I basically edited the demo till I got it working how I liked :(

Your link is corrupted, it diverts to a female contact agency. Just hope it doesn't hold viruses.

Ta, link removed. Who would have though googling 'free video hosting' would have ended up like that. On the plus side...I have met lots of Asian women in my area.

Youtube is free to upload videos.

boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

void loop() {
  if(milis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      blank_the_screen(); // your code, here
      start = millis();
      timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
    }
    else {
      currentlyShowing = true;
      start_the_lava(); // your code, here
      start = millis();
      timeout = 1000L * 60L * 2; // 2 minutes
    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);
  }
}

Thank you so much, that's awesome.

I'm still pretty newb and can't figure out how to get the existing code integrated with the above.

I assume I need to define milis, and create functions for blank_the_screen etc...

any help is greatly appreciated.

Venomouse: I'm still pretty newb and can't figure out how to get the existing code integrated with the above.

I assume I need to define milis, and create functions for blank_the_screen etc...

Oh! Your'e that much of a newb!

Ok. millis is part of the standard library - same as digitalRead and digitalWrite is part of the sthandard library. millis() gives you the number of milliseconds since the arduino was last reset. uint32_t is a standard data type: a 32-bit unsigned integer.

The bits where I said "your code here" are where you put some stuff in. Presumably, you'd use your fastLed doohickey (which I have never used) t display some stuff. You may not need start_the_lava at all if your Fire2012WithPalette() redraws the entire display every time.

Thanks again,

So here’s the updated code.

My leds are running on startup for 2 mins, then they stay light without blinking…ie solid color.

I edited a function ‘Fire2012WithPaletteoff’ and set the leds to black which may not have worked as a band aid / lack of knowledge on how to basically say…turn everything off but keep going to the next step.

#include <FastLED.h>

#define LED_PIN 2

#define COLOR_ORDER GRB

#define CHIPSET WS2811

#define NUM_LEDS 20

#define SPARKING 120

#define COOLING  55

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 30


boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

bool gReverseDirection = false;

CRGBPalette16 gPal;



CRGB leds[NUM_LEDS];





void setup() {

  delay(3000); // sanity delay

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness( BRIGHTNESS );
  
}







void Fire2012WithPaletteoff()

{


gPal = CRGBPalette16( CRGB::Black, CRGB::Black, CRGB::Black);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}






 static byte heat[NUM_LEDS];

 
void Fire2012WithPalette()

{


gPal = CRGBPalette16( CRGB::Red, CRGB::Orange, CRGB::Yellow);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}


void loop() {
  if(millis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      Fire2012WithPaletteoff(); // your code, here
      start = millis();
      timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
    }
    else {
      currentlyShowing = true;
      Fire2012WithPalette(); // your code, here
      start = millis();
      timeout = 1000L * 60L * 2; // 2 minutes
    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);
  }
}

Venomouse: My leds are running on startup for 2 mins, then they stay light without blinking..ie solid color.

And if you wait, ddo they stay a nonblinking solid colour for somewhere between 5 and 10 minutes, and then start blinking again for another 2 minutes?

To test this, change

timeout = random(1000L * 60L * 5, 1000L * 60L * 10);

to

timeout = random(1000L *  5, 1000L * 10);

to get a timeout between 5 and 10 seconds (rather than minutes),

and change

timeout = 1000L * 60L * 2;

to

timeout = 1000L * 5;

to make the leds blink for 5 seconds rather than 2 minutes.

for an explanation of random, see the documentation.

Thank you,

Confirms that the lights start blinking again after the short delay.

I changed my Fire2012WithPaletteoff to different colors to make it easier to show.

It appears it didn’t work.

At the moment with the above testing here is what happens

On power up - Sequence flashes red/yellow/orange as intended

Slight pause - (same colors)

Sequence flashes red/yellow/orange.

Your help has been awesome, I see the light at the end of the tunnel…

#include <FastLED.h>

#define LED_PIN 2

#define COLOR_ORDER GRB

#define CHIPSET WS2811

#define NUM_LEDS 20

#define SPARKING 120

#define COOLING  55

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 30


boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

bool gReverseDirection = false;

CRGBPalette16 gPal;



CRGB leds[NUM_LEDS];





void setup() {

  delay(3000); // sanity delay

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness( BRIGHTNESS );
  
}







void Fire2012WithPaletteoff()

{


gPal = CRGBPalette16( CRGB::Blue, CRGB::Green, CRGB::Black);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}






 static byte heat[NUM_LEDS];

 
void Fire2012WithPalette()

{


gPal = CRGBPalette16( CRGB::Red, CRGB::Orange, CRGB::Yellow);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}


void loop() {
  if(millis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      Fire2012WithPaletteoff(); // your code, here
      start = millis();
      //timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
      timeout = random(1000L *  5, 1000L * 10);
    }
    else {
      currentlyShowing = true;
      Fire2012WithPalette(); // your code, here
      start = millis();
      //timeout = 1000L * 60L * 2; // 2 minutes
      timeout = 1000L * 5;
    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);
  }
}

Update,

I was able to find the turn all LED’s off command so I replaced my ‘off’ function with a simple:

FastLED.clear();

From testing, it would appear in the loop it’s not getting to the first if statement?

Here is a video of what is happening (this time on Youtube.)

Thank you

#include <FastLED.h>

#define LED_PIN 2

#define COLOR_ORDER GRB

#define CHIPSET WS2811

#define NUM_LEDS 16

#define SPARKING 120

#define COOLING  55

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 30


boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

bool gReverseDirection = false;

CRGBPalette16 gPal;


CRGB leds[NUM_LEDS];





void setup() {

  delay(3000); // sanity delay

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness( BRIGHTNESS );
  
}




static byte heat[NUM_LEDS];

 
void Fire2012WithPalette()

{


gPal = CRGBPalette16( CRGB::Red, CRGB::Orange, CRGB::Yellow);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}


void loop() {
  if(millis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      FastLED.clear();
      start = millis();
      //timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
      timeout = random(1000L *  5, 1000L * 10);// for testing
    }
    else {
      currentlyShowing = true;
      Fire2012WithPalette(); // your code, here
      start = millis();
      //timeout = 1000L * 60L * 2; // 2 minutes
      timeout = 1000L * 5; // for testing
    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);
  }
}

Final working for those interested.

#include <FastLED.h>

#define LED_PIN 2

#define COLOR_ORDER GRB

#define CHIPSET WS2811

#define NUM_LEDS 16

#define SPARKING 80 // was 120

#define COOLING  30 // was 55

#define BRIGHTNESS 200

#define FRAMES_PER_SECOND 28 //was 30


boolean currentlyShowing = false;
uint32_t start=0;
uint32_t timeout=0;

bool gReverseDirection = false;

CRGBPalette16 gPal;


CRGB leds[NUM_LEDS];





void setup() {

  delay(3000); // sanity delay

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness( BRIGHTNESS );
  
}




static byte heat[NUM_LEDS];

 
void Fire2012WithPalette()

{


gPal = CRGBPalette16( CRGB::Red, CRGB::Orange, CRGB::Yellow);
// Array of temperature readings at each simulation cell

  static byte heat[NUM_LEDS];

 

  // Step 1.  Cool down every cell a little

    for( int i = 0; i < NUM_LEDS; i++) {

      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));

    }

 

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little

    for( int k= NUM_LEDS - 1; k >= 2; k--) {

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

    }

   

    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom

    if( random8() < SPARKING ) {

      int y = random8(7);

      heat[y] = qadd8( heat[y], random8(160,255) );

    }

 

    // Step 4.  Map from heat cells to LED colors

    for( int j = 0; j < NUM_LEDS; j++) {

      // Scale the heat value from 0-255 down to 0-240

      // for best results with color palettes.

      byte colorindex = scale8( heat[j], 240);

      CRGB color = ColorFromPalette( gPal, colorindex);

      int pixelnumber;

      if( gReverseDirection ) {

        pixelnumber = (NUM_LEDS-1) - j;

      } else {

        pixelnumber = j;

      }

      leds[pixelnumber] = color;

    }

}


void loop() {
  if(millis() - start >= timeout) {
    if(currentlyShowing) {
      currentlyShowing = false;
      FastLED.clear(true);
      start = millis();
      timeout = random(1000L * 60L * 5, 1000L * 60L * 10); // pretty sure random does longs
      //timeout = random(1000L *  5, 1000L * 10);// for testing
    }
    else {
      currentlyShowing = true;
      Fire2012WithPalette(); // your code, here
      start = millis();
      timeout = 1000L * 60L * 2; // 2 minutes
      //timeout = 1000L * 5; // for testing

    }
  }

  if(currentlyShowing) {
    Fire2012WithPalette(); // run simulation frame, using palette colors
    FastLED.show(); // display this frame
    FastLED.delay(1000 / FRAMES_PER_SECOND);

  }
}