Go Down

Topic: Two newbie questions: status and printing a boolean (Read 732 times) previous topic - next topic

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?

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? 

magagna

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 http://www.arduino.cc/en/Serial/Print

for more details.

Good luck!
http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

James C4S

#2
Nov 10, 2011, 06:25 am Last Edit: Nov 10, 2011, 07:45 am by James C4S Reason: 1

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.


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:

Code: [Select]

if (state)
 Serial.println("Connected!");
else
 Serial.println("Failed!");
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

magagna

'status' gets highlighted even in this basic code:

Code: [Select]
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.
http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

WizenedEE

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

magagna

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!


http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

ViennaMike

Thanks, all.  That answers both questions. 

Go Up