Problems setting variable values - please help

Working on a project for my son's school and running into some unusual behavior with variables. Please help (excuse the code, it is very messy and copied and pasted from different sources):

Variable "int functionstatus;" is defined on line 53 and then it is assigned value of 1 on line 125 (under void setup). Unfortunately, when running the code, the value ends up 0 (looking through serial monitor). Cannot figure out what causes it to reset to 0 or why it cannot recognize that value of 1 was assigned under void setup. Additional info:
----a) The code was working fine until recently, it just started doing this after making a minor change unrelated to this particular variable.
----b) All other variables defined and assigned before or within void setup work fine.
----c) the functionstatus variable holds the value fine once it is assigned through void loop. it just does not take an assignment from void setup. (again, this was working fine until recently).

P.S. This may be related, although little different issue: I had another issue with another board and code where I was using similar variable to control different phases of operation. At some point, based on serial monitor, variable values stopped making any sense but the board and connected electronics continued behaving normally. I figured that the serial monitor was reading the variable values incorrectly but the board (the the compiled code in it) could see the correct values. so, I left it alone. So, is it possible there is some problem with a compiler or serial monitor or software on my PC that may be causing problems?

Getting an error trying to go to Arduino Create (whatever that is?) via your link. Rather post the code.

Here is the code; thank you.

P.S. the code was too long to be posted directly.

code.txt (16.3 KB)

excuse the code, it is very messy

No. I can't do that. It takes virtually NO effort to bang a carriage return after each ; so you have ONE statement per line.

Similarly, hit the enter key after every {.

It takes virtually NO effort to use Tool + Auto Format, so your code doesn't look like it was typed by a drunken monkey.

There is just no way I can make sense of that code.

Hi Paul,

I reformatted the code per your suggestion using online editor. (Do not have access to Arduino editor right now but hope did not miss anything in the online editor).

Another note regarding the variable as you look for it in the program: in the final version, the functionstatus will be 0 at the beginning and will set to 1 as part of the "void canopyclose()". Right now, for testing purposes, I am simply setting the variable value as 1 to bypass the canopyclose() function.

P.S. I reason for "cramping" the code together was for me to avoid scrolling up and down too much and keeping everything "tight". From your comment, I gather this is not the best practice for coding.

code.txt (19.2 KB)

From your comment, I gather this is not the best practice for coding.

No, it is not. Using functions and separate files is.

      lastDebounceTime = millis(); canopy();

Missed (at least) one...

  if (pwdprompt==true) {

This will evaluate to true if pwdprompt contains true. It will evaluate to false if pwnprompt contains any other value. So, look like a real programmer:

  if (pwdprompt)
  {

Right now, for testing purposes, I am simply setting the variable value as 1 to bypass the canopyclose() function.

So, what does functionstatus = 2 mean?

  Serial.println(functionstatus);

I hate code like this. 3 shows up in the Serial Monitor app. What the heck does it mean?

  Serial.print("functionstatus: ");
  Serial.println(functionstatus);

takes very little more time to type, but conveys orders of magnitude more information.

    byte* states = &registerState[i];
    if (i == reg){
      bitWrite(*states, actualPin, state);
    }
    shiftOut(dataPin, clockPin, MSBFIRST, *states);

Creating a pointer to point to an array element uses no less storage than making a copy of the element. Setting the array element to the modified variable would be clearer than all the pointer dereferencing stuff.

When you do stuff like this, comments are useful. Explain WHY the code is doing what it is doing.

By the way, if you can't find where you are stepping on the value in functionstatus, and I certainly don't see anything obvious, it is most likely because you are writing beyond the end of an array. So, carefully check all array uses, to make sure that YOU do bounds checking, since the compiler does not generate code to do so, by default. The resulting code is much slower is the primary reason it doesn't.

Paul,
the array code is not mine, they are from LED library examples; I just modified slightly and, frankly, do not even now how it works.

Having said that, can you please explain little further (or point me to a reference) about the array boundaries? If the functionstatus variable is not part of any functions that use arrays, can it still be affected?

thanks.

If the functionstatus variable is not part of any functions that use arrays, can it still be affected?

Yes. If you write one element beyond the end of an array, and the variable that gets trashed is the functionstatus variable, then it doesn't matter which function wrote beyond the end of the array.

Having said that, there is only one array in your code, and that is dynamically allocated. I really don't understand doing that when the size of the array is known at compile time.

The function that writes to that array is called a lot, with unpredictable input.

      prevI=++prevI;

The effect of this statement is undefined.

You can use

    ++prevI;

or

    prevI = prevI + 1;

You can NOT use

      prevI=++prevI;

The same horrid mistake is made with --prevI.

OK. thank you. will try to comment out the array code and see if it fixes the variable problem.

Hi John,

Thanks for the help again. Just wanted to follow up:

I ran the code again and the variable worked fine (same code) - not sure what changed.

Still, i did change how array was running per your recommendation.