Welcome, %1$s. Please login or register. - Did you miss your activation email?

Login with username, password and session length
 

Author Topic: Viper Engines- Ramp up and flicker  (Read 271 times)

Offline whb64

  • Captain
  • ****
  • Posts: 393
Viper Engines- Ramp up and flicker
« on: October 14, 2017, 03:54:42 am »
Hi everyone...

I did do a search but didn't really find what I was looking for.  It could be I had the wrong search parms.  I found sketches to make an LED flicker, but is there one that will ramp it up to full brightness and then go into a flicker mode?

I figure I should add this- I have a bunch of Nano's laying around so for this Viper build I will be using one of them.

I did get some blue flickering LEDs from HDA but they just don't seem bright enough, at least nowhere near as bright as the super brights I bought from LighthouseLED. 
« Last Edit: October 14, 2017, 03:59:35 am by whb64 »

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #1 on: October 14, 2017, 05:12:35 pm »
I found some code for a flickering effect and it does work.  I'd like it to be just a little more pronounced though.

here is the code I found and uploaded to my NANO-

#define FLICKER_LED_PIN 10

// the start of the flicker (low)
static int flicker_low_min = 200;
static int flicker_low_max = 240;

// the end value of the flicker (high)
static int flicker_high_min = 230;
static int flicker_high_max = 256;

// delay between each low-high-low cycle
// low->high |flicker_hold| high->low
static int flicker_hold_min = 40; // milliseconds
static int flicker_hold_max = 80; // milliseconds

// delay after each low-high-low cycle
// low->high->low |flicker_pause| low->high...
static int flicker_pause_min = 100; // milliseconds
static int flicker_pause_max = 200;  // milliseconds

// delay low to high and high to low cycle
static int flicker_speed_min = 900; // microseconds
static int flicker_speed_max = 1000; // microseconds

void setup() {
  pinMode(FLICKER_LED_PIN, OUTPUT);
}

int flicker_random_low_start = 0;
int flicker_random_low_end = 0;
int flicker_random_high = 0;
int flicker_random_speed_start = 0;
int flicker_random_speed_end = 0;

void loop() {

  // random time for low
  flicker_random_low_start = random(flicker_low_min, flicker_low_max);
  flicker_random_low_end = random(flicker_low_min, flicker_low_max);

  // random time for high
  flicker_random_high = random(flicker_high_min, flicker_high_max);

  // random time for speed
  flicker_random_speed_start = random(flicker_speed_min, flicker_speed_max);
  flicker_random_speed_end = random(flicker_speed_min, flicker_speed_max);

  // low -> high
  for (int i = flicker_random_low_start; i < flicker_random_high; i++) {
    analogWrite(FLICKER_LED_PIN, i);
    delayMicroseconds(flicker_random_speed_start);
  }

  // hold
  delay(random(flicker_hold_min, flicker_hold_max));

  // high -> low
  for (int i = flicker_random_high; i >= flicker_random_low_end; i--) {
    analogWrite(FLICKER_LED_PIN, i);
    delayMicroseconds(flicker_random_speed_end);
  }

  // pause
  delay(random(flicker_pause_min, flicker_pause_max));
}

Couple of questions for you experts-

I don't seem to see where the output pin is set in the code.  I know it's pin 10 because that's what he showed on his page, but I don't see where that is defined.   Next thing is I need to run three LEDs for the three thrusters, so I would need three pins.  I don't think I should need to copy the code three times, there should be a way to define the sketch to run on multiple pins correct?  How would I do that in this code?  Third question- what parms would I change to make the flickering effect more pronounced?

And the last question-  Is there a way to initially ramp up the LEDs from dim to full brightness and then they start to flicker.  I'm trying to get the effect of the thrusters starting up and then flickering when fully on.

If I pasted the code wrong please let me know.  I used the insert code option but the code wasn't readable.

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #2 on: October 14, 2017, 05:17:56 pm »
Talking to myself on here lol...  apologies about that.

Okay so I just changed  #define FLICKER_LED_PIN 10 to #define FLICKER_LED_PIN 11 and now pin 11 works.

Is it really that simple?  So now to make three pins work do I just do #define FLICKER_LED_PIN 10 11 12 ?

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #3 on: October 14, 2017, 09:32:05 pm »
Well, giving it a look ...

If you want the flicker more pronounced, try changing the low_min, low_max to something lower and high_min, high_max to something higher.
Maybe low_min=120, low_max=150 and high_min=240, high_max=256 ?

