Debug variable changes w/o cause between setup and loop sections - why, FIXED!

I'm working on a fairly complex project that will be controlled by an Arduino Uno. A friend suggested that instead of manually commenting and uncommenting all my debug stuff, I put in a physical debug switch. I would then make all my debug statements in the form "If debug == on, print stuff"

I would then read the debug switch at the start of a program load, and handle the debug statements accordingly.

This seemed like a good idea, so I implemented it, by running a toggle switch between D6 and ground, and using the internal pullup resistor, and modifying the code to match.

I found that for some reason, my debug tests worked in the setup section, but not in the loop section. After thorough error checking, I started experimenting and found the problem seemed to be that the value of my debug variable (originally called "debug" but later changed to "bugHunter" in case I had a problem w/ an undocumented reserved name - didn't help) was changing between the setup and loop sections of the code.

I have created the following minimal sketch to demonstrate the behaviour:

  // DEBUG SWITCH VARIABLE - uses D6 through toggle switch to Ground
    int debugPin = 6;
    int bugHunter = 1;

    void setup() {
        // DEBUG SWITCH SETUP - NOTE switch ON = 0
        pinMode(debugPin, INPUT_PULLUP);
        int bugHunter = digitalRead(debugPin);
        
       Serial.begin(9600);
        
        // Lots of stuff trimmed out
        
        if (bugHunter == 0) {
                Serial.println("In setup, debug is ON ");
        }
       
        
        Serial.print("setup debug=");
        Serial.println(bugHunter);        

    } // SETUP END

    void loop() {

        Serial.print("beginning of loop bugHunter=");
        Serial.println(bugHunter);   

        if (bugHunter == 0) {
                Serial.println("In loop, debug is ON ");
        }
        if (bugHunter== 1)   {
                bugHunter = 0;
        }
   
   // Lots of other stuff trimmed out     

        Serial.print("end of loop bugHunter=");
        Serial.println(bugHunter);        
        
        
    } // END LOOP Section

This was my final iteration of trying to narrow down just what the problem is. With the debug switch turned ON, I get the following serial monitor output (bold and comments added):

In setup, debug is ON
setup debug=0
// setup -> loop transition - note change in value!
beginning of loop bugHunter=1
// since bugHunter =1, in loop statement not printed
// code resets bugHunter to zero here
end of loop bugHunter=0
// value does not change during loop recycle however
beginning of loop bugHunter=0
In loop, debug is ON
end of loop bugHunter=0
beginning of loop bugHunter=0
In loop, debug is ON
end of loop bugHunter=0
<etc....>

As emphasized, the value of bugHunter starts as 0, then gets changed to 1 at the transition between the setup and loop sections. However it remains unchanged on subsequent iterations of the loop, so the ONLY problem is the setup -> loop transition...

If the debug switch is OFF, bugHunter starts out as a 1 and stays that way until reset, which is the proper behaviour.

Thus I seem to have narrowed the problem down to a value of zero being changed to a 1 during the setup to loop transition.

Does anyone have any idea about what is happening, and how I should fix it?

Thanks,

ex-Gooserider

int bugHunter

and

    void setup() {
        int bugHunter = digitalRead(debugPin);

both are different variables, same name... use in functions without 'int' prefix, as that declares a new variable

Your issue is in your Setup function:

int bugHunter = digitalRead(debugPin);

You create the variable as a global variable outside of setup then you recreate it inside setup. This causes it to be local to the setup function and not carry over to the loop() function. So just remove the 'int':

bugHunter = digitalRead(debugPin);

That will work.
Mushfiq

MANY THANKS!

The second "int" was the problem, fixing it made both my test sketch and my main program work properly.... :slight_smile:

As a side note, written as a person who is totally new to Arduino programming, and has had very minimal programming experience otherwise; it seems to me that there isn't a lot of documentation on any of the stuff that goes above the "void setup {" line. The "bare minimum" example doesn't mention the concept, and I haven't really seen much elsewhere written about it, compared to the setup and loop sections. Of course, I may not have been looking in the right place....

ex-Gooserider

it seems to me that there isn't a lot of documentation on any of the stuff that goes above the "void setup {" line.

It's a big subject, with an obscure name - scope.

Unfortunately, quite a few of the examples are written in a way that encourages newbies to give all variables global scope when it isn't necessary.

AWOL:
Unfortunately, quite a few of the examples are written in a way that encourages newbies to give all variables global scope when it isn't necessary.

I feel like that stems from the setup/loop business. There are plenty of variables that really should be in the scope of the main function, but with no main they have to be global. Actually I don't know about that statement; can everything needed happen with static variables?

can everything needed happen with static variables?

For the most part, yes. That involves passing more arguments to functions, including references and pointers. The IDE doesn't handle references well (as in it doesn't create function prototypes properly for functions that take reference arguments). So, most examples use global variables to avoid a lot is issues, scope being the biggest one.

When setup() and loop() both need to access variables, like pin numbers, static variables are not appropriate.