Newbie to Arduino Code works, but don't know why (see more)

Hi,

Just wanted to say thanks ahead of time.

OK, I wrote some code (which is a mashup of some of the examples) and I just want some assistance on WHY it works the way it does.

/* Project to make the second light blink on every 4th blink */

const int ledPin1 = 12; //declare ledPin1 as pin 12
const int ledPin2 = 13; // declare ledPin2 as 13
int ledCount = 0; //times light has flashed 
long previousMillis = 0;
int ledState1 = LOW;
int ledState2 = LOW;
long interval = 1000;


void setup() {
  pinMode(ledPin1, OUTPUT);  //declare both pins as outputs
  pinMode(ledPin2, OUTPUT);
//  Serial.begin(9600); 
  
}

void loop() {
  unsigned long currentMillis = millis();
  Serial.println(ledState2);

   
    
      if (currentMillis - previousMillis > interval) {
        previousMillis = currentMillis; 
        
        if (ledState1 == LOW) {
          ledState1 = HIGH;
         ledCount++;   //adds +1 to the variable 
        }else {
          ledState1 = LOW;
          ledState2 = LOW;
        
        
        }
digitalWrite(ledPin2, ledState2);
 digitalWrite(ledPin1, ledState1);       

    
}
   if (ledCount % 3  == 0){
     ledState2 = HIGH;
   }else{
     ledState2 = LOW;
   }

}

Ok, this code will do exactly what I am wanting. on the 3rd blink ledPin2 will blink with ledPin1. They will both go on and off at the same time.

/* Project to make the second light blink on every 4th blink */

const int ledPin1 = 12; //declare ledPin1 as pin 12
const int ledPin2 = 13; // declare ledPin2 as 13
int ledCount = 0; //times light has flashed 
long previousMillis = 0;
int ledState1 = LOW;
int ledState2 = LOW;
long interval = 1000;


void setup() {
  pinMode(ledPin1, OUTPUT);  //declare both pins as outputs
  pinMode(ledPin2, OUTPUT);
//  Serial.begin(9600); 
  
}

void loop() {
  unsigned long currentMillis = millis();
  Serial.println(ledState2);

   
    
      if (currentMillis - previousMillis > interval) {
        previousMillis = currentMillis; 
        
        if (ledState1 == LOW) {
          ledState1 = HIGH;
         ledCount++;   //adds +1 to the variable 
        }else {
          ledState1 = LOW;
          ledState2 = LOW;
        
        
        }
}
   if (ledCount % 3  == 0){
     ledState2 = HIGH;
   }else{
     ledState2 = LOW;
   }
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin1, ledState1);
}

This piece of code will act differently, on the third blink ledPin2, and ledPin1 will both light up at the same time but when ledPin1 turns off, ledPin2 stays on until the next blink cycle.

For example, pin1 on, pin1 off, pin1 on and pin2 on, pin1 off, pin1 on and pin2 off.

I'm newer to programming and electronics. Like I said I got it to work with the intended results I originally wanted, but I want to know why positioning matters so to speak.

Remember that changing ledState1 and ledState2 doesn't have an effect on the lights. Only the digitalWrite() lines can change the lights.

In the first example you are changing both lights and then modifying ledState2.

In the second example note that LED2 is on for any blink cycle where ledCount is a multiple of 3 and ledCount only changes when LED1 goes on, not when it goes off.

Here is some code that will give the blink pattern you want:

LED 1: ON OFF ON OFF ON OFF
LED 2: OFF OFF OFF OFF ON OFF

    digitalWrite(ledPin1, ledCount == 0 || ledCount == 2 || ledCount == 4);
    digitalWrite(ledPin2, ledCount == 4);
    ledCount++;   //adds +1 to the variable 
    ledCount %= 6;  //  Keep it in the range 0 to 5

johnwasser:
Remember that changing ledState1 and ledState2 doesn't have an effect on the lights. Only the digitalWrite() lines can change the lights.

In the first example you are changing both lights and then modifying ledState2.

