Hi Trace,
glad to hear you like the code
Like I said: it was a fun little project, and if fully functional, maybe I'll add it to the LED effects article.
As for your questions;
1) Dark laser after pulsing effect
I did not think about that haha ...
The best place would be to add "setAll(0,0,0);" at the end of the "startrek_phaser()"" function.
So:
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
...
setAll(0,0,0); // <-- add this just before the last accolade
}
2) Where to put the push button
Well, the best way would be in the "void loop()" function - this is where you'd usually do things.
Start by pretending the "startrek_phaser()" function code does not exist and implement a push button activity as you'd normally would do.
Then when the button gets pushed, call the "startrek_phaser()" function 3 times, or make a short for-loop that calls "startrek_phaser()" function 3 times.
For example, start with this push-button example for the Arduino and slightly modify it to respond to a button push (seeing the button being released as the "push"), so you'll get something like the code below. Note: I removed the ledPIN from that example, since we do not use it here.
...
// constants won't change. They're used here to set pin numbers:
#define buttonPin 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
...
void setup() {
...
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn phaser on 3x:
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
}
}
...
So all combined and untested (bold is what I have added) you should get something like this:
#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6
#define buttonPin 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
randomSeed(analogRead(0)); // for a better random
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn phaser on 3x:
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 4, 10, 20, 60, 40);
}
}
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);
}
setAll(0,0,0);
}
// 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 = CRGB(red, green, blue);
}
FastLED.show();
}
Hope this helps!
Are you actually having or building a Enterprise model?