Variable changes without interaction

Hello all,

I’m currently teaching myself programming by developing a car-park like simulation with entry gates, traffic lights, etc but I’m currently stuck at one point and would really appreciate it, if some could explain to me, why the program behaves like it does.

With the code below, I’m reading an analog value from a lightbarrier within a 5s time limit to detect wether a car successfully passed through the gates. To do so, I created the two variables ‘falling’ and ‘rising’ which represent the falling and rising edges of the lightbarrier.

void car_entry() {
  entr_switch_state = false;
  if(parkingslots<1) {
    return;
  }  
  boolean falling, rising = false;
  
  long int start = millis();
  Serial.println(falling);
  Serial.println(rising);
  while ((millis()-start)<5000) {
    int x = analogRead(pin_entry_lb);
    
    if ((x<entry_lb_threshold_l) && rising==false) {
      Serial.println("TEST");
      falling = true;
    }

    if ((x>entry_lb_threshold_h) && falling==true) {
      rising = true;
    }     
  }

  Serial.println(falling);
  Serial.println(rising);

Now I have the following issue:
The read analog value is always greater than entry_lb_threshold_l which is why (after my understanding), the if-statement in the while-loop shouldn’t get executed.
But I get the following output on the serial monitor:

0
0
1
1

It shows that the if-statement was not executed as the “TEST” statement also was not printed. However, the two boolean values still flip.
I discovered that when declaring those two variables outside the function, everything behaves as it’s supposed to and the two booleans do not change.

I would appreciate it, if somebody could explain this behaviour to me.
Thanks in advance

boolean falling, rising = false; Are you aware that only “rising” is initialised?

Why don’t you add a print statement to show the value that is collected by analogRead(). Presumably it is not what you think it is.

When writing print statements it is good practice to include text so you know what is being printed. For example, instead of

 Serial.println(falling);

put

 Serial.print("Initial value of FALLING ");
  Serial.println(falling);

Also, don’t use single-character names for variables as it makes code very difficult to search for all instances of that variable when problems arise.

This style of code

  while ((millis()-start)<5000) {

blocks the Arduino from doing other things until it completes. Usually it is better to use IF and let loop() do the repetition.

Variables used with millis() should be unsigned long.

If you need more help please post the complete program. If it is long then a better idea is to make a short program that illustrates the problem. Doing so often brings the cause of the problem to light.

…R

TheMemberFormerlyKnownAsAWOL:
boolean falling, rising = false; Are you aware that only “rising” is initialised?

That’s a great hint, thank you very much. I actually wasn’t aware that only the second variable gets initiated - I always assumed that both variables get initated with 0.

I changed the code to boolean falling = false, rising = false; and everything started working as intended.

So is it correct to say, that the error was caused by an uninitialized variable which then just displayed random data at its specific memory address?

Robin2:
Why don’t you add a print statement to show the value that is collected by analogRead(). Presumably it is not what you think it is.

When writing print statements it is good practice to include text so you know what is being printed. For example, instead of

 Serial.println(falling);

put

 Serial.print("Initial value of FALLING ");

Serial.println(falling);




Also, don't use single-character names for variables as it makes code very difficult to search for all instances of that variable when problems arise.

This style of code


while ((millis()-start)<5000) {



blocks the Arduino from doing other things until it completes. Usually it is better to use IF and let loop() do the repetition.

Variables used with millis() should be unsigned long.

If you need more help please post the complete program. If it is long then a better idea is to make a short program that illustrates the problem. Doing so often brings the cause of the problem to light.

...R

Thank you for your suggestions, I will definitely try to change that while loop for a non-blocking timing solution.

json8: That's a great hint, thank you very much. I actually wasn't aware that only the second variable gets initiated - I always assumed that both variables get initated with 0.

I believe that would be correct if you were creating global variables, but you were creating a local variable. Presumably every time the function is called the un-initiated variable has whatever value happened to be at the memory location allocated to it.

...R

Robin2:
I believe that would be correct if you were creating global or static variables, but you were creating a local variable. Presumably every time the function is called the un-initiated variable has whatever value happened to be at the memory location allocated to it.

…R

Thanks for the clarification. I was not sure how static local variables are treated.

…R