if else and the brackets - Question regarding actions

Hello, i am very new to the arduino community.
I tried the search but don't found an answer.

My question is regarding an I hope easy code.

I want to turn on an LED with one time pushing a button and pushing again the LED turns off.
The code is working very well but I don't understand the actions in detail at one specific point.

 if (readingbutton == HIGH && previousutton == LOW && millis() - timeLED > debounce) {
    if (LEDState == HIGH)
      LEDState = LOW;
    else
      LEDState = HIGH;

    timeLED = millis();
}

when the button is pushed the code checks if the the LED is on or off.
If on turn it off
if off turn it on.

When does the code set timeLED = millis() ?

only if the first if is true, or?
or only if LEDSate is LOW so the path is going through the else?

Thank you very much, I enjoyed always the moment when my code is working like I want. :slight_smile:

Your code is equivalent to this

 if (readingbutton == HIGH && previousutton == LOW && millis() - timeLED > debounce) {
    if (LEDState == HIGH)
    {
      LEDState = LOW;
    }
    else
    {
      LEDState = HIGH;
    }
    timeLED = millis();
}

The else belongs to the second if and both don't have curlies.

Hence the timeLed is not affected by the second if or the else and set when the first if is true.

You are very friendly guys, helping a beginner like me, very well. :slight_smile:

AWOL:
Your code is equivalent to this

This is what I expected, but don't know exactly.

sterretje:
The else belongs to the second if and both don't have curlies.

Hence the timeLed is not affected by the second if or the else and set when the first if is true.

So if I want to take timeLED only belonging to the else I should use brackets and take it in the else brackets, right?

agentsmith1612:
So if I want to take timeLED only belonging to the else I should use brackets and take it in the else brackets, right?

Yes.

Personally I find it advisable / clearer to always use brackets.

agentsmith1612:
You are very friendly guys, helping a beginner like me, very well

We don't often see beginners that try to understand :slight_smile:

Take a look at 'Saint' Nick's page:

See "Style Guide" section, oh what the heck, read the whole thing.

.

sterretje:
Yes.

Personally I find it advisable / clearer to always use brackets.

Yes, now i realise that it is much clearer to read and to understand what happen when. In the future I am going to use always the brackets.

sterretje:
We don't often see beginners that try to understand :slight_smile:

Thank you very much.

LarryD:
Take a look at 'Saint' Nick's page:
Gammon Forum : Electronics : Microprocessors : Arduino programming traps, tips and style guide

See "Style Guide" section, oh what the heck, read the whole thing.

.

There are very good tipps and I found some "bad" code in my own examples that I could correct now or optimize.
Thanks, that is a very helpfull side with a lot of tipps (I only understand a little bit of this codes but that codes which I understand a very helpfull at all.)

:slight_smile: :slight_smile: :slight_smile:

Now you guys have clarified my problem and gave some tipps for better coding in the future.

Next steps can come. :slight_smile: