Pages: [1]   Go Down
Author Topic: What does this mean?  (Read 381 times)
0 Members and 1 Guest are viewing this topic.
India
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Induino X is awesome!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

digitalWrite(13, on ? HIGH : LOW);

It was in the IRrelay example.What does the ? do and the : with both LOW & HIGH.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://en.wikipedia.org/wiki/%3F:#C.2B.2B

It's the functional equivelent to this:

Code:
if (on)
  digitalWrite(13, HIGH);
else
  digitalWrite(13, LOW);
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

on is probably a boolean (either true or false) or an int either 1 or 0, so it is equivalent to:
Code:
digitalWrite(13, on);
« Last Edit: January 13, 2013, 11:11:05 am by guix » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11159
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

on is probably a boolean (either true or false) or an int either 1 or 0, so it is equivalent to:
Code:
digitalWrite(13, on);
That code makes the assumption that HIGH and LOW are the same values as true and false. It just happens to be so in this case, but as a general rule it's best to avoid making that sort of assumption - the original implementation is preferable IMO.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That code makes the assumption that HIGH and LOW are the same values as true and false.
I don't think it does.
Last time I looked at the source of digitalWrite, it very specifically incorporated the C convention of zero / non-zero as meaning LOW / HIGH.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does the ? do and the : with both LOW & HIGH.

If variable "on" has any value other than 0, set the pin. Otherwise (when "on" is 0), clear the pin.
Logged

Pages: [1]   Go Up
Jump to: