Go Down

Topic: Understanding this line " ? HIGH : LOW; " (Read 21919 times) previous topic - next topic

Zoandar

There is a project in the Arduino - A Quick-Start Guide on page 58 that I do not understand, and the author did not explain the line in detail before moving on to the next project.
The project is under the BinaryDice.pde section, and this line appears in all the code variations as we move through making the projects controlling button more reliable and then de-bouncing the button. It is the line:

Code: [Select]
led_state = (led_state == LOW) ? HIGH : LOW;

I understand that it has to do with switching the led_state to the opposite state depending upon what is currently detected, but what I don't get is the way the syntax is arranged (how to 'read' what the line does) for those last HIGH and LOW conditions. It would seem to be some sort of "if-then" condition that is not spelled out. Could someone clarify this for me?

It appears in the following code :

Code: [Select]



const unsigned int BUTTON_PIN = 7;
const unsigned int LED_PIN =    13;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
 
}

int old_button_state = LOW;
int led_state = LOW;

void loop() {
  const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
  if (CURRENT_BUTTON_STATE !=old_button_state &&
      CURRENT_BUTTON_STATE == HIGH)
      {
       
    led_state = (led_state == LOW) ? HIGH : LOW;           // <-----------------------------This line confuses me ?? <-------------------------
    digitalWrite(LED_PIN, led_state);
    delay(300);                                                         // this line is added to "debounce" the switch.
  }
  old_button_state = CURRENT_BUTTON_STATE;
}   


maniacbug

#1
Mar 09, 2011, 06:12 am Last Edit: Mar 09, 2011, 06:16 am by maniacbug Reason: 1

There is a project in the Arduino - A Quick-Start Guide on page 58 that I do not understand, and the author did not explain the line in detail before moving on to the next project.
The project is under the BinaryDice.pde section, and this line appears in all the code variations as we move through making the projects controlling button more reliable and then de-bouncing the button. It is the line:

Code: [Select]
led_state = (led_state == LOW) ? HIGH : LOW;


It is equivalent to the following code:

Code: [Select]

if (led_state == LOW)
 led_state = HIGH
else
 led_state = LOW;


The latter is obviously much more readable.  Or if you wanted to be really obtuse, you can write this :D

Code: [Select]

led_state ^= 1;

retrolefty

And it doesn't help matters that the main Arduino reference page ( http://arduino.cc/en/Reference/HomePage ) doesn't show usage examples of:

?
or
^=


marzetti

I have found a number of shortcomings like this one in the Arduino documentation. Really confusing for a newbie and time consuming to search for answers. Is there a mechanism to provide feedback to the developers on such matters?

graynomad

Quote
Really confusing for a newbie

And yet Arduino is supposed to be for beginners.

Quote
Is there a mechanism to provide feedback to the developers on such matters?

Good luck with that  :)

You probably should get a "Beginners guide to C" book or some such.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

u0421793

If it really was supposed to be for beginners, it wouldn't use C.

ben10


If it really was supposed to be for beginners, it wouldn't use C.


Probably C next to assembly has been used for programming micro controllers for a very long time.

jraskell

C/C++ has more available documentation, tutorials, books, and libraries than virtually any other language out there.
It is also one of the most space and performance efficient languages out there, which is of significant value on a platform with very limited memory space and processing power.

The simple fact is, if Arduino had used just about any other language to program in, for one reason or another it wouldn't have become nearly as popular as it has become.

u0421793

ben10, and jraskell, - think about what you both just said: your responses are totally irrelevant to my point.

C really is too difficult for people who aren't doing this stuff for a living. C is completely the wrong language for a beginners toy such as the arduino. Who cares what microcontrollers have been programmed in for a long time? Who cares what quantity of documentation and books etc are out there? Who cares about space efficiency and performance efficiency at all? That's the last thing a beginner is concerned with. The only people that care about your points are proper programmers and engineers that do this sort of thing all day. For beginners, C is the wrong language.

Zoandar

