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