Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



LED Effects - Star ...
 
Share:
Notifications
Clear all

[Solved] LED Effects - Star Trek Phaser Array

223 Posts
14 Users
33 Likes
82.4 K Views
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

Thanks for posting the code ... but you may have overlooked something in my earlier reply:

Posted by: @hans

At first glance I see there is no index used with the "leds" array.

For example:

leds = CRGB(red, green, blue);

Should be something like:

leds[i] = CRGB(red, green, blue);

 

 


   
ReplyQuote
(@anxvfs)
New Member
Joined: 6 years ago
Posts: 1
 

Please help! I tried all of these on my Nano and all the lights stay on all the time I tried all of your suggestions in this forum and they still dont look like these video's:

Just another attempt to post the videos, this time embedded:

https://www.tweaking4all.com/wp-content/uploads/2020/01/startrek-phaser-normal-1.mp4

https://www.tweaking4all.com/wp-content/uploads/2020/01/startrek-phaser-diffuse-1.mp4


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

@anxfs - Could you post your code?


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  
Posted by: @trace

For the Warp Jump: Yes, the LED´s are set to a blue color, increase brightness to an almost white AND we have then a white short flash and decreasing brightness from almost white to steady blue again.

Almost a year later (apologies for that), here an initial stab at the warp effect.
I went a little overboard with this one, and added tons of parameters to play with.

I created a function you can call (StarTrekWarp), which takes quite a few parameters.
I hope the documentation in the code is helpful.

Good to know: when fading to a color, you always need a stepsize (the amount a color decreases/increases per step) and a stepdelay (brief delay before doing the next step).

Since a few things are done repeatedly, I've created 3 functions:

- warpFlash; this shows the flash, I just made it function to keep the steps more readable 
- FadeOrInstanToColor: this instantly or fades to a target color
- fadeToColor: this fades from one color to another.

These functions fadeTowardColor and nblendU8TowardU8 were borrowed from Kriegsman's GitHub example.

 

