Avionic lights-cannot get it to flash twice

Hello, I am complete beginner with Arduino, playing with it for few days. I have succesfully completed my first project-lightning for RC car. But now I got stuck on my second challenge. I am trying to code complete avoinics lights for my RC plane. At the moment, my strobos do flash and revolvig light works too, but I want my strobos do flash twice (30ms flash-30ms off-30ms flash-1000ms off) and, when possible, make revolving light do pause between cycles. Naturally, my code is completely without delays, with timing based on millis.I am trying to get this to work, but unsuccesfully. I will be very thankfull for any advice, my brain is cooked already.

const int redled=3;
const int whiteled=5;
const int doublered=6;
const int majak=9;

unsigned long previousred=0;
unsigned long previouswhite=0;
unsigned long previousdoublered=0;
unsigned long majaktime=0;

int redoff=1000;
int redon=30;
int whiteoff=1100;
int whiteon=25;
int doubleredoff=1050;
int doubleredon=30;
int majakperiode=2000;
int majakdisplace=500;
int value;

void setup(){
  pinMode(redled, OUTPUT);
  pinMode(whiteled, OUTPUT);
  pinMode(doublered, OUTPUT);
}

void loop(){
  unsigned long currentred=millis();
  unsigned long currentwhite=millis();
  unsigned long currentdoublered=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);}
  if(currentdoublered-previousdoublered>=doubleredon){
    digitalWrite(doublered, LOW);}
    if(currentdoublered-previousdoublered>=doubleredoff){
      digitalWrite(doublered,HIGH);
      previousdoublered=currentdoublered;}
 majaktime = millis();
  value = 128+127*cos(2*PI/majakperiode*majaktime);
   analogWrite(majak, value);
}

Try rewriting your code as a state machine, it will make it simpler. Example:

enum ledState {firstOnPeriod, shortInterval, secondOnPeriod, longInterval};

static ledState redState = longInterval;
static unsigned long redStartMillis = 0;

void loop()
{
  unsigned long now = millis();
  switch (redState)
  {
    case firstOnPeriod:
      if (now - redStartMillis >= redOn)
      {
        digitalWrite(redLed, LOW);  // end of first on-period, so turn LED off
        redState = shortInterval;
      }
      break;
    case shortInterval:
      if (now - redStartMillis >= redOn + redOff)
      {
        digitalWrite(redLed, HIGH);  // end of short off-period, so turn LED on
        readState - secondOnPeriod;
      }
      break;
    case secondOnPeriod:
      // ... you can write this yourself
    case longInterval:
      // ... you can write this yourself 
  }
}

Thank you, I tried to rewrite part of the code after your advice and it works like a charm. Twice flashing strobo problem is solved. Now I try to work out problem with delay between revolving light cycles.

Please explain what is working for you now, and what else you want it to do. Also post the code as you now have it.

I have integrated code for twice flashing strobo, written after your advice, in my sketch. All lights are working now as I wanted. Last one thing I wanted to do for now is to make delay for revolving light (at the end of the sketch attached below) between cycles. Sorry for maybe too elementary newbie questions, I'm learning on the fly....Many thanks for your help, got me going a little bit further.

const int redled=3;
const int whiteled=5;
const int twinred=6;
const int revolving=9;

unsigned long previousred=0;
unsigned long previouswhite=0;
unsigned long revolvingtime=0;

int redoff=1000;
int redon=30;
int whiteoff=1100;
int whiteon=25;
int twinLongOff=1050;
int twinOn=40;
int twinOff=80;
int revolvingperiode=2000;
int revolvingdisplace=500;
int value;

unsigned long twinStartMillis=0;
enum ledState {firstOnPeriod, shortInterval, secondOnPeriod, longInterval};
static ledState twinState=longInterval;

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(6,LOW);
    twinState=shortInterval;}
    break;
    case shortInterval:
    if(now-twinStartMillis>=twinOn+twinOff){
      digitalWrite(6,HIGH);
      twinState=secondOnPeriod;}
      break;
      case secondOnPeriod:
      if(now-twinStartMillis>=twinOn+twinOff+twinOn){
        digitalWrite(6,LOW);
        twinState=longInterval;}
        break;
        case longInterval:
        if (now-twinStartMillis>=twinLongOff){
        digitalWrite(6,HIGH);
        twinState=firstOnPeriod;
         twinStartMillis=now;}
}
 revolvingtime = millis();
  value = 128+127*cos(2*PI/revolvingperiode*revolvingtime);
   analogWrite(revolving, value);
}
  1. The way you are formatting your code makes it very difficult to read. Please use Tools->Auto Format in the Arduino IDE to format your code before posting it (or else format it manually in the conventional way).

  2. I don't understand what you mean by "make delay for revolving light (at the end of the sketch attached below) between cycles".

