Pages: [1]   Go Down
Author Topic: Two newbie questions: status and printing a boolean  (Read 649 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? 
Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (state)
  Serial.println("Connected!");
else
  Serial.println("Failed!");
« Last Edit: November 10, 2011, 01:45:11 am by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

'status' gets highlighted even in this basic code:

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.
Logged

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, all.  That answers both questions. 
Logged

Pages: [1]   Go Up
Jump to: