Help with Serial.print

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.

How can I get around this?

Thanks in advance.

Read this before posting a programming question ...

Pay particular attention to the sixth item under 6. Getting help on the forum.

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.

Thanks for any help

lights_take5.ino (13.2 KB)

    Serial.print("level "),
    Serial.println(channelValue),
    Serial.print("  min  "),
    Serial.print(channelMinValue),
    Serial.print{"  max  "),
    Serial.println(channelMaxValue);

Statements should end with semicolons, not commas.

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)

What does "nothing prints" mean?

...are you getting ?

level
min
max

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.

I dunno.

Solved it. I had the print calls after the 'return' call, so the program would never get to them.

Thanks for your help

@kdeemer, thank you for the follow-up.