newbe

I am new to the world of Arduino and have been for lack of a better word tring my luck at loading, testing and changing sketches to suit needs. Hobbies are old traffic lights, Halloween props and radio controlled planes, helis and boats. I have a sketch that I uploaded for navigational lights for a scale RC plane but wanted to see if there is an easy way to duplicate part of the sketch to have a second strobe output and a second fading output work with its own timing. Like cut and paste part of the sketch but stuck when it comes to what needs to be changed so they work independent of the other codes.

Dave

Huh ?

What Mee_n_Mac said.

It’s hard to give advice without seeing your code. Possibly you have simple code written using “delay(xx)” for timing. To get a second sequence with different timing you are likely going to have to rewrite it to use a timer count instead. There are other discussions of that method on this board if you search. Again, without seeing your code and having a more detailed explanation of what you want it’s hard for us to say. Good luck!

Here is the code… I have dropped in the changes I am tring to make on lines 2 and 4

const int redled=3;

const int whiteled=5;

const int twinred=6; //would like to copy this output to flash opposite from the other

const int revolving=9; //would like to copy this output and control its timing

unsigned long previousred=0;

unsigned long previouswhite=0;

unsigned long revolvingtime=0;

int redoff=1000;

int redon=30;

int whiteoff=1100;

int whiteon=100;

int twinLongOff=1000;

int twinOn=40;

int twinOff=100;

int revolvingperiode=2000;

int value;

int FadeOff=2;

int FadeOn=2000;

unsigned long twinStartMillis=0;

unsigned long fadeStartMillis=0;

enum ledState {

firstOnPeriod, shortInterval, secondOnPeriod, longInterval};

static ledState twinState=longInterval;

enum revolvingState {

Fade, Off };

static revolvingState revolveState=Off;

void setup(){

pinMode(redled, OUTPUT);

pinMode(whiteled, OUTPUT);

pinMode(twinred, OUTPUT);

}

void loop(){

unsigned long currentred=millis();

unsigned long currentwhite=millis();

if(currentred-previousred>=redon){

digitalWrite(redled, LOW);

}

if(currentred-previousred>=redoff){

previousred=currentred;

digitalWrite(redled, HIGH);

}

if(currentwhite-previouswhite>=whiteon){

digitalWrite(whiteled, LOW);

}

if(currentwhite-previouswhite>=whiteoff){

previouswhite=currentwhite;

digitalWrite(whiteled, HIGH);

}

unsigned long now=millis();

switch (twinState){

case firstOnPeriod:

if(now-twinStartMillis>=twinOn){

digitalWrite(twinred,LOW);

twinState=shortInterval;

}

break;

case shortInterval:

if(now-twinStartMillis>=twinOn+twinOff){

digitalWrite(twinred,HIGH);

twinState=secondOnPeriod;

}

break;

case secondOnPeriod:

if(now-twinStartMillis>=twinOn+twinOff+twinOn){

digitalWrite(twinred,LOW);

twinState=longInterval;

}

break;

case longInterval:

if (now-twinStartMillis>=twinOn+twinOff+twinOn+twinLongOff){

digitalWrite(twinred,HIGH);

twinState=firstOnPeriod;

twinStartMillis=now;

}

break;

}

switch (revolveState){

case Fade:

if (now-fadeStartMillis >= FadeOn)

{

revolveState=Off;

digitalWrite(revolving, LOW);

}

else

{

revolvingtime = now - fadeStartMillis;

value = 128 - 128cos((2PI*(float)revolvingtime)/revolvingperiode);

analogWrite(revolving, value);

}

break;

case Off:

if (now-fadeStartMillis > FadeOn + FadeOff)

{

revolveState=Fade;

fadeStartMillis=now;

}

break;

}

}

Ok, the one where you want to “flash opposite from the other”; do you mean you want another light on the other side of the plane that flashes the same way or a second light that is off when the first is on and on when the first is off? Either of these are pretty easy. In the “same way” case you just wire a second LED to the same output (you may need a driver to drive bright enough). In the “on when the other is off” case you have two options 1) wire the LED to the same pin as the first LED but drive through an inverting driver OR 2) assign the new LED to another unused pin (i.e. 7 seems to be unused) then in code everywhere you set “whiteled” low follow that by setting your new LED high and vice versa.

For the other addition that duplicates “revolving” you will need to assign another output ( pin 8 perhaps?) and write a new switch statement similar to the one that starts “switch(revolveState){”. Your new switch will have the same structure and states but you will need to assign new timing variables so you can change them.

Of course all of this assumes your controller has the additional pins accessible and that you are able to supply appropriate hardware to drive the additions.

Hope this helps!

Ooops. Pin 8 will not work for your second revolving output. It needs to be a PWM capable pin. Maybe 10 or 11.

Yes, I would like the second “strobe” to flash totally opposite from the current strobe. Still learning the arduino lingo so figured I would put it out there for some tweaking!

Dave

Contemplating this a bit more, I don’t think you really want light the 2nd LED exactly opposite from the current ine. The current LED is one-time period on and 11 time-periods off. That is mimicking an actual strobe with the LED. Exactly opposite would have the second LED on for 11 time periods and off for one. That’s not realistic. What you want is a second strobe that is also off for 11 time-periods and on for one, but the two are 180 degrees out of phase. This is where the timer counter comes in. Instead of having redled on for one and off for 11 you will want this; redled1 on for one, both off for 5, redled2 on for one, both off for 5, repeat.

Also, regarding my comment about the revolver, I was trying to simplify it for you and went too far. To keep the states of the two revolvers from interfering with each other they will have to have separate state variables. I was wrong when I said that they could share the state variables. They share state definitions, but the variables need to be separate.