Variable Question.

Everything I have seen so far shows that variables can only be set as numbers. Lets say I wanted an input to show proof of run for a fan. How would I set up a variable to show on or off? Any help is appreciated!

bool fanIsRunning = true;

or false

Thanks for the info. Put that in the program and I'm getting close. When I Serial.print(fanIsRunning), it prints a 0 or 1 depending on the status. Just curious, but is there a way to have it print true/false other then Serial.print("fanIsRunning: True");

Try this:

Serial.println(fanIsRunning ? "true" : "false") ;

Thanks, it works great. Trying to understand the ? operator though......

It is just a condensed form if if-then-else

It's the same as

if (fanIsRunning  == true)
{
Serial.println("true");
}
else
{
Serial.println("false");
}

Makes perfect sense now, Thanks! And Thanks Keith for that Link!

You are welcome. As your case shows sometimes it is nice to have an if/then/else that returns a value. After all, if you compare your code and Shpaget's "same-as" note that he has two calls to Serial.println(), which takes up extra space.

That being said, it is possible that gcc is smart enough to create the exact same code in both situations.

mtooey:
Thanks for the info. Put that in the program and I'm getting close. When I Serial.print(fanIsRunning), it prints a 0 or 1 depending on the status. Just curious, but is there a way to have it print true/false other then Serial.print("fanIsRunning: True");

So, your question is not about what the value is, but about how it appears when you print it.

What you are perhaps after is the C ternary operator - it's a compact way to put an if statement in an expression.

Serial.print(fanIsRunning ? "true" : "false");

I think there might be a way to override the print method that Serial calls when it is passed a boolean type. There is one already, the one that prints "0" or "1". But I don't know how to do it. It's been a few years, and I'm still catching up.

It might be the most elegant way to do it.

Then you could just use Serial.print(fanIsRunning);

Or perhaps better, create a fan object. Then you can say:

 fan.print();

to tailor the output to the object. For example, fan.print() might print "running" or "not running", while light.print() might print "on" or "off".

Serial.print("The fan is ");
fan.print();
Serial.print( "and the light is");
light.print();
Serial.println(".");

"The fan is not running and the light is off."

Lot of great info here everyone. Really appreciate the feedback!

aarg:
I think there might be a way to override the print method that Serial calls when it is passed a boolean type. There is one already, the one that prints "0" or "1". But I don't know how to do it.

What you're talking about is called function overloading, where several functions can have the same name, and the compiler automatically chooses the correct one based on the types of the arguments.

And that's why your idea won't work. The type boolean is a typedef for uint8_t, a generic unsigned byte. The compiler won't be able to tell the difference between your version intended to handle boolean and other version(s) that can directly or indirectly handle unsigned 8 bit integers. While C++ offers many different types, the variations on integers are not strongly typed enough to be able to handle this properly.

It would work if you defined your own boolean type as a class with the appropriate properties and operators, but that would break a lot of existing code that expects boolean to be a simple unsigned byte, as well as require you to add another Serial.print() variant that is specific to that class.

Or perhaps better, create a fan object. Then you can say:

 fan.print();

This is a much better idea. Encapsulate everything that is specific to fan in a fan object. It makes much more sense for the fan object to know how to properly print its status, rather than try and make Serial.print() be able to handle a lot of esoteric data types.