Alrighty then … it took me a lot more time to find all my stuff, write the code and studying the YouTube video, I did have a lot of fun with it … so here we go.
So I made it configurable, so you can modify it relatively easy for your purposes.
The function “startrek_phaser” takes the following parameters:
PhaserRed, PhaserGreen, PhaserBlue: define the color of the LEDs (0xff, 0x90, 0 = orange).
TargetLED: The LED at what position needs to be “fired” (use “-1” for a random position).
PhaserWidth: the number of LEDs used to “walk” to the aiming position. 4 seemed nice.
ShootingWidth: the number of LEDs used when “firing” and here 3 or 4 seems nice.
msAimDelay: the milliseconds delay between each step when “walking” to the firing position (less = faster), 10 seemed good here.
PulsateAmount: how many times the phaser needs to pulse when firing – I picked 20.
msPulsateDelayBright: the milliseconds delay while “firing” before going to a darker color (less = shorter), 60 seemed good.
msPulsateDelayDarker: the milliseconds delay while “firing” before going to a brighter color (less = shorter), 40 seemed good.
Since you may want to tweak this as well, I’ve defined 3 values (didn’t want to add even more parameters to the function);
DimmingPhaserAim: the amount the outer LEDs will dim while aiming
DimmingShoot: the amount the outer LEDs will dim when firing
DimmingPulsing: the amount the outer LEDs will dim when pulsating while firing.
Note: When the phaser is made of an odd number of LEDs (for example “3”), then the middle LED will be the bright one.
When the [haser is made of an even number of LEDs (>3) then the middle 2 LEDs will be bright.
I’ll try to post 2 videos (I lack the super cool Enterprise model), one normal and one diffused (I just held some toilet paper above it).
So here my code, let me know what you think …
#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6
void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
randomSeed(analogRead(0)); // for a better random
}
void loop()
{
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40); // TargetLED = -1 = random pick
delay(1000); // delay between phase shots
}
void startrek_phaser(byte PhaserRed, byte PhaserGreen, byte PhaserBlue, int TargetLED,
byte PhaserWidth, byte ShootingWidth, int msAimDelay,
int PulsateAmount, int msPulsateDelayBright, int msPulsateDelayDarker)
{
// These define how much the outer LEDs sim during Aiming, Shooting and Pulsating
#define DimmingPhaserAim 200
#define DimmingShoot 128
#define DimmingPulsing 128
int PhaserHalf = PhaserWidth/2; // division will take integer part: 3/2 = 1.5 -> 1, 4/2 = 2 -> 2
int PhaserCenterWidth = 2 - (PhaserWidth%2); // odd numbers: 1, even numbers: 2
int PhaserAim;
if(TargetLED==-1)
{
PhaserAim = random(NUM_LEDS); // Pick random aim point
if(PhaserAim<PhaserHalf) // just making sure we do not go out of the range of LEDs
{
PhaserAim = PhaserWidth;
}
else if(PhaserAim+PhaserHalf>NUM_LEDS)
{
PhaserAim = NUM_LEDS-PhaserHalf;
}
}
else
{
PhaserAim = TargetLED;
}
int StepsLeftside = PhaserAim; // 0 - PhaserAim
int StepsRightside = NUM_LEDS-PhaserAim; // PhaserAim - NUM_LEDS
int maxSteps = max( StepsLeftside, StepsRightside );
int LEDPos;
// move LEDs from outside to phaser position
for(int counter=0; counter<=maxSteps; counter++)
{
setAll(0,0,0); // set all LEDs dark
// Left side towards aim point
LEDPos = PhaserAim-maxSteps+counter;
if( LEDPos >= 0 ) {
for (int PhaserBlock = 0; PhaserBlock<PhaserWidth; PhaserBlock++)
{
if(LEDPos+PhaserBlock>0)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPhaserAim );
}
}
}
}
// Right side towards aim point
LEDPos = PhaserAim+maxSteps-counter;
if( LEDPos < NUM_LEDS ) {
for (int PhaserBlock = 0; PhaserBlock<PhaserWidth; PhaserBlock++)
{
if(LEDPos+PhaserBlock<NUM_LEDS)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPhaserAim );
}
}
}
}
FastLED.show();
delay(msAimDelay);
}
// pulsing LEDs when firing phaser
LEDPos = PhaserAim;
PhaserHalf = ShootingWidth/2; // division will take integer part: 3/2 = 1.5 -> 1, 4/2 = 2 -> 2
PhaserCenterWidth = 2 - (ShootingWidth%2); // odd numbers: 1, even numbers: 2
setAll(0,0,0); // set all to black since shooting width may be different than aiming width
for(int counter=0; counter<PulsateAmount; counter++) {
// Set phaser at aim position to the usual brightness
for (int PhaserBlock = 0; PhaserBlock<ShootingWidth; PhaserBlock++)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingShoot );
}
}
FastLED.show();
delay(msPulsateDelayBright);
// Make the outer LEDs pulsate (not the center LED or LEDs)
for (int PhaserBlock = 0; PhaserBlock<ShootingWidth; PhaserBlock++)
{
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPulsing );
}
}
FastLED.show();
delay(msPulsateDelayDarker);
}
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
leds[i] = CRGB(red, green, blue);
}
FastLED.show();
}