Here a little video (sorry I couldn't hold my phone more stable).

I've just used some random numbers and color - fine tune towards your needs 😀 

Hope this works for you.

 

Here the INO file (sketch):

 

And here the full code:

 

#define FASTLED_INTERNAL    // just used to mute the Pragma messages when compiling
#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

void loop() 
{
  StarTrekWarp(CRGB(0,0,255), 1000, true, 10, 10,  // Initial start color and hold time, fade to inital color with stepsize and stepdealy
               CRGB(50,50,50), 20, 40,            // bright to fade to, with stepsize and stepdelay
               CRGB(255,255,255), 500, false, 0,   // flash color, flash duration, before/after black and black durationr
               30, 100,                            // fade back to previous color stepsize and delay, 
               true, true, 1000);                  // end with black, fade to black, how long to wait before going to black

  delay(10000);
} 

void StarTrekWarp(CRGB initialColor, int holdInitialColor, bool fadeToInital, int fadetoInitialStepSize, int fadetoInitialDelay, 
                  CRGB brightColor,  int brightStepSize, int brightDelay, 
                  CRGB flashColor,   int flashDuration,  bool blackbetweenFlash, int prepostFlashDelay, 
                  int backtoInitalStepSize, int backtoInitialDelay,
                  bool endWithBlack, bool fadeToBlack, int holdinitialColorTime) 
{  
  CRGB tmpColor=initialColor;
  
  //  Step 1 - Set or fade, and hold, initialColor - if fade is true, then fade in to initial color
  FadeOrInstanToColor(CRGB::Black, initialColor, fadetoInitialStepSize, fadetoInitialDelay, fadeToInital);
  
  delay(holdInitialColor);
  tmpColor=initialColor;
  
  //  Step 2 - fade to brightColor
  tmpColor = fadeToColor( tmpColor, brightColor, brightStepSize, brightDelay);

  //  Step 3 - flash
  warpFlash(flashColor, blackbetweenFlash, prepostFlashDelay, flashDuration, false, 20, 5);

  // back to previous color from before the flash
  fill_solid( leds, NUM_LEDS, tmpColor ); 
  FastLED.show();

  //  Step 4 - Finish fade to initialColor and to black if set
  tmpColor = fadeToColor( tmpColor, initialColor, backtoInitalStepSize, backtoInitialDelay);
  
  // if end=black, then use holdinitialColorTime as a delay before going to black, fade to black if selected (fade uses fade in values of initial color)
  if(endWithBlack) 
  {
    delay(holdinitialColorTime);
    FadeOrInstanToColor(initialColor, CRGB::Black, fadetoInitialStepSize, fadetoInitialDelay, fadeToBlack);
  }
}

void warpFlash(CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay)
{
  // FLASH
  // temporary black if blackInBetween=true
  if(blackInBetween) {
    fill_solid( leds, NUM_LEDS, CRGB::Black ); 
    FastLED.show();
    delay(blackDelay);
  }

  // Flash color either instant or with fade
  if(InstantFlash)
  {
    fill_solid( leds, NUM_LEDS, flashColor ); 
    FastLED.show();
    delay(flashDuration);
  }
  else
  {
    CRGB tmpColor = leds[0];     // copy current color
    CRGB previousColor = leds[0];
    
    tmpColor = fadeToColor( tmpColor, flashColor,    fadeStepsize, fadeDelay);    // fade to flash color
    tmpColor = fadeToColor( tmpColor, previousColor, fadeStepsize, fadeDelay); // fade back to start color
  }
  
  // temporary black if blackInBetween=true
  if(blackInBetween) {
    fill_solid( leds, NUM_LEDS, CRGB::Black ); 
    FastLED.show();
    delay(blackDelay);
  }  
}

void FadeOrInstanToColor(CRGB StartColor, CRGB StopColor, int StepSize, int SpeedDelay, bool FadeToColor)
{
  if(FadeToColor)
  {
    fadeToColor( StartColor, StopColor, StepSize, SpeedDelay);
  } 
  else
  {
    fill_solid( leds, NUM_LEDS, StopColor ); 
    FastLED.show();
  }
}

CRGB fadeToColor( CRGB StartColor, CRGB StopColor, int StepSize, int SpeedDelay)
{
  CRGB tmpColor=StartColor;
  
  do {
    tmpColor = fadeTowardColor(tmpColor, StopColor, StepSize);
    fill_solid( leds, NUM_LEDS, tmpColor ); 
    FastLED.show();
    delay(SpeedDelay);
  } while (tmpColor!=StopColor); 

  return tmpColor;
}

// Functions from Kriegsman example
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
  nblendU8TowardU8( cur.red,   target.red,   amount);
  nblendU8TowardU8( cur.green, target.green, amount);
  nblendU8TowardU8( cur.blue,  target.blue,  amount);
  return cur;
}

// function used by "fadeTowardColor"
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
  if( cur == target) return;
  
  if( cur < target ) {
    uint8_t delta = target - cur;
    delta = scale8_video( delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video( delta, amount);
    cur -= delta;
  }
}</ws2811,>

 


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

Note:

As you can see I've added the option to fade in to the initial color when the warp starts, but you can also have it instant (set fade=false).
The same at end, where either the color remains blue, or switches to black, or fades to black.

The fading from blue -> white and from white -> blue (before and after the flash) can be tunes as well.
Ditto for the flash.

Enjoy 😉  


   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans
Hi Hans, this year is not a good year. So I really had no time to anything with Arduino. There for I did not wanted to push you further into writing Codes and so on. I didn´t know that just mentioning the Warp effect turns into getting a full code from you just a day after. Maybe I will have some time to play with the code on weekend.
Thanks a lot. And I hope you feel good and healthy.

Björn


   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans I know I have said weekend, but couldnt wait and so i have tried your code today haha

I have some issues. Since the beginning of this year, I have forgotten a lot about Arduino. So I thought I just load up your ino and it´s good to go. Well, it starts with blue, fades into some kind of violet and jumps to a yellowish flickering white and stays like that. Did I forgot something?

In the line // Step 3 - flash...... I have seen some parameters which I think shouldnt be there - false, 20, 5 ---- cause I have to set everything only in loop. Am I right?

