Pages: [1]   Go Down
Author Topic: Protothreading  (Read 277 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking into protothreading: http://playground.arduino.cc/Code/TimedAction#Example

I understand for
Code:
for (First; Last; increment)
do while, and while loops.


I don't understand part of this bracket (from the example in the link above)
Code:
void blink(){
  ledState ? ledState=false : ledState=true;
  digitalWrite(ledPin,ledState);
}

I don't understand the layout of 
Code:
ledState ? ledState=false : ledState=true;
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Translation:

ledState is true?
If yes, set it to false
If no, set it to true

This statement inverts true to false or false to true. I would rather say ledState=!ledState; where the ! means NOT.
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's the ternary operator:

Code:
    boolean_value ? result_if_true : result_if_false;

They are using a round-about way of inverting the boolean value of ledState.  The easy way is:

Code:
   ledState = ! ledState;

They could also have used the operator more properly:

Code:
    ledState = ledState ? false : true;
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

Thank you very much guys!
liudr & johnwasser
I understand now.
Logged

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

I don't understand the layout of 
Code:
ledState ? ledState=false : ledState=true;

Yeah, the person who coded that probably didn't understand it either. It's a misuse of the ternary operator and a very contrived way to invert a boolean.
Logged

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://en.wikipedia.org/wiki/Obfuscated_code

Maybe?
Logged


Pages: [1]   Go Up
Jump to: