SciFi Model Action

The Arduino Exchange => The Arduino Exchange => Topic started by: Grumpy Popeye on February 24, 2017, 04:15:42 pm

Title: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 24, 2017, 04:15:42 pm
Hi Guys,

I'm working on my Venator Space triangle and I'm trying to work out a decent sketch to have the engine lights do a fast flicker (looks almost steady on, but you can detect a bit of a pulse)

My source voltage is going to be 12V,  the ship has 8 engines that will be connected in pairs, so four outputs. I've added a quick drawing of the layout.



My current sketch has four outputs on delays to simulate a flicker, each one activate a transistor for the LED pairs. I know there is a more elegant way to do this, but I haven't much of a clue how to do it.

I'm at the office right now, but I'll post my code when I get home

SOME TIME LATER...

Here's the code,


const int LED1 = 13; // LED connected to digital pin 13
const int LED2 = 12; // LED connected to digital pin 12
const int LED3 = 11; // LED connected to digital pin 11
const int LED4 = 10; // LED connected to digital pin 10

void setup()
{
  pinMode(LED1, OUTPUT); //sets the digital pin as output
  pinMode(LED2, OUTPUT); //sets the digital pin as output
  pinMode(LED3, OUTPUT); //sets the digital pin as output
  pinMode(LED4, OUTPUT); //sets the digital pin as output
 
}
void loop()
{
  digitalWrite(LED1,HIGH); //turns LED on
  delay(7);             //wait for 7MS
  digitalWrite(LED1, LOW);  //turns the LED off
  delay(7);             //waits for 7MS
  digitalWrite(LED2, HIGH); //turns LED on
  delay(6);             //wait for 6MS
  digitalWrite(LED2, LOW);  //turns the LED off
  delay(5);                 // waits for 5MS
  digitalWrite(LED3, HIGH); //turns LED on
  delay(6);             //wait for6MS
  digitalWrite(LED3, LOW);  //turns the LED off
  delay(6);             //waits for 6MS
  digitalWrite(LED4, HIGH); //turns LED on
  delay(5);             //wait for 5MS
  digitalWrite(LED4, LOW);  //turns the LED off
  delay(5);             //waits for 5MS

 
}
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: simi on February 24, 2017, 08:34:39 pm
Not sure if this will help, but here's the method I wrote for my D7 flicker engines.  Pretty simple, the effect gets evaled every 50 milliseconds and the "flickering" comes from the random function.  In my code below, I had yellow and red LEDs triggered.  You can probably ignore the if/else statement, mine was triggered by a button toggle.

long engineCurrentMillis = 0 ; //timer for the engine lights
void controlEngines()
{
  engineCurrentMillis = millis() ;
  if( isEnginesRunning )
  {
    if( engineCurrentMillis-engineMillis >50 )
    {
      analogWrite( YELLOW_ENGINE_LIGHTS_PIN, random(255) );
      analogWrite( RED_ENGINE_LIGHTS_PIN, random(255) );
      engineMillis = millis() ;
    }
  }
  else
  {
    engineMillis = 0 ;
    analogWrite( RED_ENGINE_LIGHTS_PIN, LOW ) ;
    analogWrite( YELLOW_ENGINE_LIGHTS_PIN, LOW ) ;
  }
}


Derp, here's a vid on how the effect looks (picture is worth a thousand words):

http://vid1376.photobucket.com/albums/ah7/csiemback/Revell_D7/IMG_2080_zpsortmx1gc.mp4 (http://vid1376.photobucket.com/albums/ah7/csiemback/Revell_D7/IMG_2080_zpsortmx1gc.mp4)
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: simi on February 25, 2017, 05:32:40 pm
One more thing, you're using the delay function - while this may be fine for you purposes - it's a blocking call.  This means the nothing can happen in the system while the delay is on.  Generally, you use some type of timer so that you can do "other things".  In the code I added previously, there's a timer for the engines. It's set to 50 millis - so the flicker changes the lights every 50 millis by a random amount.  You can change the 50 millis to a lower or higher number to get a faster or more subtle flicker event.  But to my point, you're loop function doesn't get blocked - preventing you from doing other things.  So for example:

