If statement do not work

Hey guys! I'm here with a really strange problem. The code you see below does not work like it should (or at least as my programming skills says so). Code inside "if" statement runs nonstop, while it should run once and wait till "temporary" integer equals zero? The rest of a code is commented out, to not make anything as I debug this issue.

  int temporary = 0;
  if(temporary == 0){
    radio.openWritingPipe(pipe);
    radio.startWrite(sdata, sizeof(sdata));
    Serial.print("Sent ");
    Serial.println(sdata[1]);
    sdata[1]++;
    temporary++; // or temporary = 1;
    //Recieve();
  }

The code you see below does not work

The code below is not complete. It does not compile or run. I appreciate the use of code tags and an attempt to post a reduced piece of code which demonstrates the problem, but what you have posted in not that.

Please read the How to use this forum posting. http://forum.arduino.cc/index.php/topic,148850.0.html

Hi and welcome to the forum.

Is the whole code in your "loop" program ? As you know, the "loop" program gets executed over and over, right ?

One of the very last lines to be executed is "temporary++;" This line increments the "temporary" variable, as a result, it is no longer equal to "0" and should not executed the 'if' block of code.

Question: what is the very first' line to be executed ? (hint: that line is right *before your "if" statement.)

Once that line is executed (int temporary = 0;) , what is the value of the "temporary" variable ? What is its value once it comes to the 'if" statement ?

Solution: you should put the line "int temporary = 0;" in the setup section so that the "temporary" variable gets executed only once.

I hope it helps !

  • dan

(karma points definitely accepted...)

That's partially correct, but the wrong solution.

Your temporary variable is created inside the loop function and is therefore local to the loop function. Google "C++ scope" for a more thorough explanation of what that means. One of the consequences of that is that this variable is destroyed every time loop exits and is recreated when loop restarts. So it will always be equal to zero when it gets to that if statement since it is right before it.

There are two possible solutions. Putting the word "static" in front of the variable definition tells the program to keep that variable so it lives on between function calls. The other solution is to make it global. Define it above setup, outside of any function, so that it is not part of the loop function but can be accessed from there.

If you define the variable in setup, it would only exist there and would be destroyed once setup exits. That is definitely not what you want to do.

there's a difference between this:

for(;;) {
  int temp = 0;
  if(temp == 0) {
    temp ++;
  }
}

and this:

int temp = 0;
for(;;) {
  if(temp == 0) {
    temp ++;
  }
}

Sorry guys for dumb question that I did. After a post I found the mistake myself, but didn't write anything back to this thread because I was waiting a notification about it :D I got used to other forums where they send notifications for every action you're involved :D Thanks for everybody who responded :)

I got used to other forums where they send notifications for every action you're involved

That's the way it is here too. Oh, that's right, since the new and improved software was installed that was taken out along with other things that everyone liked.