Thanks for the clarifications! It is always refreshing when I find people who obviously know far more than I do about the topic in question, yet they will take the time, sometimes in great detail, to help me figure out something that already comes naturally to them. I have been on the other side of this fence more than once, and on certain other forums a similar question would have gotten me the terse answer "learn C" and ended there. I'm glad you folks are not like that! :)

I am not sufficiently knowledgable to enter the debate on whether C or its variants are the best pick for Arduino, but what I HAVE learned so far is that the Arduino IDE does not support 'all' the code I have seen being used in other C++ references I have encountered when trying to look things up online for myself. So if I did get a book or tutorial on the basics of C++ it would effectively contain 'misinformation' for my purposes. That's why I am avoiding doing it.

In the subset of C++ code that does work on Arduino, does it work exactly the same way as would be used in other uses of C++? If for any reason the folks who created the Arduino IDE modified any of the commands' behavior, then trying to cross reference as a beginner would be a bad choice.

Whatever the case, in order to work with the Arduino platform I have to learn the language provided. (Unless someone wrote a MS Basic to Arduino code converter). :)

Thanks.

PaulS

Quote
So if I did get a book or tutorial on the basics of C++ it would effectively contain 'misinformation' for my purposes.

You need to understand what parts of the code are hardware specific (such as fopen()) and what parts are generic data manipulation/storage/allocation functions.

Most of the generic stuff will work on the Arduino. The hardware specific stuff will not.

There are some generic features that are not supported, mostly dealing with floating point processing, but most stuff is supported. An understanding of the hardware you are dealing with, and the limits that it comes with, will make understanding the language, and what is and is not supported easier.
The art of getting good answers lies in asking good questions.

AWOL

@Ian:
To an extent, I agree with you, but hiding more complexity is just like finding an air-bubble when you're wallpapering - push it down, and it'll just pop up somewhere else, possibly in a different shape.

If you find a platform that is more forgiving of beginners, you'll almost certainly find that it is more complex and therefore more expensive. It will need more memory for a start.
You won't be able to migrate your project quite so simply or cheaply from breadboard to completed PCB.

Certainly, I've yet to come across a more cost-effective way of getting started than the Arduino (and yes, I know I do this stuff for a living) - PICs are even less friendly (they're either bare processors programmed in assembler, or comercial products running performance-sapping interpreters), and the last machine I designed from ground-up was a wire-wrapped 6809 which nearly drove me insane during debug (even with access to an in-circuit emulator).

I'd love a 20 MHz Z80 to tinker with, but it wouldn't be quite so easy to fiddle with the peripherals.

Remember, this is a hobbyist platform - trainer and evaluation boards do exist, but often they and/or their tools cost serious money.

I don't think Wiring is going to migrate to anything less C-like in the foreseeable future.

maniacbug

I say, get the C book.  As noted before the stuff that you need to ignore are calls into the os for things like file management and displays.  Perhaps there is a C for micro controllers book out there.  From what I've seen, I've never run into a limitation of Arduino C++ that was not brought about by the hardware.  And for all that stuff , the Arduino docs are excellent.

jraskell

First, I don't agree that C is too hard for anybody not programming for a living.

Second, there are several microcontroller boards available based on BASIC, and none of them have become nearly as popular as Arduino, and there are several reasons for this.
1. Those boards are far more limited than Arduinos BECAUSE they use BASIC.  It's less efficient, both in terms of memory space and performance.
2. Many experienced programmers will not bother working with BASIC, as such those boards lack the variety of third-party libraries that are available on the Arduino.  Is there any other board out there that has something comparable to what Arduino has in the Playground?  No.

Third, my second point and sub-points should make it perfectly clear that my original post was completely relevant.  A beginners board that is ONLY used by beginners will never become popular for that very reason.  And the current market completely supports that statement.

u0421793

Actually, why isn't it possible to use "Processing", which is what occupied pretty much the entire first half of the arduino book I got, before it dawning on the reader that you don't program the things using processing at all.

Go Up