When setting the GPIO pins output levels must one only use the words HIGH or LOW or could one use a String variable set to a String "HIGH" or "LOW". ??
Why?
Using the strings will not work, and it won't compile if you try.
You can use the numeric values of high and low though - LOW is 0, HIGH is 1 ) (they're just #defines )
digitalWrite(pin,HIGH);
is the same as
digitalWrite(pin,1);
And you can certainly use a variable
const byte High = HIGH;
const byte low = LOW
You can use an enum, too.
As a wild guess, the OP wants to control the state of pins via a Serial interface.
XY thing, if he would say that we could create a macro to do it with the ternary operator:
#define SET_GPIO_WITH_STRING(x) ((strcmp(x, "HIGH") ? (LOW) : (HIGH))
The reason LOW is first is that strcmp() returns 0 for an equal string.
Yeah, but that's a bit difficult to ask if one has never heard of a macro is or a ternary operator.
After 25 years programming, don't ask me in in interview what a ternary operator is. But I know the construction '?:'
I was just saying if he would ask what he wants to actually do we could come up with a more constructive answer.
The reason LOW is first is that strcmp() returns 0 for an equal string.
So, why not do the more understandable
#define SET_GPIO_WITH_STRING(x) ((strcmp(x, "HIGH") == 0 ? (HIGH) : (LOW))
Just to make it one comparison instead of two.
KeithRB:
XY thing, if he would say that we could create a macro to do it with the ternary operator:#define SET_GPIO_WITH_STRING(x) ((strcmp(x, "HIGH") ? (LOW) : (HIGH))The reason LOW is first is that strcmp() returns 0 for an equal string.
t
Thank you KeithRB. I've been using that since 1999, but never knew what it was called!
Don't people read K&R anymore? The "official" name in the grammar is "conditional expression"
When K&R introduce it they say: "The conditional expression written with the ternary operator '?:'..."
Ternary operator is just a general term for a 3 operand operator (as opposed to a binary operator which takes two operands) - since there is only one ternary operator in C....
The conditional operator is the only ternary operator most programming languages that have it. Since conditional is used for lots of closely related things in programming, while ternary is not used as often, I think that may have driven the popularity of calling that the "ternary operator" instead of the "conditional operator".
My main exposure to the ternary operator before I started using the arduino was with structured text on distributed control systems. Looking back I have seen it referred to as a conditional operator. The DCS literature recommends using it in sequential function charts instead of if statements (which also work).
My C class (based on K&R first edition) was back in the mid 80's. I don't use C on a daily basis, so I tend to google anything unusual. But some of the rust is flaking off of my C skills.
Seems reasonable to only check the first character...
#define SET_GPIO_WITH_STRING(x) ((x[0] == 'H') || (x[0] == 'h') ? (HIGH) : (LOW))
Which makes adding more options trivial...
#define SET_GPIO_WITH_STRING(x) (((x[0] == 'H') || (x[0] == 'h') || (x[0] == '+') || (x[0] == '1')) ? (HIGH) : (LOW))
MorganS:
Why?
+1. Why?
That's obviously a difficult question and the OP is still writing a response.