Quantcast
Channel: Tweaking4All.com » All Posts
Viewing all articles
Browse latest Browse all 142

Reply To: LED Effects – Star Trek Phaser Array

$
0
0

Hi Hans,

again all I can say is THANK YOU!

I
really like your structure of Loop and Void. Have the function in a void
and call it in the loop, makes the pushbutton function easy.
Yesterday
I was trying to add the pushbutton within the void and failed. I´m
still thinking too difficult to get the right solutions.
But your replies are really good stuff to learn from.

In your code was a little syntax error ( ; after define pushbutton). But then it worked flawless.

I also added brightness ( less brightness is better for testing or it will burn your eyes :D ) and put a lil delay after every shoot (changes are bold). Now it is spot on.

#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 (removed syntax error ; )
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
  FastLED.setBrightness(20);
  // 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, 3, 10, 20, 60, 40);
    startrek_phaser( 0xff, 0x90, 0, -1, 4, 3, 10, 20, 60, 40);
    startrek_phaser( 0xff, 0x90, 0, -1, 4, 3, 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);
  delay(500);
}

// 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();
}

And you helped me more than I could ever expect. THANKS!!!

Depending on how much leisure time I will have this year, I´m going to start building the Enterprise D from AMT in 1/1000.

If you want to see some of my projects, you can just take a look at my youtube channel:

Trace Berlin


From now on, I will mention you and your forum in my next building videos.


Viewing all articles
Browse latest Browse all 142

Trending Articles