void loop()
{
   controlEngines() ; // this hits the engine flicker method
   controlBeaconLights() ; // this function might turn on/off your beacon lights every 1 or 2 seconds
}

I've cleaned up my code previously, mine had a switch to allow you to turn on/off the engine effects. If you just want it "on", you can simplify the method as follows:

long engineMillis = 0;
long engineCurrentMillis = 0 ; //timer for the engine lights
void controlEngines()
{
  engineCurrentMillis = millis() ;
 
  if( engineCurrentMillis-engineMillis >50 ) //every 50 millis, change the lighting effect
  {
      analogWrite( YELLOW_ENGINE_LIGHTS_PIN, random(255) );
      analogWrite( RED_ENGINE_LIGHTS_PIN, random(255) );
      engineMillis = millis() ;
   }
}


There are other ways of doing this with classes/objects - but this would be the next evolution of what you're trying to do.

Cheers!

Simi
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: simi on February 25, 2017, 06:01:46 pm
...and..one last thing.  My code is using the PWM pins. These pins use Pulse Width Modulation (PWM).  That's why my code is using the analogWrite method with a 0-255 range rather than digitalWrite(ON/OFF).  The range makes the LED brighter/less bright basded on the magic of PWM (which just fools your eyes - that's all).  The digitalWrite is a ON/OFF.  Just so you know, the pins on an arduino that support PWN usually have a "~" symbol next to the pin number.  Also, here's the official documentation:

https://www.arduino.cc/en/Reference/analogWrite (https://www.arduino.cc/en/Reference/analogWrite)

Cheers!
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Azrael on February 26, 2017, 06:15:43 am
I'll second Simi's point about using analogWrite - your on/off will look a little jarring after a while.  If you waver your output using analogWrite between something like 60% to 100% randomly every 25ms or so (I choose 25 because that results in a 'flicker' rate faster than the eye can pick up - 40Hz), it'll still give you that flux effect you're seeking.  I've never used transistors on the analog-capable pins, but I can't imagine a reason why it wouldn't work. 

Are your engines circuits paired, linked engines next to one another? Or are they spread out so they look independent?  If they're right next to each other, then my recommendation above would be probably closer in output to one another, more like 80%-100%, to reduce the contrast between pairs.  You want the perception of variation and flux, not to smack the observer in the face with a trout "See it?!? Do you see it?!?!?"

Az
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 26, 2017, 02:21:35 pm
Thanks for the input guys, I'm definitely going to try Sini's code, his video looks pretty close to what I had envisioned.

Here's what my code looks like in action,
http://s111.photobucket.com/user/Darph_Bobo_photos/media/48D2289C-E6FA-43D7-92D0-5C118DCFE13C_zpscfgpdzzd.mp4.html

Azrael, my idea was to have the engines blink in pairs; I attached a fast 'n dirty pic in my first post on how I plan to mate them up

Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 26, 2017, 03:26:12 pm
So I have Simi's code up and running happily, but I can only run one LED per output and I can't use the PWM output to drive the transistors. Am I relegated to having to use fiber optics to light all eight engines?

This is the board I plan to use for the final product, it supposedly has up to 20 digital outputs, but I don't know if that means PWM outs or not.

https://www.universal-solder.com/products/arduino-nano-v3-0-compatible-canaduino-nano-v3-0?variant=32623556362

Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: simi on February 26, 2017, 05:34:13 pm
Hey Grumpy,

I'm starting to hit my wall on knowledge of electronics - but any arduino chip (there's a spec for it) should have several PWM pins.  So if you have 4 or 5 (or less) LEDs you want to do this with - it should be fine.  Also, you can power multiple LEDs on a single pin (which is what I did).  You need to adjust the resistors to do this.  I THINK (please check!) that the specs recommend 20ma or less per pin.  I've powered 3 LEDs/pin previously - though I don't recall what ohm resistor I used for each LED.  The light-level using the higher resistors didn't wasn't really noticeable to me.

Looks like you're using a nano chip, which is what I used.  The PWM pins are 3,5,6,10,11.