To set Arduino to the side for almost a year wasn´t a good idea for me :(


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

You're most welcome.

No worries Trace! This year has been not fun indeed.
And I totally get it that you (just like myself) do not always have the time to work on the projects we'd love to work on 😉 

This goes for me as well, soi WHEN I setup all my Arduino stuff to work with, it becomes easier to write some code 😁 

 

First note: the line in the void setup() for setting up the LED strip, somehow gets mangled by the forum - I corrected it, and it would be best to download the INO (in case you had not done this.
The correct line is:

FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

Now this line you may want to compare with previously working projects - just to make sure all the settings are right.
For example, my strip seems to like GRB (green red blue) as a color order. Your strip may use RGB (red green blue) instead.
This will indeed trigger unexpected colors.

Also: don't forget the NUM_LEDS and PIN (you probably did that already).

 

As for step 3 being correct: yes that line is correct:

warpFlash(flashColor, blackbetweenFlash, prepostFlashDelay, flashDuration, false, 20, 5);

as the function is defined as:

void warpFlash(CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay)

The last 3 parameters are intended to disable fading (so it will do an instant color change), and if fading is used to indicate speed and steps in how fast the color needs to fade.

I found flashing by instant on/off not so great, so since I had already written a function for fading, I figured I'd give that a try and it looked better (in my opinion).
But since we all look at things differently, I figured just adding the option, so Trace can pick what he likes 😁 


   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans

You are right, I already did everything you have mentioned. And I also tested other codes to see if everything is set alright and older codes work fine. But it still fades from blue to violet to a bright flickering yellowish white and stays on with that color.

I also don´t understand why I have to set up some paramter for the flash in Void Loop and some I have to set in Step 3 - flash. The flash one is the only one I have to set up paramters in two different locations. That confuses me.

And I tested out different colors and timings but the effect described above is the same, just with other colors and timings.

Have a nice weekend!


   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans Hello Hans, I have tried different things on my own but it just won´t work :( But it must have worked due to your video of the effect. Any ideas? 


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

Hi Trace,

sorry for the late response 😊 

As for odd colors: do you have the setup line for your other projects (that do display the correct colors)?
Maybe we can try one of those, you'd only have to make sure the LED count is correct.

Maybe the "setCorrection" part needs to be changed for you strip - but I'm sure you'd seen that in other projects to be significant..
There are quite a few variations (Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc, HighNoonSun, DirectSunlight, OvercastSky,
ClearBlueSky, WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent, FullSpectrumFluorescent , GrowLightFluorescent, BlackLightFluorescent, MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium, UncorrectedTemperature) and I honestly have never tested any of these. I'm sure the will influence the colors though.

You could even leave setCorrection off to see what that does:

FastLED.addLeds<WS2811, PIN, RGB>(leds, NUM_LEDS);

or something like this:

FastLED.addLeds<NEOPIXEL, PIN, RGB>(leds, NUM_LEDS);

(note the WS2811 vs NEOPIXEL - the type of strip you're using, I couldn't find a good reference for this, but here you'll see several examples in the code)

 

Apologies if the parameters for the flash are confusing.
Technically I did not have to make that a function, but it makes the overview in the void loop() easier to read.

The parameters for the Flash are thought to have a more refined control over how the flash works.
I found that just turning the LEDs on (white) and off again did not do it any justice. You can still do that though. Maybe I could have put them in a different order.

So when using all options, the flash works something like this:

  1. Original color, say blue for argument sake, before we call the function.
  2. optional: set LEDs to black for a defined time
  3. Instantly or fade from the original color (or black) to the flash color and keep the flash on for a given time.
    With fade it will fade back to the previous color, or black, at the same speed.
  4. optionally: set LEDs to black again for a defined time (same as #2)

Since turning LEDs on goes pretty fast, I found a very short fade before and after to be helpfull. But that is all option.

So for just a simple white 100ms flash, only the bold values play a role, the other ones are just arbitraty numbers, since we have to pass womething to the function.:

warpFlash( CRGB(255,255,255), false, 0, 100, true, 0, 0);

 

I'll go through the parameters, and maybe they are little much, or you may find them useful.

void warpFlash(CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay)

CRGB flashColor determines the color of the main flash. So in most cases white, eg. CRGB(255,255,255).
FlashDuration determines how long the main flas will show.

 

void warpFlash(CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay)

InstantFlash, if set to true, will jump straight to the color you have defined for the main flash.
However, if set to false, it will gradually go to that color, based on fadeStepsize and fadeDelay (used before reaching the defined color and when going back to the original color).

 

void warpFlash(CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay)

The black in between option is to offer a higher contrast between the color before the flash and the flash itself.
If not added, you'd get a simple LEDs ON and OFF effect.
Enabled however, depending on the blackDelay, it will go to black for a certain time before firing the flash.


   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans

Hi Hans,

it is a tough week, Im glad only one day to go. So your reply time is fine.
I think im still confused with the order of functions or parameters.

For example in loop:



CRGB(255,255,255), 500, false, 0, 30, 100, true,

true, 1000);

 

 

So in Void StarTrekWarp it says those values stand for:



CRGB flashColor, int flashDuration, bool blackbetweenFlash, int prepostFlashDelay, int backtoInitalStepSize, int backtoInitialDelay, bool endWithBlack,

bool fadeToBlack, int holdinitialColorTime

 

Till here, i understand for what the values stand for (what is prepostFlashDelay?)

But in Void WarpFlash we have this:



CRGB flashColor, bool blackInBetween, int blackDelay, int flashDuration, bool InstantFlash, int fadeStepsize, int fadeDelay

 

I can´t find the connection between those in my head.

The setup line from other projects is not different to this one. It is the same like in the StarTrek Phaser Code.

No matter what I change in your code, the effect is still the same. I don´t even get a flash. It is really just blue turning to a flickering yellow with no change after then. It´s not going black again or fade back to blue or anything.

The weird thing is, that every other code from you is working, so I can exclude a library or setup problem.


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

Hi Trace 😊 

Yeah it not just a though week 😉 
I pressed a wrong button so it posted before I finished. Let me try again.


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

Trying it again:

FadeOrInstanToColor           StarTrekWarp
flashColor                ->  flashColor            -> Color of the Flash
blackInBetween            ->  blackbetweenFlash     -> switch LEDs to black before and after Flash (True/False) 
blackDelay                ->  prepostFlashDelay     -> delay before and after the Flash 
flashDuration             ->  flashDuration         -> how long the flash will last
InstantFlash                                        -> Instant Flash (true) or Fade in/out Flash (false)
fadeStepsize                                        -> Fade in/out step size for the Flash (if not InstantFlash=true)
fadeDelay                                           -> Delay in Flash fade steps (if not InstantFlash=true)

The last 3 are not linked to a parameter in StarTrekWarp, maybe I should have added those to the function call.

prepostFlashDelay is the delay before and after the flash (the same value for both).

I'm still not sure why the colors are wrong ...?
You could try just using the flash, something like this:

void loop() {
  fill_solid( leds, NUM_LEDS, CRGB::Black ); 
FastLED.show(); // all LEDs OFF/Black

// Instant white flash for 500 ms
warpFlash(CRGB::White, false, 0, 500, true, 0, 0);

// 5 second delay before repeating this delay(5000); }

   
ReplyQuote
(@trace)
Estimable Member
Joined: 4 years ago
Posts: 170
 

@hans

Hi Hans,

sometimes I kinda feel stupid.

The whole time I have tested the code while the Arduino is still connected to USB. Today I uploaded the code, disconnected the Arduino and used a external power source (battery). And it worked....... 🙄 

Then I just reduced the amount of LED´s in the code and now it also works while it is still connected to USB.

So it was just a power consumption issue. Blame me.

Since then I was able to play with all of your nice customisable parameters. But one thing is missing.  I know, I know, I always want more to fit my needs 🤣 

.....brightness control.....

It´s no problem for me to control the over all brightness. And I figured out where to put a piece of code to have a dimmer color and a bright flash.

But I can´t figure out how to have an over all brightness of (let´s say) 50. Then fading brighter and brighter to (let´s say 150) and then have a flash of full brightness and fading back to (let´s say) 50.
It would be a really nice effect to still be able to choose one steady color, a fading color and a flash color.

Adding a push button to activate the effect should not be a problem for me.

At the end it will be so that the light is always on and only when i press a button it will trigger the effect for ramping up, flash, ramping down.

Have a nice weekend.


   
ReplyQuote
Page 7 / 15
Share: