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

Login with username, password and session length
 

Author Topic: Need some newbie help!  (Read 107 times)

Offline incubus35

  • Lieutenant
  • **
  • Posts: 84
Need some newbie help!
« on: December 12, 2018, 02:58:04 pm »
Hi all,

I was wondering if someone would be able to help me please?

I am not really a programmer, so I'm not sure I'll be able to learn how to do what I want to achieve. I have two different sketches that I am trying to combine into one (i'm currently having to run each on a separate Arduino board, which seems a bit of a waste!). One is a candle effect, that I'm using to show battle damage, and the other is for navs/strobes.

I have not coded either of these myself - I believe they were both created by a user called Electrick_RWM on the Arduino exchange.

Would anyone be able to help me combine them into one sketch please? I am also trying to figure out the correct timings for the navs and strobes for the Enterprise Refit.

thanks in advance!
Pete

Offline simi

  • Admiral
  • *****
  • Posts: 636
  • JUST enough knowledge to do some serious damage
Re: Need some newbie help!
« Reply #1 on: December 12, 2018, 04:38:48 pm »
Hey Pete,

Can you perhaps share both sketches?  Would be easier to combine in a single sketch.  Also, would be helpful if you could supply the actual lighting components and power supply - as there's only so many amps/volts an arduino can safely handle. 

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 incubus35

  • Lieutenant
  • **
  • Posts: 84
Re: Need some newbie help!
« Reply #2 on: December 12, 2018, 04:47:51 pm »
Hi Simi!

Thanks for replying! I thought I had included the sketches as attachments. Can you not see them? i had to convert them to txt files so they would upload.

In terms of some of the other details, I'm not that experienced with this stuff either sadly. For the navs sketch I am just using 5 LEDs, wired in parallel, with resistors attached to each bulb. 3 are for the navs and 2 are for the strobes.

For the candle effect, I'm just using two prewired SMDs, again wired in parallel. Both sketches work separately and I don't have any issues with power running these - they are just using a 12v adapter attached directly to the Arduino.

Is that enough information?

Thanks again for your help!
Pete
« Last Edit: December 13, 2018, 04:42:44 am by incubus35 »

Offline simi

  • Admiral
  • *****
  • Posts: 636
  • JUST enough knowledge to do some serious damage
Re: Need some newbie help!
« Reply #3 on: December 13, 2018, 07:16:56 pm »
Hey - it looks like you have pins that conflict between the 2 sketches.  That could probably be resolved.  The other minor issue with the candle sketch is its use of the delay function.  That's will throw a little bit of a monkey in the wrench if you try to combine the 2 sketches.  Basically, a delay stops ALL processing until it completes.  There are other methods to get around this too.  Question on the candle sketch - Can  you compare it (try running this) and see which one "looks" better for you situation.  These use pins 5 and 6 (controls 2 LEDs).  The sketch below is a bit easier to combine with the other -but try it out.  If you really like the candle one you got - could be updated to work.


const int STARBAORD_ENGINE_LIGHT_PIN = 5 ;
const int PORT_ENGINE_LIGHT_PIN = 6 ;

long engineCurrentMillis = 0 ; //timer for the engine lights
long engineMillis = 0 ; // timing variable for the Engines

void setup()
{
  pinMode( STARBAORD_ENGINE_LIGHT_PIN, OUTPUT ) ; 
  pinMode( PORT_ENGINE_LIGHT_PIN, OUTPUT ) ;
}

void loop()
{
  //random flicker
  controlEngines() ;
}

void controlEngines()
{
  engineCurrentMillis = millis() ;
 
  if( engineCurrentMillis-engineMillis >50 )// change this value to get different flickering rates
  {
    analogWrite( STARBAORD_ENGINE_LIGHT_PIN, random(255) ); //randomly set LED level from  0-255
    analogWrite( PORT_ENGINE_LIGHT_PIN, random(255) );//randomly set LED level from  0-255
    engineMillis = millis() ;
  }
}


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: 636
  • JUST enough knowledge to do some serious damage
Re: Need some newbie help!
« Reply #4 on: December 13, 2018, 07:31:28 pm »
Well, here's the 2 sketches combined.  The 2 flickering LEDs are on pins 5 and 6, while the others are on whatever you had before.  Didn't actually test this, but it did compile :). And it's not the prettiest code in the world.   Also, since this sketch that you're using has only 3 pins (I would recommend using a pin/LED) are you having 1 or 2 of the arduino pins driving MORE than one LED?  If so, that's OK as long as the resistors are high enough to keep the amp below 20mA.  Or you can use a pin/LED with lower resistors (but still keep each pin at or under 20mA).  Lemme know on that....also - the flickering rate for the "candle" effect in the code below can be changed somewhat by changing the 50 millis to some higher or lower value.  Also, you can change the random number to something more definitive.  And lastly, the controlEngines() method you can change to something that makes sense for you sketch (ie, controlDamage or something like that).  That's if you want.  I just copied the the code I used to have a random nacelle flicker with red LEDs.


