Blinking LED, What's different?

const int LED13 = 13;
const int LED12 = 12;
unsigned long currentMillis = 0;
unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int ledState13 = LOW;
int ledState12 = LOW;
const int _700MS = 700;
const int _500MS = 500;
const int _300MS = 300;
const int _200MS = 200;
boolean IsLED12True = false;
boolean IsLED13True = false;
int countLED12[2] = {0};
int countLED13[2] = {0};

void setup() {
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
Serial.begin(9600);
}

boolean led(boolean IsLED13True){
if (IsLED13True == true) {
previousMillis13[1] = previousMillis13[0];
if (currentMillis - previousMillis13[1] >= _200MS) {
previousMillis13[0] = currentMillis;
if (ledState13 == LOW) ledState13 = HIGH;
else ledState13 = LOW;
digitalWrite(LED13, ledState13);
countLED13[1]++;
if (countLED13[1] == 6) {
countLED13[1] = 0;
IsLED13True = false;
}
}
}
}

void loop() {
currentMillis = millis();

if (currentMillis - previousMillis13[0] >= _500MS && countLED13[0] != 4) {
previousMillis13[0] = currentMillis;
if (ledState13 == LOW) ledState13 = HIGH;
else ledState13 = LOW;
digitalWrite(LED13, ledState13);
countLED13[0]++;
if (countLED13[0] == 4)
IsLED13True = true;
}
led(IsLED13True);
}


const int LED13 = 13;
const int LED12 = 12;
unsigned long currentMillis = 0;
unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int ledState13 = LOW;
int ledState12 = LOW;
const int _700MS = 700;
const int _500MS = 500;
const int _300MS = 300;
const int _200MS = 200;
boolean IsLED12True = false;
boolean IsLED13True = false;
int countLED12[2] = {0};
int countLED13[2] = {0};

void setup() {
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
Serial.begin(9600);
}

//boolean led(boolean IsLEDTrue[0], unsigned long currentMillis, const int SEC, int BLINK) {

void loop() {
currentMillis = millis();

if (currentMillis - previousMillis13[0] >= _500MS && countLED13[0] != 4) {
previousMillis13[0] = currentMillis;
if (ledState13 == LOW) ledState13 = HIGH;
else ledState13 = LOW;
digitalWrite(LED13, ledState13);
countLED13[0]++;
if (countLED13[0] == 4)
IsLED13True = true;
}

if (IsLED13True == true) {
previousMillis13[1] = previousMillis13[0];
if (currentMillis - previousMillis13[1] >= _200MS) {
previousMillis13[0] = currentMillis;
if (ledState13 == LOW) ledState13 = HIGH;
else ledState13 = LOW;
digitalWrite(LED13, ledState13);
countLED13[1]++;
if (countLED13[1] == 6) {
countLED13[1] = 0;
IsLED13True = false;
}
}
}
}

=======================
Number 1 is after put in the led function and
Number 2 is before put in the led function

I want to make like Number 1 but it doesn't work what I want like Number 1.

Oops.. my mistakes, Number 1 is Number 2, Number 2 is Number 1 Sorry...

I want to make like Number 1 but it doesn't work what I want like Number 1.

I can't make much sense of this. Are you saying that sketch 1 does not work ? If so, what does it do and what should it do ?

Oops.. my mistakes, Number 1 is Number 2, Number 2 is Number 1 Sorry...

Now I am even more confused.

What are you trying to do and with which sketch ?

I think you have to explain what you are trying to do with the sketch.

For starters, lets get code in CODE format shall we? Its not frigging rocket science!!

1:-

const int LED13 = 13;
const int LED12 = 12;
unsigned long currentMillis  = 0;
unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int ledState13 = LOW;
int ledState12 = LOW;
const int _700MS = 700;
const int _500MS = 500;
const int _300MS = 300;
const int _200MS = 200;
boolean IsLED12True = false;
boolean IsLED13True = false;
int countLED12[2] = {0};
int countLED13[2] = {0};


void setup() {
  pinMode(LED12, OUTPUT);
  pinMode(LED13, OUTPUT);
  Serial.begin(9600);
}

//boolean led(boolean IsLEDTrue[0], unsigned long currentMillis, const int SEC, int BLINK) {

void loop() {
  currentMillis = millis();

  if (currentMillis - previousMillis13[0] >= _500MS && countLED13[0] != 4) {
    previousMillis13[0] = currentMillis;
    if (ledState13 == LOW) ledState13 = HIGH;
    else ledState13 = LOW;
    digitalWrite(LED13, ledState13);
    countLED13[0]++;
    if (countLED13[0] == 4)
      IsLED13True = true;
  }

  if (IsLED13True == true) {
    previousMillis13[1] = previousMillis13[0];
    if (currentMillis - previousMillis13[1] >= _200MS) {
      previousMillis13[0] = currentMillis;
      if (ledState13 == LOW) ledState13 = HIGH;
      else ledState13 = LOW;
      digitalWrite(LED13, ledState13);
      countLED13[1]++;
      if (countLED13[1] == 6) {
        countLED13[1] = 0;
        IsLED13True = false;
      }
    }
  }
}