I'm know there are other "devices" that will allow the arduino to handle more current.  For example, my arduino controls a 12v and 5v rail via MOSFETs - so the arduino isn't handling that current - just the the small amount to "turn on" the gate from the mosfet (teh 5v and 12v don't go through the arduino).  I'm not certain how a mosfet might affect the PWN effect (I think it would be fine).

Sorry if this isn't as helpful as it could be.  But probably like you, everything I've learned about electronics has been to get some of my kits lighted!  I'm sure there are others here that have more definitive answers.  I too would be interested to find out.

Also, I know how frustrating it can be to have what you want clearly in your mind and not be certain how to get a physical representation of it working.  I'm sure you know this, but make sure when you prototype your circuit, you let it run for 24 hours or so straight.  If things will burn out, it will do so within that time (and not happen after you've glued that last piece into your kit!).

Cheers!

Simi
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 27, 2017, 01:13:18 am
While I have yet to do an overnight test, here is a short video of my engine progression,

https://www.facebook.com/john.beaulieu.359/videos/10154222605871791/

Thanks for your help Simi, the ship's engines are gonna look great!
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Azrael on February 27, 2017, 09:13:39 am
Hi guys - on a standard Arduino, or an Arduino-compatible pinout board, the pins that are capable of PWM are numbers 3,5,6,9,10 and 11.  On a standard board they're marked with a tilde sign (~). 

  Az

P.S.  Yeah, those test shots look a lot better with PWM :).
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: RossW on February 27, 2017, 09:39:00 am
So I have Simi's code up and running happily, but I can only run one LED per output and I can't use the PWM output to drive the transistors. Am I relegated to having to use fiber optics to light all eight engines?

This is the board I plan to use for the final product, it supposedly has up to 20 digital outputs, but I don't know if that means PWM outs or not.

https://www.universal-solder.com/products/arduino-nano-v3-0-compatible-canaduino-nano-v3-0?variant=32623556362
Why can't you run the PWM output through a NPN transistor (e.g. 2N40001) to power more LEDs? I do that all the time. Note that there's a difference between a transistor and a most - you need to flip your output signal for a misfit (i.e. HIGH or 100% duty cycle translates to off; 0% or LOW is on). I'm sure everyone has their own preference, but I like the 2N40001 - never had any problems with it.
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 27, 2017, 09:48:15 am
The transistors I was using (you m in the first part of my video) are 2N222s, I had them on hand so that's what I used  ;)

I'll have to try one of them out to see if they'll work on an PWM output
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: simi on February 27, 2017, 04:09:30 pm
So I have Simi's code up and running happily, but I can only run one LED per output and I can't use the PWM output to drive the transistors. Am I relegated to having to use fiber optics to light all eight engines?

This is the board I plan to use for the final product, it supposedly has up to 20 digital outputs, but I don't know if that means PWM outs or not.

https://www.universal-solder.com/products/arduino-nano-v3-0-compatible-canaduino-nano-v3-0?variant=32623556362
Why can't you run the PWM output through a NPN transistor (e.g. 2N40001) to power more LEDs? I do that all the time. Note that there's a difference between a transistor and a most - you need to flip your output signal for a misfit (i.e. HIGH or 100% duty cycle translates to off; 0% or LOW is on). I'm sure everyone has their own preference, but I like the 2N40001 - never had any problems with it.

Yeah, I figured there's a better, smarter, gooder :) way of doing it!  I'm better at coding than I am at electronics.

Grumpy, the updated effects you posted do look pretty darn good in my opinion.  If you do end up using something else (like including a transistor) update this thread if you can on the part# and circuit layout if you have the time.  Would be useful for me!

Good luck and keep us updated!


Cheers!

Simi
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: RossW on February 28, 2017, 08:14:24 am
Sorry, I mixed up my diode number (1N4001) with my transistor - I'm using a 2N2222A NPN transistor which can handle larger currents. The attached diagram shows how I wire up the LEDs (they connect to the M1 & M2 connectors). The line with the 1k resistor is coming from my PIC micro controller, but it would be same for an Arduino PWM pin.
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 28, 2017, 11:07:26 am
Will do Simi! I'll put the final code up too so that others can use it for their builds (pretty much the exact same as yours, just some cut/paste for more outputs)

RossW, I'll try my cct again tonight with the transistors, I may have plugged it into the wrong spots on my breadboard last time.

