Aircraft Navigation Lights - Bad Coding?

I am new to Arduino so bear with me. I fly scale model aircraft so I wanted to create passable navigation lights. Arduino might just be the ticket. Working on my breadboard the following code works well but the anti-collision strobe wing lights 1 & 2 and strobe 3 tail light slow down if the beacon section of code is implemented. Is this the processor too busy or my coding bad?

/*
 Model Aircraft Navigation light system ver 2.
  by Phil Ford 2017. New to Arduino Jan 2017
  Board to be powered on when aircraft reciever is on.
  Assign a switch for landing lights eg; Gear switch to turn on/off
*/


// the setup function runs once when you press reset or power the board
int strobe1 = 8; //Anti-Collision wing tip
int strobe2 = 7; //Anti Collision wing tip
int strobe3 = 6; //Anti Collision rear
int green = 9; //Starboard Wing lamp
int red = 10; // Port Wing lamp
int beacon = 5; // Top Red Beacon (Revolving) Optional - Analogue Pin
int pin2 = 2; // Pulse from Receiver Switch - Landing Lights

void setup()
{
  //Pinmodes of the leds
pinMode(strobe1, OUTPUT);  //Wing Tip
pinMode(green, OUTPUT);  //Starboard
pinMode(red, OUTPUT);  //Port
pinMode(strobe2, OUTPUT);  //Wing Tip
pinMode(strobe3, OUTPUT);  //Tail
pinMode(beacon, OUTPUT);  //Belly Light
pinMode(pin2, INPUT);  // RX Signal

}

// the loop function runs indefinitely
void loop() {
  pin2 = pulseIn(2,HIGH,20000); //Checks the value of channel (Work in progress here!)

Serial.println (pin2); //Prints the channel's value on the serial monitor
  
    //steady lights - Wing lamps
  digitalWrite(green, HIGH); //Green Steady Wing light
  digitalWrite(red, HIGH); //Red Steady Wing light

  
  //strobe effects Strobe 1
  digitalWrite(strobe1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe1, LOW);    // turn the LED off by making the voltage LOW
  delay(50);                       // wait for milli seconds
    digitalWrite(strobe1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe1, LOW);    // turn the LED off by making the voltage LOW
  delay(150);                       // wait for milli seconds


    //Strobe 3. Fast flash Tail
                      // wait for a second
  digitalWrite(strobe3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe3, LOW);    // turn the LED off by making the voltage LOW
  delay(250);                       // wait for milli seconds
    digitalWrite(strobe3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe3, LOW);    // turn the LED off by making the voltage LOW
  delay(500);
  
  
  //Strobe 2. Slightly more delay than Strobe 1 for effect
  digitalWrite(strobe2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe2, LOW);    // turn the LED off by making the voltage LOW
  delay(50);                       // wait for milli seconds
    digitalWrite(strobe2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);                       // wait for milli seconds
  digitalWrite(strobe2, LOW);    // turn the LED off by making the voltage LOW
  delay(150);                       // wait for milli seconds
 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(beacon, fadeValue);         
    // wait for 20 milliseconds to see the dimming effect    
    delay(20);                            
  } 
  
  delay (100);
  analogWrite(beacon, 0);
  delay (100);
  analogWrite(beacon, 255);  
  delay (100);
  analogWrite(beacon, 0);
  delay (80);
  analogWrite(beacon, 255);  

  // fade out from not-quite-max to min in increments of 5 points:
  for(int fadeValue = 200 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 200):
    analogWrite(beacon, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
  }
  }

You're using delay for timing. So only one thing can be dealt with at a time. You need to write non-blocking code if you want things to be able to have their own timing intervals respected. See the "Blink Without Delay" example.

Thanks Delta-G. I'll have a look at "non blocking code"

Philf51:
Thanks Delta-G. I’ll have a look at “non blocking code”

No have a look at ‘Blink Without Delay’. These delay will stop your whole program, if you use blink without a delay, you won’t have to use delay so your program won’t have to completely ‘stop’ for a certain amount of time :)!

your coding could be better.
heres’ a few hints:

  1. move the code to control red and green to setup(), you only need to do it once.

  2. for the strobes, try something like this:

digitalWrite(strobe1, (millis() % 1000L) < 500L);
digitalWrite(strobe2, (millis() % 1000L) > 750L);
digitalWrite(strobe3, (millis() % 200L) < 100L);

  1. for the beacon
    digtalWrite(beacon, (millis() % 25L) < ((millis() % 5000L) / 200L));

(this should do the beacon dim up to bright, but not back down again. you can do that yourself later)

akatchi:
No have a look at ‘Blink Without Delay’.

Blink without delay is non-blocking code.

wg0z:

digitalWrite(strobe1, (millis() % 1000L) < 500L);

digitalWrite(strobe2, (millis() % 1000L) > 750L);
digitalWrite(strobe3, (millis() % 200L) < 100L);

That looks neat in this example but it’s not a good general-purpose solution. For larger, more complex programs, this is going to waste a LOT of time doing the long divisions. The last time I profiled it, using the % operator on longs was just excruciatingly slow. The simple subtraction used in most blink-without-delay code is hundreds of times faster. Floating-point division is actually faster than long division.

Keep using it for now, but when you start using this Arduino as part of the autopilot on the model, then you will need to ditch these modulos.

My original post was mostly meant to be illustrative only.

from (millis() % 1000L) it's a small step to (millis() % 1024L), and then another even smaller step to

((unsigned) millis()) & 0x3ff;

Perhaps the compiler would optimize "% 1024L to "& 0x3ff" in any case ?!

I just tested it on the closest convenient Arduino. (Micro)

Yes, the compiler does optimise the %1024 to avoid the long division. But restricting your timing intervals to powers-of-two is not going to work for most applications.

I say again: This is not a good general-purpose solution.

Have a look at how millis() is used to manage timing without blocking in several things at a time. There are 3 LEDs in the example and there could be lots more.

...R

AWOL: Blink without delay is non-blocking code.

I know.

This post may be better on RC Forums but here goes. A user on a RC Forums sent me the code which he has been using and which works very well. Even the revolving beacon effect is good. But the Arduino is switched on by using a 3 position switch in the code and on my JR Tx that is the flap switch.

But I need the Arduino to start when the main power switch is on and the landing lights only come on using a two position switch, probably gear (channel 5).

Pulses from the TX range from 899us to 2105us. Below is the code for the switching.

/*
 * Process the pulse and set the state of the lights.
 */
void ProcessPulse(unsigned int pulse)
{
    // Adjust these values for your receiver
  if ((pulse < 1200) || (pulse > 2100))  // switch is off or error
  { 
    NavLightsOff();
    LandingLightsOff();
    #ifdef DEBUG
      Serial.println("Lights off");
    #endif
  }
  else if (pulse > 1700) // All lights are on
  {
    NavLightsOn();
    LandingLightsOn();
    #ifdef DEBUG
      Serial.println("Lights on");
    #endif
  }
  else if (pulse > 1300) // Nav lights are on
  {
    NavLightsOn();
    LandingLightsOff();
    #ifdef DEBUG
      Serial.println("Nav Lights On");
    #endif
  }
}

Phif51, did you ever get a resolution to this last post? I have been going through the same issue.

I was looking for a way to add a simple strobe (using bright LED's run by an Arduino nano drawing its power from an Arduino Uno (used as the flight controller for a drone build)) to a drone build. I wanted to use an aux channel of my transmitter (switch) and truly thought that this was going to be simple to find someone who had done something "simple" just like it. I couldn' find ANYTHING that was any help. I looked in my free time for a week and nothing. I came across your sample serial code above and used it to see if I was at least making the switch work(i was). Then integrated what parts I needed into the code I created for the strobe and it worked perfectly.