"#define FLICKER_LED_PIN 10" is basically saying "FLICKER_LED_PIN=10", so everywhere there is FLICKER_LED_PIN in the code, it really means 10.
You'd need to have a separate #define and different name for each pin number so something like:

#define FLICKER_LED_PIN1 10
#define FLICKER_LED_PIN2 11
#define FLICKER_LED_PIN3 12

As for a ramp up then flicker, it is probably doable, but it might be a little more complicated (you'd probably have to rewrite the code without using
delayMicroseconds ...)

Let me give this some thought ...

Spencer

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #4 on: October 14, 2017, 10:13:17 pm »
Here is some code, similar to what you posted below, that is a little more descriptive and simple.

Will need to think a bit about adding a fade then flicker. Would you want that after a button push or something ...?


#define LED_FLICKER_PIN 10
//sets the variable "LED_FLICKER_PIN to pin 10, needs to be a pwm pin;

// this is the brightness when the led "dips", will be between lowest_low and highest_low
static int lowest_low=40;
static int highest_low=80;

// this is the brightness when the led "peaks", will be between lowest_hight and highest_high
static int lowest_high=70;
static int highest_high=255;

// time led is at peak, between shortest_on and longest_on
static int shortest_on=40;
static int longest_on=120;

// time led is at dip, between shortest_off and longest_off
static int shortest_off=40;
static int longest_off=120;

void setup() {
  // set pin as output
  pinMode(LED_FLICKER_PIN, OUTPUT);
}

void loop() {
  // get the random brightnesses for on and off
  int low_brightness = random(lowest_low, highest_low);
  int high_brightness = random(lowest_high, highest_high);

  // get the random times for on and off
  int on_time = random(shortest_on, longest_on);
  int off_time = random(shortest_off, longest_off);

  // do the flicker
  analogWrite(LED_FLICKER_PIN,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN,low_brightness);
  delay(off_time);
}
« Last Edit: October 14, 2017, 10:15:52 pm by Spencer »

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #5 on: October 14, 2017, 10:36:30 pm »
This code is the same as I posted, but now has a fade in setup(), so the fade will happen when the code is restarted / reset the Arduino, then will come
the flicker, which is purposefully exaggerated.

You can make it more subtle making the high and low brightnesses more equal and fiddling with the on and off times. It will really be a case of your
aesthetic preferences. But the on and off time ranges should similar and on and off brightnesses should overlap at least a little bit ...

If you want to fade then flicker after a button push or something, that would be a bit more (but not too much more) complicated ...

Anyway ... here's some code!

#define LED_FLICKER_PIN 10
//sets the variable "LED_FLICKER_PIN to pin 10, needs to be a pwm pin;

// this is the brightness when the led "dips", will be between lowest_low and highest_low
static int lowest_low=40;
static int highest_low=80;

// this is the brightness when the led "peaks", will be between lowest_hight and highest_high
static int lowest_high=70;
static int highest_high=255;

// time led is at peak, between shortest_on and longest_on
static int shortest_on=40;
static int longest_on=120;

// time led is at dip, between shortest_off and longest_off
static int shortest_off=40;
static int longest_off=120;

// time to fade up, in milliseconds
static int fade_time=1500;
static int step_time = int(float(fade_time/255));

void setup() {
  // set pin as output
  pinMode(LED_FLICKER_PIN, OUTPUT);
 
  // fade up from off to max
  for (int i=0; i<255; i++){
    analogWrite(LED_FLICKER_PIN, i);
    delay(step_time);
  }
}



void loop() {
  // get the random brightnesses for on and off
  int low_brightness = random(lowest_low, highest_low);
  int high_brightness = random(lowest_high, highest_high);

  // get the random times for on and off
  int on_time = random(shortest_on, longest_on);
  int off_time = random(shortest_off, longest_off);

  // do the flicker
  analogWrite(LED_FLICKER_PIN,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN,low_brightness);
  delay(off_time);
}
« Last Edit: October 14, 2017, 10:40:59 pm by Spencer »

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #6 on: October 14, 2017, 10:39:56 pm »
Thanks Spencer, I'll try your sketch.

I did try adding
#define FLICKER_LED_PIN1 10
#define FLICKER_LED_PIN2 11
#define FLICKER_LED_PIN3 12

But I see I didn't change it from PIN1, so that's why it didn't work.