Thanks again for all your help and support guys!
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: Grumpy Popeye on February 28, 2017, 08:55:46 pm
So I plugged in a transistor into my cct and the results were less than stellar. Even while being fed by 12V the LEDs were way dimmer than those being driven off the Arduino. While I can't account for the dimness, I think the transistor is only switching on once its voltage threshold is reached, so we're not getting the full effect of the PWM signal.

Here's a fun bit of video, Arduino feed at the top, 12V transistor on the bottom;

http://vid111.photobucket.com/albums/n154/Darph_Bobo_photos/8A34EDC4-86E2-48B8-B21A-3DC39CD3532F_zpsxcx9epvo.mp4
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: AussieTrekkie on February 28, 2017, 09:35:15 pm
Probably a little late. When I did a course in electronics 15 years ago my understanding of the transistor, the base leg is what turns the transistor on and off. The base leg only needs a very small amount of current to turn on the transistor. In saying this a transistor cannot be used in a circuit that the Arduino will be used for flickering the lights, you need to connect the led directly to the PWM output on the Arduino to get the effect. In my Refit build using multiple Arduinos to control my lighting system I am using the before mentioned transistors 2N222A to turn on multiple leds on a circuit from one output on the Arduino. I have the base leg connected to the Arduino and the Collector and Emitter legs are connected to the led and the main power supply. So the Arduino is turning on the transistor but the transistor is turning on the led using the 9/12 volt power.

Don't know if this helps you in anyway.
Scott.
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: madmonk on March 01, 2017, 02:51:41 am
I regularly run 2 LEDs of one output and usually attach the resistor to one leg of the LED then take 2 wires back to the output, the Arduino outputs run at 5V so I use a 180Ohm resistor in line with each LED, basically running the LEDs in parallel.
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: texastony1946 on August 15, 2017, 08:15:14 am
I'm trying with no success to ramp and flicker an LED at the same time. The code gets tangled up in timing between the 2 functions. I think I can do this with 2
JFET in seires (one to flicker ,one to ramp). Any ideas would be appreciated.
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: scottminium on May 09, 2018, 04:20:06 pm
In case anyone is looking for ideas, for my Y-Wing I flicker the engines and ramp them.  They ramp up or down, then hold, then randomly move on to a new value.  They ramp at 1 setting/ms, so the effect is quick, but not instantaneous.
sketch:
const int EngPin =9;     
const int DummyPin = 8;   // unused pin for initial random value
int NewEngPower=200;            //initial value for the next brightness
int EngPower=150;            //Brightness setting
int EngOnTime=100;          //how many ms to hold the current EngPower value

unsigned long seed=0;
int count=32;

void setup() {
  pinMode(EngPin, OUTPUT);       
  pinMode(CockpitPin, OUTPUT);     
  pinMode(DummyPin, INPUT);
  randomSeed(analogRead(DummyPin));
  analogWrite(CockpitPin, 180);
  }

void loop(){
  //ENGINES
       // Engines will randomize output for random times,
       // ramp up or down to new value then
       // stay at a particular brightness for 50-500ms after ramp up/down
   analogWrite(EngPin, EngPower); 
   if (NewEngPower>EngPower) {EngPower++; } 
      else if (NewEngPower<EngPower) {EngPower--;}
            else {EngOnTime--;}  //if NewEngPower==EngPower, then hold at that power
   if (EngOnTime<=0) {   //respawn the seed generator, maybe useless
                       count=32;
                       while (--count)
                          seed = (seed<<1)|(analogRead(DummyPin)&1);
                       randomSeed(seed);
                       NewEngPower=random(150,255);  //minimum acceptable value and max for brightness
                       EngOnTime=random(80);     //adjust to to get desired flicker rate
                     }
 
       
  delay(1);  //this is the overall control loop, so engine functions are checked/adjusted each millisecond

}
Title: Re: Need help with arduino sketch (flickering engine lights)
Post by: texastony1946 on May 10, 2018, 05:24:58 am
Scott I was able to ramp and flicker at the same time in my gundam code. Not the most eloquent solution, but worked for my needs. It ramps and flickers , then holds at max output.