If else only working with var declaration as global

Good day.
I have a problem with an if .. else not executing when declaring var local in loop.
This does not work, only executes the else part:

#include <Arduino.h> //needed for Serial.println
int CLK2 = 5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);

  pinMode(CLK2, OUTPUT);
}

void loop() {
  bool CLK2_State;
    if (CLK2_State == true) 
    {
      CLK2_State = false;
      Serial.println(CLK2_State);
      digitalWrite(CLK2, LOW);
      delay(1);
    }
    else 
    {
      CLK2_State = true;
      Serial.println(CLK2_State);
      digitalWrite(CLK2, HIGH);
      delay(1);
    }
}

This works as intended toggle the pin.

#include <Arduino.h> //needed for Serial.println
int CLK2 = 5;
bool CLK2_State;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);

  pinMode(CLK2, OUTPUT);
}

void loop() {
    if (CLK2_State == true) 
    {
      CLK2_State = false;
      Serial.println(CLK2_State);
      digitalWrite(CLK2, LOW);
      delay(1);
    }
    else 
    {
      CLK2_State = true;
      Serial.println(CLK2_State);
      digitalWrite(CLK2, HIGH);
      delay(1);
    }
}

Only the bool declaration moved, tried with an int, sane problem.

Regards

Should probably added using Ver 1.8.2

that's because your boolean is reset every time you loop... read about scope and lifetime of your variables.

if you want to remember the value of CLK2_State across calls to the loop() function then you need to declare it static

And as you are learning small keywords, learn as well the const keyword: your pin number CLK2 will never change, right? so tell this to the compiler by using const int CLK2 = 5;

As pin numbers are usually most of the time below 255 and never negative, there is no need to use an int to store a pin number - a byte will do so usingconst byte CLK2 = 5;is even better.

Lastly it's good practice to give meaningful names to variables, so we tend to add the word 'pin' to variables names that refer to a pin number soconst byte CLK2Pin = 5; would be great !

Thank you.
I assumed wrongly :confused: that the loop function is the same as the main in traditional compilers, where var's declared in main maintain their values until exiting.

By declaring the bool as static, it works.
I now understand that loop gets called periodically and it is not constantly in this function.

Hope I understand loop correctly.

Regards

AlanMiller:
Hope I understand loop correctly.

You do - actually the Arduino IDE generates a main() for you in the background

you can see it does (removing a bit of the extra stuff to keep it simple - the link above is the real thing)

int main(void)
{
   setup();

   for (;;) {
      loop();
   }     
   return 0;
}

so basically calls setup() once and then loop() in an infinite for loop - so it's a function call and thus indeed you depend on variable scope / lifetime for what you put in the loop() function

AlanMiller:
Thank you.
I assumed wrongly :confused: that the loop function is the same as the main in traditional compilers, where var's declared in main maintain their values until exiting.

In both cases, variables declared in the function maintain their values until exiting. There is no special difference in that behaviour between main() and loop(). The compiler treats them identically in that respect.

aarg:
In both cases, variables declared in the function maintain their values until exiting. There is no special difference in that behaviour between main() and loop(). The compiler treats them identically in that respect.

100% correct

I think AlanMiller wanted to say that the apparence for the developer is that the variables declared in main() are lasting "forever" because they live until the end of the program (and then there is nothing left to see)

AlanMiller:
Thank you.
I assumed wrongly :confused: that the loop function is the same as the main in traditional compilers, where var's declared in main maintain their values until exiting.

By declaring the bool as static, it works.
I now understand that loop gets called periodically and it is not constantly in this function.

Hope I understand loop correctly.

Regards

You will also understand why its called loop and not main then!! Its the event loop processor. Google will
explain event loops.

void loop() {
  bool CLK2_State;
    if (CLK2_State == true)

Local variables are NOT initialized by the compiler. If you don't provide an initial value, it is stupid to assume that the variable holds any particular value.