Arduino Code for lighting system without using delays

Joce

New member
Hi,

Arduino is not multitask and each time you put delays, you stop the program execution. This make any lighting system very sequential and not realistic. This also reduces the capacity to do multiple other tasks in parallels

I wanted to have wingtip strobes blinking fast two times and have a delay after (like airbus). Plus a top and bottom strobe that would alternate at one second interval. This, independently.

Thought it might be useful for others.

Copy the below code in your Arduino IDE. BTW, there is a very interesting Arduino simulator I have been using for this (UnoArduSim V2.7.1 attached)
------------------------------------------------------------------------------------------------------------------------------------------------------------
//RC Plane Strobe blinking LED without using delays.



// Not using delays ensures code is never stopped and allow inputs or other tasks to

// be donne in parallel to blinking lights.



// Also provides independance on the lighting patterns as each tasks (blinking a set) is not waiting on the other



// For the double strobe per one second pattern, we use 2 timers (tasks 1 and 2 in code). The first timer is to blink

// a limited number of time very fast. The second time resets the counter after 1+ second so the sequence can restart.

// Result is 2 quick flash and a delay of over one second in this example. All this without stopping the loop from running.





#define ledpin2 2

#define ledpin3 3

#define ledpin4 4

#define ledpin5 5

#define ledpin6 6

#define ledpin7 7

#define ledpin8 8

int channel;

int prev_channel =0;



//Stobes are going to be triggered at "StobeFrequency" for "StrobeNbFlash" times using a counter "StrobeCtr"

unsigned long previousTimeStrobeFlash = millis();

long StobeFrequency = 100;

int StrobeCtr = 0;

int StrobeNbFlash = 2;



//This is to control the one second and allow a reset of strobes (Leds on output 2 and 3)

unsigned long previousTimeLed2a3 = millis();

long StobeCycleInterval = 1500;

int ledState2a3 = LOW;





unsigned long previousTimeTopAndBotomFlash = millis();

long timeIntervalTopAndBotomFlash = 1000;

int ledState6 = LOW;

int ledState7 = HIGH;





void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

pinMode(ledpin2, OUTPUT);

pinMode(ledpin3, OUTPUT);

pinMode(ledpin4, OUTPUT);

pinMode(ledpin5, OUTPUT);

pinMode(ledpin6, OUTPUT);

pinMode(ledpin7, OUTPUT);

pinMode(ledpin8, OUTPUT);



}



void loop() {



channel = pulseIn(9, HIGH);



// put your main code here, to run repeatedly:

unsigned long currentTime = millis();



//If higher digital signal, open landing lights on pin 8. To be used on 3 way switch (Off, leds, Leds+Landing)

if (channel > 1899)

{

digitalWrite (ledpin8, HIGH);

}

else

{

digitalWrite (ledpin8, LOW);

}



if (channel > 1800)

{

// task 1 : Wing tip Strobe - Stobes are going to be

// triggered at "StobeFrequency" for "StrobeNbFlash" times

// using a counter "StrobeCtr" and the go off until reset

// after "StobeCycleInterval" interval



if(currentTime - previousTimeStrobeFlash > StobeFrequency && StrobeCtr <= 2 ) {

previousTimeStrobeFlash = currentTime;

if (ledState2a3 == HIGH) {

ledState2a3 = LOW;

}

else {

ledState2a3 = HIGH;

StrobeCtr++;

}

digitalWrite (ledpin4, ledState2a3);

digitalWrite (ledpin5, ledState2a3);



if (StrobeCtr >= StrobeNbFlash+1 ){

digitalWrite (ledpin4, LOW);

digitalWrite (ledpin5, LOW);

ledState2a3 = LOW;

}



}

// task 2 : Reset stobe counter at "StobeCycleInterval" so that they start a new

// cycle of "StrobeNbFlash" time

if(currentTime - previousTimeLed2a3 > StobeCycleInterval) {

previousTimeLed2a3 = currentTime;

StrobeCtr = 0;

}



// task 2 : top and bottom probe alternating on pins 6 and 7

if(currentTime - previousTimeTopAndBotomFlash > timeIntervalTopAndBotomFlash ) {

previousTimeTopAndBotomFlash = currentTime;

if (ledState6 == HIGH) {

ledState6= LOW;

ledState7= HIGH;

}

else {

ledState6 = HIGH;

ledState7 = LOW;

}

digitalWrite (ledpin6, ledState6);

digitalWrite (ledpin7, ledState7);

}



// Steady RED and GREEN on pins 2 and 3

digitalWrite (ledpin2, HIGH);

digitalWrite (ledpin3, HIGH);

}

else {digitalWrite(ledpin2, LOW);

digitalWrite(ledpin3, LOW);

digitalWrite (ledpin4, LOW);

digitalWrite (ledpin5, LOW);

digitalWrite(ledpin6, LOW);

digitalWrite(ledpin7, LOW);

StrobeCtr = 0;





}



if(prev_channel != channel) {

Serial.println(channel);

prev_channel = channel;



}



}
 

Attachments

  • UnoArduSimV2.7.1.zip
    22.2 MB · Views: 0
  • RC_Plane_Strobe_LEDWODelay.ino.txt
    3.9 KB · Views: 0

Joce

New member
Changed reciever signal to pin10 to have 2 landing lights. Below schematic is using std servo connectors.
1614380872948.png
 

JasonK

Participation Award Recipient
@Joce - the arduino IDE has a blink without delay example sketch on how to do just that 'blink without using delays' which can easily be used to have multiple LEDs on different timers