PWM LED Fade + Strobe Timing Assistance

howdy! i was hoping i would be able to complete this on my own, but i seem to have hit a wall… :-/ anywho, what im building is basically a navigation and collision avoidance lighting controller for use with rc aircraft. at the moment, i have 4 leds with 3 different functions im trying to control.

  • 1 red & 1 green are constant on
  • 1 red beacon slow fades via pwm
  • 1 white strobe double flashes at 2 sec intervals

i have everything looking the way i like, except the for the strobe… with my current code, when first powered up, the strobe will flash twice as it should, but then goes to a dim state and never repeats. any insight on how to go about repeating the flash sequence would greatly appreciated! :slight_smile:

ohh yea… not sure if it matters or not, but after getting the strobe working, i plan to add on/off control via pwm in from my rc tx/rx. thanx again!
-sj

int value, value2;
int ledPin7 = 7;  // Red LED connected to digital pin 7
int ledPin8 = 8;  // Green LED connected to digital pin 8
int ledPin9 = 9;  //  White LED connected to digital pin 9
int ledPin10 = 10;  // Red LED connected to digital pin 10

long time = 0, time1, time2, time3, time4, time5;

int periode = 2200;

void setup()   {                
  // initialize outputs:
  pinMode(ledPin7, OUTPUT);  
  pinMode(ledPin8, OUTPUT);  
  pinMode(ledPin9, OUTPUT);  
  pinMode(ledPin10, OUTPUT);     

  time1=millis();   // set time as now
  time2=millis()+20; //first strobe duration
  time3=millis()+120;   // short wait
  time4=millis()+150; //second strobe duration
  time5 = 2350; //long wait
}

void loop()    {                 
  {
    // Nav Light Control :
    digitalWrite(ledPin7, HIGH);    // set port LED on
    digitalWrite(ledPin8, HIGH);    // set starboard LED on
    
    // Strobe Sequence :
    time1=millis();   // set time as now

    if(millis()>time1) // first strobe flash 
      digitalWrite(ledPin9, HIGH);   // set the LED on
    if(millis()>time2) // short wait
      digitalWrite(ledPin9, LOW);    // set the LED off
    if(millis()>time3) // second strobe flash
      digitalWrite(ledPin9, HIGH);   // set the LED on
    if(millis()>time4) // end of second flash
      digitalWrite(ledPin9, LOW);    // set the LED off

    if(millis()-time1<=time5); // long wait <<<<===this is where im stuck

    // Beacon Control :
    time = millis();
    value = 128+127*cos(2*PI/periode*time);  //Beacon Control
    analogWrite(ledPin10, value);           // sets the value (range from 0 to 255)

  }
}

I think you're misunderstandin how "millis" works, or perhaps how "loop" works. It simply returns the time elapsed since the last reset. You're not updating "time2" (or time3 or time4) so that they are they "fire" the comparisons at a future time.

Another thing:

int ledPin7 = 7;  // Red LED connected to digital pin 7

why not call it:const int redLed = 7;  // Red LED connected to digital pin 7 or

const int portNavLight = 7; // Red LED connected to digital pin 7 const int starboardNavLight = 8;

?

If these are on all the time, you may as well move them into "setup".

thanx for taking a look at my problem groove!

I think you're misunderstandin how "millis" works, or perhaps how "loop" works.

this is highly probable!

It simply returns the time elapsed since the last reset. You're not updating "time2" (or time3 or time4) so that they are they "fire" the comparisons at a future time

thats basically what i was trying to ask... i understand why i need the millis function, i get what the loop does, i'm missing some fundamental piece(s) of syntax to make the loop function as i would like for it to.

so, correct me if im wrong, millis does not turnover at the restart of the loop, thus, requiring some math to update my variables as millis chugs along. ?

Another thing: Code: int ledPin7 = 7; // Red LED connected to digital pin 7

why not call it: Code: const int redLed = 7; // Red LED connected to digital pin 7

or Quote: const int portNavLight = 7; // Red LED connected to digital pin 7 const int starboardNavLight = 8; ?

If these are on all the time, you may as well move them into "setup".

ok, so changing it to constant would save on cycles, correct? and renaming the pins just makes it easier to identify what's what?

the plan is to be able to toggle LEDS on & off via a RC transmitter. i wouldn't want to assign them to const or move them to setup if i plan to add switches would i? cheers! -sj

so, correct me if im wrong, millis does not turnover at the restart of the loop, thus, requiring some math to update my variables as millis chugs along. ?

Correct. The millis() function returns the time since the Arduino was (re)started, not the time since the last start of loop().

ok, so changing it to constant would save on cycles, correct?

No. Making it const guarantees that you won't accidentally change it later. The big change, though, is to give the variable a meaningful name.

If you change the pin that is referenced by ledPin7 to 14, it is really confusing. If you change the pin referenced by redLED from 7 to 14, there is no confusion. The red LED still lights up when you set redLED HIGH.

i wouldn't want to assign them to const or move them to setup if i plan to add switches would i?

You would still use const. After all, while the Arduino is powered up, the pin that the red light is attached to isn't going to change, is it?

If the lights are going to be controlled by a switch, the code to activate them may go in setup or in loop. It depends on how the switch is used. If the switch is between the pin and the LED, then the light is controlled by both the switch and the pin state, so the code can go in setup.

If the switch is connected to a digital pin, and it's state is read to determine if the lights should be on or off, the code to turn the lights on or off needs to be in loop.

If the lights (and switch) are on the RC plane, how will you press the switch when the plane is in flight?

Correct. The millis() function returns the time since the Arduino was (re)started, not the time since the last start of loop().

ok, thanx!

No. Making it const guarantees that you won't accidentally change it later. The big change, though, is to give the variable a meaningful name.

If you change the pin that is referenced by ledPin7 to 14, it is really confusing. If you change the pin referenced by redLED from 7 to 14, there is no confusion. The red LED still lights up when you set redLED HIGH.

cool. i swapped all the names and the code seems much easier to navigate now. thanx!

You would still use const. After all, while the Arduino is powered up, the pin that the red light is attached to isn't going to change, is it?

If the lights are going to be controlled by a switch, the code to activate them may go in setup or in loop. It depends on how the switch is used. If the switch is between the pin and the LED, then the light is controlled by both the switch and the pin state, so the code can go in setup.

If the switch is connected to a digital pin, and it's state is read to determine if the lights should be on or off, the code to turn the lights on or off needs to be in loop.

If the lights (and switch) are on the RC plane, how will you press the switch when the plane is in flight?

i'll change the pins to const!

actually, the switch won't be physically connected to the arduino/plane... it is on a rc transmitter. i'll setup the tx to produce low and high pulse width signals on that channel to reflect the position of the switch and have the arduino read the values on the receiver end.

cheers! -sj