Sorry, I had no idea about something like conventional way of code writing (my knowledge of coding ended in 80's by Sinclair Spectrum and its BASIC). Now I used Auto Format function in IDE, hope it is better readable now.

const int redled=3;
const int whiteled=5;
const int twinred=6;
const int revolving=9;

unsigned long previousred=0;
unsigned long previouswhite=0;
unsigned long revolvingtime=0;

int redoff=1000;
int redon=30;
int whiteoff=1100;
int whiteon=25;
int twinLongOff=1050;
int twinOn=40;
int twinOff=80;
int revolvingperiode=2000;
int revolvingdisplace=500;
int value;

unsigned long twinStartMillis=0;
enum ledState {
  firstOnPeriod, shortInterval, secondOnPeriod, longInterval};
static ledState twinState=longInterval;

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(6,LOW);
      twinState=shortInterval;
    }
    break;
  case shortInterval:
    if(now-twinStartMillis>=twinOn+twinOff){
      digitalWrite(6,HIGH);
      twinState=secondOnPeriod;
    }
    break;
  case secondOnPeriod:
    if(now-twinStartMillis>=twinOn+twinOff+twinOn){
      digitalWrite(6,LOW);
      twinState=longInterval;
    }
    break;
  case longInterval:
    if (now-twinStartMillis>=twinLongOff){
      digitalWrite(6,HIGH);
      twinState=firstOnPeriod;
      twinStartMillis=now;
    }
  }
  revolvingtime = millis();
  value = 128+127*cos(2*PI/revolvingperiode*revolvingtime);
  analogWrite(revolving, value);
}

one of the lights is revolving light (in principle fading LED), and I wanted to achieve that LED fades in, fades out and stays off for 1sec. I highly appreciate your help. It's hard for me to learn those new things, but I try....I try to learn from others people code, try to understand and adapt for my needs....

Currently, you have this code which I presume is meant to do the fade:

  revolvingtime = millis();
  value = 128+127*cos(2*PI/revolvingperiode*revolvingtime);
  analogWrite(revolving, value);

I can see a couple of problems with this:

  1. There is no mechanism to keep the LED off for 1 second;

  2. You will get arithmetic overflow after a while, because not all unsigned long values can be converted to float on an Arduino (both unsigned long and float are 32 bits).

To solve (1), use a state machine.

To solve (2), one option is to make the fade linear instead of cosine. Then you can use integer maths. Your state machine can have 3 states: fade up, fade down, and off. Alternatively, if you want to keep the cosine fade, use 2 states (cosine fade, and off) and use a formula like this:

revolvingtime = now - fadeStartMillis;
value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiod);

The value converted from unsigned long to float is now revolvingTime rather than millis(), so it will be small enough to convert.

Maybe an ideal job for an arduino RTOS? http://arduino.cc/forum/index.php/topic,138643.0.html

Yes, although this application doesn't need a full-blown RTOS. A cooperative scheduler is simpler, uses less RAM, and is adequate for this sort of thing. Mine is at https://github.com/dc42/arduino.