2:-

const int LED13 = 13;
const int LED12 = 12;
unsigned long currentMillis  = 0;
unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int ledState13 = LOW;
int ledState12 = LOW;
const int _700MS = 700;
const int _500MS = 500;
const int _300MS = 300;
const int _200MS = 200;
boolean IsLED12True = false;
boolean IsLED13True = false;
int countLED12[2] = {0};
int countLED13[2] = {0};


void setup() {
  pinMode(LED12, OUTPUT);
  pinMode(LED13, OUTPUT);
  Serial.begin(9600);
}

boolean led(boolean IsLED13True){
  if (IsLED13True == true) {
    previousMillis13[1] = previousMillis13[0];
    if (currentMillis - previousMillis13[1] >= _200MS) {
      previousMillis13[0] = currentMillis;
      if (ledState13 == LOW) ledState13 = HIGH;
      else ledState13 = LOW;
      digitalWrite(LED13, ledState13);
      countLED13[1]++;
      if (countLED13[1] == 6) {
        countLED13[1] = 0;
        IsLED13True = false;
      }
    }
  }
}

void loop() {
  currentMillis = millis();

  if (currentMillis - previousMillis13[0] >= _500MS && countLED13[0] != 4) {
    previousMillis13[0] = currentMillis;
    if (ledState13 == LOW) ledState13 = HIGH;
    else ledState13 = LOW;
    digitalWrite(LED13, ledState13);
    countLED13[0]++;
    if (countLED13[0] == 4)
      IsLED13True = true;
  }
  led(IsLED13True);
}

Right so if I understood correctly, original No1 is now No2 and original No2 is now No1. Now what was the original question? Consider plenty of people likely to have hangovers!! ::slight_smile: :sunglasses:

My first observation would be that these arrays are not completely initialised....

unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int countLED12[2] = {0};
int countLED13[2] = {0};

If your intent was to initialise all elements to 0, then you would need this....

unsigned long previousMillis13[2] = {0, 0};
unsigned long previousMillis12[2] = {0, 0};
int countLED12[2] = {0, 0};
int countLED13[2] = {0, 0};

As written by OP, the result is 2 long flashes then repeating short flashes.

But, maybe he wanted 2 long flashes then 3 short flashes then nothing?

IsLED13True is declared as a global. It is then declared as the input variable to the led function, within the led function based on his criteria, the variable is set to false, which of course is only valid within the function, outside of the function the original value will remain unchanged....

What I am trying to say is :-

By doing this..... The entire sketch function is altered dramatically....

boolean led(boolean IsLED13Truea) {  // <=== IsLED13True is now copied in IsLED13Truea
  if (IsLED13Truea == true) { // <=== Check local copy of global IsLED13True
    previousMillis13[1] = previousMillis13[0];
    if (currentMillis - previousMillis13[1] >= _200MS) {
      previousMillis13[0] = currentMillis;
      if (ledState13 == LOW) ledState13 = HIGH;
      else ledState13 = LOW;
      digitalWrite(LED13, ledState13);
      countLED13[1]++;
      if (countLED13[1] == 6) {
        countLED13[1] = 0;
        IsLED13True = false; // <= Change ACTUAL global IsLED13True
      }
    }
  }
}

Not bad following New years eve :wink: ?

Happy New Year all,

Graham

My first observation would be that these arrays are not completely initialised....

Code: [Select]
unsigned long previousMillis13[2] = {0};
unsigned long previousMillis12[2] = {0};
int countLED12[2] = {0};
int countLED13[2] = {0};

Is it not the case that all the elements of global arrays where the elements not specifically initialised are set to zero ?

A post by AWOL recently suggested that you should't make that assumption.

Graham

Come to think of it I do remember seeing something like that. Thanks for the reminder.

If one element of the array is specified in the initialisation list but the other elements are not specified, then the other elements are initialised to the default value for the array type. For numeric arrays, this is 0.

So {0} has the effect of initialising the first element explicitly to 0 and the remaining elements implicitly to 0.

In comparison, {1} would initialise the first element to 1 and the others to 0. {} would not initialise any elements.

http://stackoverflow.com/questions/706030/are-uninitialized-struct-members-always-set-to-zero has discussion on this and references to the C++ standard.

Hi HS,

That makes sense, and in the absence of AWOL's post I saw, I cannot confirm or deny the circumstances were the same... So thanks for the info :smiley:

Happy New Year,

Graham

ghlawrence2000:
A post by AWOL recently suggested that you should't make that assumption.

Graham

Don't misquote me (I said no such thing - all statics and globals are initialised by crt0) , and mind your language.

We're all frustrated by spammers, @ghlawrence2000, but you'd be better off reporting them, rather than responding to them.

Apologies for the language....

I was quite sure I had seen a post by you about the initialisation of arrays, I was not deliberately trying to misquote you! Sorry. :slightly_frowning_face:

Regards,

Graham