_________________________________
Okay I added the above to the sketch I am using and I get this error when compiling:  'FLICKER_LED_PIN' was not declared in this scope'

I'm trying your sketch

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #7 on: October 14, 2017, 11:03:22 pm »
This code is the same as I posted, but now has a fade in setup(), so the fade will happen when the code is restarted / reset the Arduino, then will come
the flicker, which is purposefully exaggerated.

You can make it more subtle making the high and low brightnesses more equal and fiddling with the on and off time. It will really be a case of your
aesthetic preferences.

If you want to fade then flicker after a button push or something, that would be a bit more (but not too much more) complicated ...

Anyway ... here's some code!

:)

That's getting a lot closer to what I am looking for.  Maybe a little longer on the ramp up and just a little less on the flicker.  Which do I adjust for those parms?

Oh, to add pins 11 and 12 do I just add this?
#define FLICKER_LED_PIN1 10
#define FLICKER_LED_PIN2 11
#define FLICKER_LED_PIN3 12

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #8 on: October 14, 2017, 11:19:45 pm »
Glad to help  ;D

To increase the ramp time, just make fade_time bigger. Right now it's 1.5 seconds (1500 milliseconds).

For less flicker, make the off times shorter, on times longer, or both. You'll have to fiddle with it till you get what you like.

For adding more pins, do you want them all to flicker the same or each be random on their own? In either case, you'd have to
do a bit more than just add the extra define statements ...

If you want them all to ramp and flicker the same, you'll need to duplicate analogWrites for each pin, for example:
analogWrite(LED_FLICKER_PIN1,high_brightness);
analogWrite(LED_FLICKER_PIN2,high_brightness);
analogWrite(LED_FLICKER_PIN3,high_brightness);

If you want them flickering all differently, then in loop(), you'd have to repeat the random statements and set on times, off times, high and low brightnesses
for each separate pin. Not hard, but definitely some bookkeeping.

Spencer
« Last Edit: October 14, 2017, 11:21:47 pm by Spencer »

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #9 on: October 14, 2017, 11:33:02 pm »
So something like this?

 // do the flicker
  analogWrite(LED_FLICKER_PIN1,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN1,low_brightness);
  delay(off_time);
  analogWrite(LED_FLICKER_PIN2,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN2,low_brightness);
  delay(off_time);
  analogWrite(LED_FLICKER_PIN3,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN3,low_brightness);
  delay(off_time);

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #10 on: October 14, 2017, 11:43:10 pm »
So something like this?

 // do the flicker
  analogWrite(LED_FLICKER_PIN1,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN1,low_brightness);
  delay(off_time);
  analogWrite(LED_FLICKER_PIN2,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN2,low_brightness);
  delay(off_time);
  analogWrite(LED_FLICKER_PIN3,high_brightness);
  delay(on_time);
  analogWrite(LED_FLICKER_PIN3,low_brightness);
  delay(off_time);

Almost  ;)


in the setup() code:

for the outputs, use:
pinMode(LED_FLICKER_PIN1, OUTPUT);
pinMode(LED_FLICKER_PIN2, OUTPUT);
pinMode(LED_FLICKER_PIN3, OUTPUT);

and for the ramp up, use:
for (int i=0; i<255; i++){
    analogWrite(LED_FLICKER_PIN1, i);
    analogWrite(LED_FLICKER_PIN2, i);
    analogWrite(LED_FLICKER_PIN3, i);
    delay(step_time);
  }

Then in loop() use:
analogWrite(LED_FLICKER_PIN1,high_brightness);
analogWrite(LED_FLICKER_PIN2,high_brightness);
analogWrite(LED_FLICKER_PIN3,high_brightness);
delay(on_time);
analogWrite(LED_FLICKER_PIN1,low_brightness);
analogWrite(LED_FLICKER_PIN2,low_brightness);
analogWrite(LED_FLICKER_PIN3,low_brightness);
delay(off_time);

Basically everywhere you have LED_FLICKER_PIN in the original code, change it to three duplicate statements for each separate pin. This
will make them all blink the same.

Time for bed ...

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #11 on: October 15, 2017, 12:38:13 am »
Spencer I got it working for all three thrusters!  Thank you very much!

Your code is pretty easy to vary the parms as you instructed.  I will also be using this for my Cylon engines and thrusters for Klingon kits :)

I assume // is basically a the same as a rem statement or comment correct?  I would like to include this with my WIP threads and I want to add a line crediting you if you don't mind. :)

