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

Login with username, password and session length
 

Author Topic: Need help with arduino sketch (flickering engine lights)  (Read 814 times)

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Need help with arduino sketch (flickering engine lights)
« 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

 
}
« Last Edit: February 24, 2017, 11:40:21 pm by Grumpy Popeye »

Offline simi

  • Admiral
  • *****
  • Posts: 586
  • JUST enough knowledge to do some serious damage
Re: Need help with arduino sketch (flickering engine lights)
« Reply #1 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
« Last Edit: February 24, 2017, 08:38:41 pm by simi »
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline simi

  • Admiral
  • *****
  • Posts: 586
  • JUST enough knowledge to do some serious damage
Re: Need help with arduino sketch (flickering engine lights)
« Reply #2 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
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline simi

  • Admiral
  • *****
  • Posts: 586
  • JUST enough knowledge to do some serious damage
Re: Need help with arduino sketch (flickering engine lights)
« Reply #3 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

Cheers!
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline Azrael

  • Commander
  • ***
  • Posts: 136
Re: Need help with arduino sketch (flickering engine lights)
« Reply #4 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

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Re: Need help with arduino sketch (flickering engine lights)
« Reply #5 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

« Last Edit: February 26, 2017, 02:25:43 pm by Grumpy Popeye »

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Re: Need help with arduino sketch (flickering engine lights)
« Reply #6 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


Offline simi

  • Admiral
  • *****
  • Posts: 586
  • JUST enough knowledge to do some serious damage
Re: Need help with arduino sketch (flickering engine lights)
« Reply #7 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
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Re: Need help with arduino sketch (flickering engine lights)
« Reply #8 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!

Offline Azrael

  • Commander
  • ***
  • Posts: 136
Re: Need help with arduino sketch (flickering engine lights)
« Reply #9 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 :).
« Last Edit: February 27, 2017, 09:15:18 am by Azrael »

Offline RossW

  • Commander
  • ***
  • Posts: 248
Re: Need help with arduino sketch (flickering engine lights)
« Reply #10 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.

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Re: Need help with arduino sketch (flickering engine lights)
« Reply #11 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

Offline simi

  • Admiral
  • *****
  • Posts: 586
  • JUST enough knowledge to do some serious damage
Re: Need help with arduino sketch (flickering engine lights)
« Reply #12 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
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline RossW

  • Commander
  • ***
  • Posts: 248
Re: Need help with arduino sketch (flickering engine lights)
« Reply #13 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.

Offline Grumpy Popeye

  • Commander
  • ***
  • Posts: 172
Re: Need help with arduino sketch (flickering engine lights)
« Reply #14 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!

 




Enterprise © Bloc