Maybe I'm too dumb (actually I'm sure that I am) for this to solve, because I have rewritten my code after your kind advices, and now I'm again at point zero...I sure made many mistakes in there, because now my twin flashing strobo doesn't work anymore...and revolvinglight fades continously in and out. Moreover, I have such impression I have your hint about overflow in cosine fade not right implemented, because my current code has the same problem, if i see and understand the code flow correctly :~ =(

const int redled=3;
const int whiteled=5;
const int twinred=6;
const int revolving=9;

unsigned long previousred=0;
unsigned long previouswhite=0;
unsigned long revolvingtime=0;

int redoff=1000;
int redon=30;
int whiteoff=1100;
int whiteon=30;
int twinLongOff=1050;
int twinOn=40;
int twinOff=80;
int revolvingperiode=2000;
int value;
int FadeOff=1000;
int FadeOn=1000;

unsigned long twinStartMillis=0;
unsigned long fadeStartMillis=0;

enum ledState {
  firstOnPeriod, shortInterval, secondOnPeriod, longInterval};//state for twin flashing strobo
static ledState twinState=longInterval;

enum revolvingState {
  Fade, Off};     //state for revolving light
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){     //single flash strobo-works ok
    digitalWrite(redled, LOW);
  }
  if(currentred-previousred>=redoff){
    previousred=currentred; 
    digitalWrite(redled, HIGH);
  }
  if(currentwhite-previouswhite>=whiteon){  //single flash strobo-works ok
    digitalWrite(whiteled, LOW);
  }
  if(currentwhite-previouswhite>=whiteoff){
    previouswhite=currentwhite;
    digitalWrite(whiteled, HIGH);
  }
  unsigned long now=millis();
  switch (twinState){                 //twice flashing strobo
  case firstOnPeriod:                 //first flash
    if(now-twinStartMillis>=twinOn){
      digitalWrite(twinred,LOW);
      twinState=shortInterval;
    }
    break;
  case shortInterval:                 //first off
    if(now-twinStartMillis>=twinOn+twinOff){
      digitalWrite(twinred,HIGH);
      twinState=secondOnPeriod;
    }
    break;
  case secondOnPeriod:               //second flash
    if(now-twinStartMillis>=twinOn+twinOff+twinOn){
      digitalWrite(twinred,LOW);
      twinState=longInterval;
    }
    break;
  case longInterval:               //second off interval-long
    if (now-twinStartMillis>=twinLongOff){
      digitalWrite(twinred,HIGH);
      twinState=firstOnPeriod;

    }
    twinStartMillis=now;
    break;
  }
  switch (revolveState){           //revolving light off for 1sec
  case Off:
    if (now-fadeStartMillis>=Off){
      digitalWrite(revolving, LOW);
      revolveState=Fade;
    }
    break;
  case Fade:                       //revolving light on
    if (now-fadeStartMillis>=FadeOn){
      revolvingtime = now - fadeStartMillis;  //code for cosine fade
      value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiode);

      analogWrite(revolving, value);
      //revolveState=Off;  when uncommented, revolving light flashes rapidly
    }
  }
}
  case longInterval:               //second off interval-long
    if (now-twinStartMillis>=twinLongOff){
      digitalWrite(twinred,HIGH);
      twinState=firstOnPeriod;

    }
    twinStartMillis=now;
    break;

You need to move the assignment of twinStartMillis inside the if-statement. Also, you should replace "twinLongOff" by "twinOn+twinOff+twinOn+twinLongOff", unless twinLongOff is supposed to be the repetition period for the whole pattern (in which case, you should rename it).

  case Fade:                       //revolving light on
    if (now-fadeStartMillis>=FadeOn){
      revolvingtime = now - fadeStartMillis;  //code for cosine fade
      value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiode);

      analogWrite(revolving, value);
      //revolveState=Off;  when uncommented, revolving light flashes rapidly
    }

Incorrect logic. You need something along these lines:

If it is time to finish the FadeOn part of the cycle then set state back to Off, turn LED off and reset fadeStartMillis to now else do cosine calculation and analogWrite

Sorry to bother again and again…I’m ashamed of myself for not being able to get it right, but still no success on my side =( twin flashing strobo works again, but i cannot get the revolvinglight to make pause between duty cycles. I have tried everything what came to my mind, but my revolving light only fades continuously in and out… Damn! Possible my brain isn’t flexible enough for this kind of logic thinking.

const int redled=3;
const int whiteled=5;
const int twinred=6;
const int revolving=9;

unsigned long previousred=0;
unsigned long previouswhite=0;
unsigned long revolvingtime=0;

int redoff=1000;
int redon=30;
int whiteoff=1100;
int whiteon=30;
int twinLongOff=1550;
int twinOn=40;
int twinOff=100;
int revolvingperiode=2000;
int value;
int FadeOff=1000;
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 Off: 
    //if (now-fadeStartMillis<FadeOn){  
    //revolvingtime = now - fadeStartMillis;
    // value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiode);
    // analogWrite(revolving, value);
    digitalWrite(revolving, LOW);
    revolveState=Fade;
    //}
    //  break;

  case Fade:
    if (now-fadeStartMillis>=FadeOn){
      revolveState=Off;
      digitalWrite(revolving, LOW);
      fadeStartMillis=now;
    }
    else revolvingtime = now - fadeStartMillis;
    value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiode);
    analogWrite(revolving, value);
    break;
  }
}

Try (warning: untested code!):

  switch (revolveState){           
  case Fade:
    if (now-fadeStartMillis >= FadeOn)
    {
      revolveState=Off;
      digitalWrite(revolving, LOW);
    }
    else
    {
      revolvingtime = now - fadeStartMillis;
      value = 128 - 128*cos((2*PI*(float)revolvingtime)/revolvingperiode);
      analogWrite(revolving, value);
    }
    break;
  case Off: 
    if (now-fadeStartMillis > FadeOn + FadeOff)
    {  
       revolveState=Fade;
       fadeStartMillis=now;
    }
    break;
  }

Thank you!!! That's it! It's working now. Now I will study your piece of code line by line and hopefully will learn something from it. You saved my day, now I can sleep peacefully. Excellent!

I LIKE this sketch! What needs to be added to have a second set of strobes that flash opposite of the currect ones? i.e. left wing tip flash twice and then right wing tip flash twice! Dave