I'm trying to debug a sketch that I have mostly copied from another source. It is structured such that the void loop is very short and calls various other functions that are defined elsewhere in the sketch.
I want to print a variable that is defined in one of these functions. However, if I include a "Serial.print(variable)" within the loop, I get the error '"variable was not declared in this scope". On the other hand, if I include the Serial.print call within the function where the variable is created, nothing happens.
OK. Thanks. Attached is the code I am trying to debug. I want to use the serial port to monitor channelValue, channelMinValue, and ChannelMaxValue. If I include Serial.print statements in the loop, it returns the error channelValue (etc.) was not declared in this scope.
If I include the print statements within the function that creates these variables, there is no error, but nothing is printed.
kdeemer:
If I include Serial.print statements in the loop, it returns the error channelValue (etc.) was not declared in this scope.
Only the variables declared outside of functions (like your g_displayMode) are 'global' and therefore visible to all functions. If a variable is declared inside a function it is not visible in any other function. That is why channelValue is "out of scope" (not visible) inside the loop() function.
johnwasser:
Only the variables declared outside of functions (like your g_displayMode) are 'global' and therefore visible to all functions. If a variable is declared inside a function it is not visible in any other function. That is why channelValue is "out of scope" (not visible) inside the loop() function.
Thanks, but then why can't I just insert the print statements inside of the function where the variable is created? When I try this, I don't receive an error, but nothing prints. (and Serial.print works normally in other sketches)
Serial.print("level "),
Serial.println(channelValue),
Serial.print(" min "),
Serial.print(channelMinValue),
Serial.print{" max "),
Serial.println(channelMaxValue);
...if the values don't get populated, make them global. I have no problem declaring a variable inside a function and then Serial.print"ing" it within that function.