Offline Spencer

  • Physicist and Astronomer
  • Admiral
  • *****
  • Posts: 585
  • "Well ... let's play this out logically then ..."
    • My Google+ Page
Re: Viper Engines- Ramp up and flicker
« Reply #12 on: October 15, 2017, 06:51:07 am »
Your welcome!

Yes, // will treat all text on that line as a comment (not to execute).

Feel free to use the code on your threads!

Cheers,
Spencer

Offline texastony1946

  • Ensign
  • *
  • Posts: 24
Re: Viper Engines- Ramp up and flicker
« Reply #13 on: October 16, 2017, 06:25:29 am »
Thanks for this. I have worked on the fade flicker for some time with no success. I keep running into timing issues. I'm trying to do it without using the delay function and use milliseconds instead.  When you finalize your code, please post it here. Thanks.

Offline whb64

  • Captain
  • ****
  • Posts: 393
Re: Viper Engines- Ramp up and flicker
« Reply #14 on: October 16, 2017, 11:54:20 am »
Thanks for this. I have worked on the fade flicker for some time with no success. I keep running into timing issues. I'm trying to do it without using the delay function and use milliseconds instead.  When you finalize your code, please post it here. Thanks.
This is the final code I ended up with based on what Spencer provided

#define LED_FLICKER_PIN1 9
#define LED_FLICKER_PIN2 10
#define LED_FLICKER_PIN3 11
//sets the variable "LED_FLICKER_PIN to pin 10, needs to be a pwm pin;

// this is the brightness when the led "dips", will be between lowest_low and highest_low
static int lowest_low=40;
static int highest_low=80;

// this is the brightness when the led "peaks", will be between lowest_hight and highest_high
static int lowest_high=70;
static int highest_high=255;

// time led is at peak, between shortest_on and longest_on
static int shortest_on=40;
static int longest_on=120;

// time led is at dip, between shortest_off and longest_off
static int shortest_off=40;
static int longest_off=120;

// time to fade up, in milliseconds
static int fade_time=5500;
static int step_time = int(float(fade_time/255));

void setup() {
  // set pin as output
    pinMode(LED_FLICKER_PIN1, OUTPUT);
    pinMode(LED_FLICKER_PIN2, OUTPUT);
    pinMode(LED_FLICKER_PIN3, OUTPUT);
 
  // fade up from off to max
  for (int i=0; i<255; i++){
    analogWrite(LED_FLICKER_PIN1, i);
    analogWrite(LED_FLICKER_PIN2, i);
    analogWrite(LED_FLICKER_PIN3, i);
    delay(step_time);
  }
}



void loop() {
  // get the random brightnesses for on and off
  int low_brightness = random(lowest_low, highest_low);
  int high_brightness = random(lowest_high, highest_high);

  // get the random times for on and off
  int on_time = random(shortest_on, longest_on);
  int off_time = random(shortest_off, longest_off);

  // do the flicker
    analogWrite(LED_FLICKER_PIN1,high_brightness);
    analogWrite(LED_FLICKER_PIN2,high_brightness);
    analogWrite(LED_FLICKER_PIN3,high_brightness);
    delay(on_time);
    analogWrite(LED_FLICKER_PIN1,low_brightness);
    analogWrite(LED_FLICKER_PIN2,low_brightness);
    analogWrite(LED_FLICKER_PIN3,low_brightness);
    delay(off_time);
}

I also used an LED array I took from a $1 flashlight I got at Walmart.  The array has eight 5mm LEDs and there is some sort of small chip or electronic on the board that seems to be a current limiter.  The array acts as a single LED when you wire it up and when running on 3VDC it is super bright.  The flashlight runs on three AAA batteries for a total of 4.5VDC so 3VDC is well within its limits.  I will show a break down of the flashlight parts you get when you tear it apart in my WIP thread.  For $1 you get a lot of really good parts!  As mentioned you get the LED array, three AAA batteries, the battery pack, a small on/off switch, a round housing for the LED array and other parts.  I'm sure this can be used for a lot of different lighting purposes and if you are careful I don't see why you couldn't replace the bright white LEDs with different colored ones. 

I have a lot of pictures and videos to upload for my WIP build, I've just been busy with work and haven't had time to do an update.

This code brings up the LEDs for the thruster engines from dim to full brightness and then cycles into the random flicker.  It has a really nice effect.

 




Enterprise © Bloc