I need a little help please in understanding basic concepts. I have been looking at Maik Schmidt's book (Arduino a quick start guide) and already just a few sketches in I am lost. He uses the term != without explanation ( I eventually found that to mean not equal) he then uses the following line apparently to toggle a command (but again without any explanation);
led_state = (led_state ==LOW) ? HIGH : LOW; could somebody please tell me what this means. Trouble is it is virtually impossible to search for the ? symbol in books or on google. Thank heavens for excellent forums like this ! (and the last character doesn't mean NOT in this case )
That is 'C' language code.
x = 1; // variable 'x' becomes number 1
if( x == 25) // test if 'x' is equal to 25
if( x != 100) // test if 'x' is not 100
The next examples do the same:
// This is 'normal' C code
if (x == 100)
y = 1;
else
y = 0;
// This is the same
y = ( x == 100) ? 1 : 0;
That single line of code is a conditional check and assignment together in a single line.
It is the same in Java, The ? : operator in Java
It means:
If led_state is LOW (the one in brackets), then HIGH is assigned to led_state (the one on the left), otherwise LOW is assigned to led_state (again the left one).
This is a short form of an if statement that is very useful in variable assignments.
It is the same as
if led_state == LOW
led_state = HIGH
else
led_state = LOW
The part before the ? Is the Boolean expression and the : divides the true and false parts.
The things you're having trouble with are C issues, not Arduino related. It sounds like the book you're using has an expectation that you have some familiarity with C already. You'll likely ease your pain with a C or C++ tutorial or two.
Thanks folks, perhaps I should get a C primer. Having said that the books on Arduino I have been studying state that no previous knowledge is required !
Thanks for the help
Having said that the books on Arduino I have been studying state that no previous knowledge is required !
No previous knowledge of what? A blanket statement like that is silly. Of course you need some prior knowledge. You need to at least know how to read. The statement may refer to no previous knowledge of programming, and that is probably true, because the program is provided.
No previous knowledge of electronics is probably true, too, because the schematics and parts lists are provided.
Certainly some knowledge of one or the other is needed to get you interested in the Arduino in the first place.
The Reference page includes descriptions of the basic operators, ==, !=, etc. The ternary operator (the only one that C has) is not defined there, and has no place in a beginner book, in my opinion. It is a shortcut that isn't really needed with the compilers that we have today. Compilers 40 years ago did not have the optimization features that today's compilers have. Spending a few more seconds pounding keys one time, to get readable code, is better than using obscure functions to generate code that is no better than what the optimization process will produce. In my opinion.
DavidFMarks:
Thanks folks, perhaps I should get a C primer. Having said that the books on Arduino I have been studying state that no previous knowledge is required !
Thanks for the help
I hate to say this, but your assertion is untrue. From Section "Who should read this book":
If you’ve never written a piece of software before, start with a programming course or read a beginner’s book about programming first [...]