Binary counter with 2 leds

Hello ! I am starting learning Programming Arduino with the book "programmer arduino en s'amusant" and it is the first time i write on this forum. The project is a binary counter with 2 leds. The leds are showing the counting from 0 to 3. I try to understand what is happening behind the code but I don't figure it out. Please can someone explain me step by step what is happening ?

When it counts 0, led 2 and led 1 are respectivelly at 0 and 0 states
When it counts 1, led 2 and led 1 are respectivelly at 0 and 1
When it counts 2, led 2 and led 1 are respectivelly at 1 and 0
When it counts 3, led 2 and led 1 are respectivelly at 1 and 1
etc...

bEtatU is the state for led1
bEtatD is the state for led2
bPrecU is the state before

Here is the code :

byte yBroLED1 = 11;
byte yBroLED2 = 10;
byte yAnaOUI = 100; // PWM
byte yAnaNON = 0;

boolean bEtatU = 0;
boolean bPrecU = 0;
boolean bEtatD = 0;

void setup() {
  pinMode(yBroLED1, OUTPUT);
  pinMode(yBroLED2, OUTPUT);
  // Aucune animation for
}

void loop() {
  bPrecU = bEtatU;

  if (!bEtatU) analogWrite(yBroLED1, yAnaOUI);
  else analogWrite(yBroLED1, yAnaNON);

  bEtatU = !bEtatU;

  if ( (!bEtatU) && (bPrecU) ) bEtatD = !bEtatD;


  if (bEtatD) analogWrite(yBroLED2, yAnaNON);
  else analogWrite(yBroLED2, yAnaOUI);

  delay(1000);
}

Thanks in advance for any help

I try to understand what is happening behind the code but I don't figure it out. Please can someone explain me step by step what is happening ?

explain what you tried to achieve and what you see. I don't see any count going from 0 to 3...


please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Hi, @al22
Welcome to the forum.
Did you have a read of the explanation in the chapter before the code listing?

Tom... :grinning: :+1: :coffee: :australia:

certainly a very convoluted way of implementing a binary counter using PWM and two separate boolean values

a more convention implementation would be

    digitalWrite (yBroLED1, cnt & 1);
    digitalWrite (yBroLED2, cnt & 2);
    cnt ++;
1 Like

Actually, the programme is working fine and the leds are working. My question is to understand what each line of the code produce on the leds.

Start at the beginning. Which line is the first line you can't figure out?

1 Like

the one boolean is toggled each cycle

    bEtatU = !bEtatU;

the other boolean, bEtatD, is only toggled when the first, bEtaU, is set (1) (using a 3rd boolean bPrecU)

    if ( (!bEtatU) && (bPrecU) )
        bEtatD = !bEtatD;

Here is a slightly simpler version that does the same thing:

void loop()
{
  bEtatU = !bEtatU;  // 1->0, 0->1

  if (bEtatU == false)
    bEtatD = !bEtatD;    // 1->0, 0->1
    
  if (bEtatU)
    analogWrite(yBroLED1, yAnaOUI);
  else
    analogWrite(yBroLED1, yAnaNON);

  if (bEtatD)
    analogWrite(yBroLED2, yAnaOUI);
  else
    analogWrite(yBroLED2, yAnaNON);

  delay(1000);
}

If the rest of the examples are of similar quality, I suggest buying another book!

+1 - this book is meant for 10 year old and above and uses cryptic code.... that's a nonsense. throw that book away

I noticed that there is a lack of explanations in my book. Do you have any suggestions for other books in french or english ?

The Arduino Cookbook is the book I've seen mentioned here most often - I have a copy and like it.

I would suggest to use on line resource, starting with les tutos d'eskimon

Hello
Well, my recommendation is to run all IDE examples.
And the next step is to think about your own project to apply what you have learned.

That looks really cool ! Thanks

If you are trying to figure out what each line of code means, then I suggest that it is best to buy an introductory book on Arduino programming to learn.

there's i good book on programming that discusses code like this

... as a bad example of coding.