class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int ledPin; // the number of the LED pin
  long OnTime; // milliseconds of on-time
  long OffTime; // milliseconds of off-time

  // These maintain the current state

  int ledState; // ledState used to set the LED
  unsigned long previousMillis; // will store last time LED was updated
  // Constructor - creates a Flasher
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);
    OnTime = on;
    OffTime = off;
    ledState = LOW;
    previousMillis = 0;
  }

  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW; // Turn it off
      previousMillis = currentMillis; // Remember the time
      digitalWrite(ledPin, ledState); // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH; // turn it on
      previousMillis = currentMillis; // Remember the time
      digitalWrite(ledPin, ledState); // Update the actual LED
    }
  }
};
Flasher led1(7, 100, 10);
Flasher led2(8, 450, 2000);
Flasher led3(4, 400, 400);

const int STARBAORD_ENGINE_LIGHT_PIN = 5 ;
const int PORT_ENGINE_LIGHT_PIN = 6 ;

long engineCurrentMillis = 0 ; //timer for the engine lights
long engineMillis = 0 ; // timing variable for the Engines

void setup()
{
  pinMode( STARBAORD_ENGINE_LIGHT_PIN, OUTPUT ) ; 
  pinMode( PORT_ENGINE_LIGHT_PIN, OUTPUT ) ;
}

void loop()
{
  led1.Update();
  led2.Update();
  led3.Update();
  //random flicker
  controlEngines() ;
}

void controlEngines()
{
  engineCurrentMillis = millis() ;
 
  if( engineCurrentMillis-engineMillis >50 )// change this value to get different flickering rates
  {
    analogWrite( STARBAORD_ENGINE_LIGHT_PIN, random(255) ); //randomly set LED level from  0-255
    analogWrite( PORT_ENGINE_LIGHT_PIN, random(255) );//randomly set LED level from  0-255
    engineMillis = millis() ;
  }
}
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 incubus35

  • Lieutenant
  • **
  • Posts: 84
Re: Need some newbie help!
« Reply #5 on: December 14, 2018, 09:27:50 am »
Simi, you are a legend! Thank you so much for this! It has been bugging me for months having to use two boards!

Here is a short video of what you have helped me achieve
https://youtu.be/Z1EZAAWFRpE
« Last Edit: December 14, 2018, 09:30:58 am by incubus35 »

Offline simi

  • Admiral
  • *****
  • Posts: 636
  • JUST enough knowledge to do some serious damage
Re: Need some newbie help!
« Reply #6 on: December 14, 2018, 11:58:02 am »
Glad I could help (and glad your sketches were pretty simple).  That is a seriously nice build with some nice lighting effects.  I'm glad the candle effect replacement code I swapped out seems to work pretty well.  I hated to switch out implementations on you(cause you probably liked the effect you originally had), but it was faster than re-writing the original candle sketch code to play nicer with the other sketch.  If you prefer your older version of the effect, it's certainly possible to modify the code to use it...but that effect you got going on looks damn nice as it is!

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.

 




Enterprise © Bloc