In the second example note that LED2 is on for any blink cycle where ledCount is a multiple of 3 and ledCount only changes when LED1 goes on, not when it goes off.

Here is some code that will give the blink pattern you want:

LED 1: ON OFF ON OFF ON OFF
LED 2: OFF OFF OFF OFF ON OFF

    digitalWrite(ledPin1, ledCount == 0 || ledCount == 2 || ledCount == 4);

digitalWrite(ledPin2, ledCount == 4);
    ledCount++;   //adds +1 to the variable
    ledCount %= 6;  //  Keep it in the range 0 to 5

Hey well thank you for the reply, I really appreciate it (don't understand the code, but that's why I'm learning).

Well, The first one works perfectly fine. I know what you mean about modifying ledState2 after the digitalWrites, this is what has me confused. the first code works perfectly fine, and the second code does not.

(I liked your led diagram better.)

First one (as done on the arduino:

LED1: on off on off on off
LED2: off off off off on off

Second one:
LED1: on off on off on off on off on off on off
LED2: off off off off on on on off off off on on...

What's going on is it does not make sense to me why the first one works, and the 2nd one does not. (I'm sorry I'm being a dunce here).

AS I understand it, the code should go:

Initializing Variables

Ask for the time the board has been on, compare the time

if the led is off set to on (and increase the counter)
if it HIGH on set ledstate1/2 to low

if the counter is divisble by 3 set the pin to high.

then post the results

What I don't understand is why that doesn't work, but if I change posting the results to between the two statements.

Does this make sense?

One way to figure out things like this is to 'play computer' and go through the cycles on paper.

In your examples you start with:
ledCount=0, ledState1=LOW, ledState2=LOW
Let's abbreviate that as "0,LOW,LOW"

In the first pass through the timed code:

ledStat1 is LOW so:
ledState1 is set to HIGH
ledCount is set to 1
ledCount%3 == 1
ledState2 is set to LOW
LED 1 is turned on
LED 2 is turned off
the state is now 1,HIGH,LOW

In the second pass through the timed code:

ledStat1 is HIGH so:
ledState1 is set to LOW
ledState2 is set to LOW
ledCount%3 == 1
ledState2 is set to LOW
LED 1 is turned off
LED 2 is turned off
the state is now 1,LOW,LOW

In the third pass:

ledStat1 is LOW so:
ledState1 is set to HIGH
ledCount is set to 2
ledCount%3 == 2
ledState2 is set to LOW
LED 1 is turned on
LED 2 is turned off
the state is now 2,HIGH,LOW

In the fourth pass:

ledStat1 is HIGH so:
ledState1 is set to LOW
ledState2 is set to LOW
ledCount%3 == 2
ledState2 is set to LOW
LED 1 is turned off
LED 2 is turned off
the state is now 2,LOW,LOW

In the fifth pass:

ledStat1 is LOW so:
ledState1 is set to HIGH
ledCount is set to 3
ledCount%3 == 0
ledState2 is set to HIGH
LED 1 is turned on
LED 2 is turned on
the state is now 3,HIGH,HIGH

In the sixth pass:

ledStat1 is HIGH so:
ledState1 is set to LOW
ledState2 is set to LOW
ledCount%3 == 0
ledState2 is set to HIGH
LED 1 is turned off
LED 2 is turned on
the state is now 1,LOW,HIGH

In the seventh pass:

ledStat1 is LOW so:
ledState1 is set to HIGH
ledCount is set to 4
ledCount%3 == 1
ledState2 is set to LOW
LED 1 is turned on
LED 2 is turned off
the state is now 2,HIGH,LOW

As you can see, ledCount only gets changed every other pass. The ledState2=LOW in the first IF is intended to turn off the light in the second half of each cycle but the IF after that turns ledState2 back on because ledCount is still 3.

To make it explicit that LED 2 should not be on when LED 1 is off, change the second IF to

if ((ledCount % 3 == 0) && ledState1 == HIGH)

I freaking finally got it (dunce).

since the counter does not count when the light is already on, the 2nd if statement is still true when it is run.

(only hours lost on something stupid like that).

I appreciate you explaining that to me.