Two newbie questions: status and printing a boolean

  1. When I went to name a variable "status" the IDE flags it as a reserved word, but I can't find any reference to such a reserved word. What is "status" used for?

  2. I declared a boolean variable:

boolean state = false;

Then I tried to print it with Serial.println(state); but nothing comes out of the serial monitor. I got around it by just declaring state to be an int, but why should I have to? What am I doing wrong?

I'm not sure about status...will check and get back to you.

Regarding your print statement, by default serial.print() will try to print one byte data as ASCII. Something is actually coming out, but it's ASCII 0 which is probably not what you want. You can force the output to be numeric like this:

Serial.print(state, DEC);

See Serial.print() - Arduino Reference

for more details.

Good luck!

ViennaMike:

  1. When I went to name a variable "status" the IDE flags it as a reserved word, but I can't find any reference to such a reserved word. What is "status" used for?

Could be a library that you have installed. Could be a holdover from Wiring. It is probably a method to a class, so using it as a variable probably doesn't matter. The IDE isn't very sophisticated about highlighting. All keywords are stored in a file called "keywords.txt". Each library comes with its own keywords.txt file.

ViennaMike:
boolean state = false;

Then I tried to print it with Serial.println(state); but nothing comes out of the serial monitor.

The reference page says Serial.print and println can take any variable type as an argument. If you look at the source code, there is no definition for the boolean type.

When I have booleans, I find it kind of useless to just get a "true" or "false" response. To get anything useful, you'd need to do a Serial.print() before printing the value anyway. So instead I do this:

if (state)
  Serial.println("Connected!");
else
  Serial.println("Failed!");

'status' gets highlighted even in this basic code:

void setup(void) {
  int status = 9;
}

void loop(void) {
}

but it compiles with no problems.

I didn't see 'status' in lib/keywords.txt so my guess is you're right, it's a holdover.

It's in the ethernet library, and it looks like it is indeed a method.

Oh I get it now...the IDE reads all the libraries keywords.txt on startup, not just when you #include a library. I changed 'status' to 'stptus' in the Ethernet keywords.txt and restarted the IDE and you're right it wasn't highlighted.

Good to know. Thanks!

Thanks, all. That answers both questions.