[arduino c]a special problem with (...?...:...);

hello,
I'm sure there is lot of questions on this operator but since I don't know what is allready critisized I decide to make a new topic on it. I don't understand the error message at all.

    Out=(Out == HIGH ? LOW , digitalWrite(13,LOW) : HIGH , digitalWrite(13,HIGH));

and the error critics goes on the type of the data.

second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'

what is the critics here as the type of the data looks to me similar. I searched for this operator in my liturature since some signs could be wrong but I don't find anything.

Can someone help me?

greetings

Manni

(Out == HIGH ? LOW , digitalWrite

The expressions to the right of the ? are supposed to return a value. A void function clearly doesn’t.
Why not a simple “if”?

There is no reason to qualify your title “Arduino C”, this is simply C/C++.

The tertiary operator (? : ) is a convenience for making your code shorter and (usually) harder to understand.

If it's giving you any trouble at all (and it looks like it is), abandon it and write that stuff out longhand in a few more lines.

Edit: Ternary, not tertiary!

wildbill:
The tertiary operator (? :slight_smile: is a convenience for making your code shorter and (usually) harder to understand.

+1

-jim lee

The tertiary operator (? : ) is a convenience for making your code shorter

More correctly the tertiary (sic) operator (? : ) is a convenience for making your source code shorter

Ternary operator and comma operator in the same statement! Oh my!

 Out=(Out == HIGH ? LOW , digitalWrite(13,LOW) : HIGH , digitalWrite(13,HIGH));

The problem is that the value of the comma operator is whatever the rightmost operand is. In this case, that operand is the value returned by digitalWrite(). But digitalWrite returns a void, which can't be assigneds to Out.

What you meant to do is:

 Out=(Out == HIGH ? digitalWrite(13,LOW), LOW : digitalWrite(13,HIGH), HIGH);

There are a number of reasons not to do it that way, but that's what you meant. Another alternative is to take advantage of the fact that the value of the assignment operator is the value assigned:

 Out == HIGH ? digitalWrite(13,Out=LOW) : digitalWrite(13, Out=HIGH);

Or, more compactly:

 digitalWrite(13,Out = (Out==HIGH?LOW:HIGH));

Now, HIGH has a value of 1, LOW of 0. If you compare to LOW, the microprocessor can use a 'jmpz' instruction. If you compare with equality to HIGH, the microprocessor has to subtract 1 and check for zero. So: compare to LOW rather than comparing to HIGH.

 digitalWrite(13,Out = (Out==LOW?HIGH:LOW));

But there is no reason on God's green earth to do this as a one-liner.

 Out = (Out==LOW?HIGH:LOW);
 digitalWrite(13,Out);

So much easier to read and understand.

Also, consider using LED_BUILTIN rather than hardcoding 13.