Pages: 1 2 3 [4] 5 6   Go Down
Author Topic: Understanding this line " ? HIGH : LOW; "  (Read 6956 times)
0 Members and 1 Guest are viewing this topic.
Ohio - USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

      Zoandar
  ---v/\/\/\v---

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have another unexplained use of something which I would like to understand. In the code below, what does the following line using a single "&" do?

Bit-wise-and.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

I feel for the developers on this one. Since they have basically provided an IDE that uses C++ using a very-widely used compiler (gcc), they could have just said "go look up how C++ works". And there are many, many good tutorials on it. And a lot of documentation. But of course this "go find out for yourself" attitude is not beginner-friendly. So they try to summarize C (and C++) on their site. But sooner or later they will miss something out (like the "?" operator).

Now they can add that in, but then someone will ask how constructors/destructors work, or some other obscure language feature (like pragmas). And whilst I like the idea of keeping everything central, sooner or later you have to go find out a bit more about C yourself.

And then there's the question about "is C friendly for beginnners?". Well, possibly not. But the easier you make it for beginners the harder you make it for people who pass beginner stage and want to actually do something useful.

Let me give you an example ... a year or so ago my son was working on a PICAXE chip (programmed in their version of Basic). And I suppose you can say that Basic is easier to learn than C. But once you get past making a few beeps and flashing a few lights, you start hitting limitations. Like, the Basic itself has an overhead that takes up valuable memory space, so your maximum program size is more limited than using C.

So anyway, we were making a program that played a small sentence in Morse code. So we wrote a subroutine to do a dot, and another to do a dash, to try to keep memory usage down. But then we hit some bizarre limitation, that you could only use something like 16 GOSUBs in a program. Not nested, just a total of 16, anywhere! It made me wonder how they implemented it ... some sort of jump table rather than genuine subroutine calls.

So my point is, you are trading off making it easy for people on their first day (of which they will only have one) against making it easy for them in subsequent days, of which there will be many.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think Picaxe is a good platform in many ways, but if you hang around the forum for long you'll notice that half the threads are about getting around the platform's limitations.

For example most threads that ask "How do I chew gum and walk at the same time, all at 500Hz" usually get a response like "Use one Picaxe to chew gum and another to walk".

To be fair though it is I think appropriate for its intended audience, ie beginners and school kids, although I have seen some pretty reasonable projects using them.

BTW that's interesting about the gosub limit, I always assumed it was a stack depth issue and as few programs get 16 deep in nested functions I didn't see it as a serious limitation.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Ohio - USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have another unexplained use of something which I would like to understand. In the code below, what does the following line using a single "&" do?

Bit-wise-and.


Thanks. I Googled "bit-wise and" and read part of a wiki about it. Seems rather complex considering it is working with individual bits. I am not sure why they chose to implement such a seemingly advanced command in a book for beginners. Surely there must have been an easier-to-understand way to accomplish the same task.
Logged

      Zoandar
  ---v/\/\/\v---

Ohio - USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The PICAXE story is interesting, and I think I get where you were going with it. But for my own personal belief, there is never a good reason not to document something. Everything should have some kind of user manual. And with so many things having website-based user guides these days (Arduino is no exception) I feel it is inexcusable that when a new command is added, but not in the 'official' reference list, someone (whoever created it?) should be updating the list.

Although the first book I went through had some shortcomings, one thing I have to give the author credit for was that he did teach that using a lot of comments is good form, so you can see what was going on when you might come back to the sketch a year from now. In this second book, there are virtually NO comments in his code! He (mostly) explains what he is doing in the text, but the code listings have no comments whatsoever.

The most recent project has 75 lines of code. I got it to work, and am now going through it in Arduino and adding a comment for nearly every line, describing what it does. This is part of my learning process. However, there are some lines I do not understand, which of course the author did not explain, and the words he used are not in the Arduino Reference. The Help menu's "find in reference" command is becoming a joke. Pretty much everything I highlight and select "find in reference" I get "no reference found".

I'll have to post this project (likely in a new thread) and ask for help to understand those lines which I can't explain. The most frustrating thing about working through these books written for 'beginners' is that the author will suddenly take off on a tangent into some more advanced programming segment and not explain what he is doing. If the commands he used were easy to look up, it would not be as much of an issue. But they are not listed. And there doesn't seem to be any other way to figure these unlisted things out than to come here and ask about it. Were it not for this forum, I imagine a lot of Arduinos would be collecting dust, mine included.

Logged

      Zoandar
  ---v/\/\/\v---

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not sure why they chose to implement such a seemingly advanced command in a book for beginners.

I agree.  I think the only time I've needed a bit-wise-and is when manipulating hardware registers.  There are certainly other examples: driving an LED matrix; displaying a graphic; unpacking boolean values.  But I can't think of any beginner examples.

Quote
Surely there must have been an easier-to-understand way to accomplish the same task.

The code appears to be contrived for the purpose of illustrating the bit-wise-and operator.  I can't think of anyone on the forum asking for help with their "binary dice" application.  I suggest you try to find a book / tutorial that's a bit more "real world".  Have you looked over Lady Ada's tutorials?  They usually lean a bit on the hardware side but they are certainly "real".
Logged

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

GOSUB
Now thats a word I've not heard in a long long time!
Logged


Cumming, GA
Offline Offline
Edison Member
*
Karma: 20
Posts: 1643
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The main problem you run into with typing in examples directly from a "C" book are primarily due to what the Arduino developers did to remove some of the "annoying bits" of C that would make a novice's eyes glaze over.

So... I can see that you do get messed up since the developers of Wiring set the stage for Arduino (and the loosely coupled Processing) and  as such it created nice things like a wrapper for the C  "main()" function... which any good book on C will tell you that you must have,  and you know only see this as "loop()" on Arduino (and Wiring as well).

I do agree that knowing how to find good information about how Arduino works is a bit of a hurdle to overcome when just starting out.

Logged

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

I am not sure why they chose to implement such a seemingly advanced command in a book for beginners.

I agree.  I think the only time I've needed a bit-wise-and is when manipulating hardware registers.  There are certainly other examples: driving an LED matrix; displaying a graphic; unpacking boolean values.  But I can't think of any beginner examples.

Flag bitfields come up constantly: if (alarms & ZONE3_FIRE) or open(fd, O_WRONLY | O_EXCL), etc. etc. and unpacking structures, when some helpful protocol designer has decided to save bits (or registers) by putting the value you need in the third-through-fifth bits of the second byte of whatever. I certainly wouldn't recommend ignoring them.

In the real world, there are only two possible outcomes for a system that intends to "make it easy for non-programmers to program". Either the system is simple, in which case the users are at the mercy of the developers who decide what they can and can't do with it, or the system grows complex to the point where anything is possible in it -- in which case learning to use it becomes exactly as complex as learning to program the system it "replaces". Programming isn't a collection of mystic rituals for scaring outsiders; it's just the art of telling the computer what you want done in a way that will actually get it done. You can't remove what Fred Brooks would call "fundamental complexity" without removing the ability to get things done.
Logged

Ohio - USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This discussion is interesting, but it is getting off the topic I had intended when I started this thread. Getting help interpreting the code I am seeing. Whether or not this "beginner" book is deemed, through opinion, to be a 'good' beginner book or not, it is the one I am using. I really don't want to head off in pursuit of 'the ideal book' or I will never get anywhere with this hobby, because someone somewhere will always think the one I found is good and someone else will want to shoot it down. smiley The only underlying theme I am seeing in this discussion is that apparently no one has written a complete reference manual for the code the Arduino developers allow to be used.

To that end, I have figured out the current project code, all but 4 lines. The project uses the Bounce.h debouncing library for its buttons. But there are commands used for which, once again, I cannot find any reference material. I tried looking for references in C and C++ and Google came up at a loss. I can find other Arduino code posts USING these commands, but the programmer did not use any comment lines to explain them.

The commands I need to understand are the "button.update" and "button.read" used in this function (subroutine) of the larger BinaryDiceGame Sketch. I am guessing button.read means read the state of the button. What is "button.update"?

Code:
void handle_guess_button() {                              //declare the handle_guess_button subroutine function
  if (guess_button.update()) {                     //??????? has to do with updating the state of the guess button?? Not covered in Reference.
    if (guess_button.read() == HIGH) {                    //if the guess button is pressed, then set it HIGH
      guess = (guess % 6) + 1;                            //take current value of "guess", divide it by 6, and only keep the remainder using the % modulo function
                                                          //then add 1 to the remainder.
      output_result(guess);                               //"output_result" (a function declared below) is called to display the value of "guess"
      Serial.print("Guess: ");                            //Print the word "Guess: " to the Serial Monitor
      Serial.println(guess);                              //print the current value of "guess" to the serial terminal, then do a CR.
    }
  }
}

Logged

      Zoandar
  ---v/\/\/\v---

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

Read it and find out. You have the book with the code in it, so you know more than we do. By the looks of it, it seems like it's meant to return true when the button state has changed, but without seeing the definition, I can only guess. You have a book with the definition in it, and probably a discussion in plain english of what it does, so you unlike me, you shouldn't need to guess.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The commands I need to understand are the "button.update" and "button.read" used in this function (subroutine) of the larger BinaryDiceGame Sketch. I am guessing button.read means read the state of the button. What is "button.update"?

OK, I got a bit off-track, sorry. smiley

Let's get the terminology straight first. They are functions, not commands. And the code is trying to call them. Let me explain ...

This is a function:

Code:
int foo ()
  {
  return 42;
  }

If you "call" it, it returns a value (42 in this case). This is how you might call it:

Code:
int a;
a = foo ();

Now after calling it, the variable "a" will contain 42.

Functions can take "arguments", for example:

Code:
int bar (int x)
  {
  return x + 42;
  }

Now you call it by passing a parameter:

Code:
int a;
a = bar (3);

Now after calling it, the variable "a" will contain 45.

I know that button.update is a function (without reading any more code) because it is followed by parentheses. Like in the example above, when we called "foo".

Just to explain, this is not a function call:

Code:
int a;
a = foo;

No brackets after foo, so it is just assigning the contents of foo into a.

This is not a function call:

Code:
int a;
a = bar + (3);

This is because there is an "operator" between bar and (3). So we are adding 3 to bar, and assigning the result to a. The brackets are just for precedence, like this:

Code:
int a;
a = bar + 4 * (2 + 3);

In this case the brackets just say "add before multiply".

Back to your example. Both guess_button.update() and guess_button.read() are function calls, because there are brackets, and nothing between the function name and the brackets. So you have to search the code (or maybe the library) to see what the functions do. Clearly they are not taking arguments (like my "bar" function did) because there is nothing inside the brackets. But the brackets have to be there, or they aren't function calls.

The next issue is the dot. Really you have two things here: "guess_button" and "update". I am guessing that "guess_button" is an instance of a class of some sort, and "update" is a member function. So really, somewhere "inside" the class of which guess_button is an instance, is a function called update.

The general idea of classes is they describe a general thing. For example, you, Zoandar are a person. So, "person" is a class of things, and Zoandar is an instance (one of) that class.

Moving on from guess_button.update() to the "if" part. An "if" statement executes the true branch if the "thing inside the brackets" is non-zero. Like this:

Code:
if ( foo() )
  {
  // do if true
  }
else
  {
  // do if false
  }


So based on my earlier example of the function foo, what is happening is that it calls foo first, and gets back a number (in this case 42). If the number is not zero the "true" part is executed, otherwise the "false" part (if any).

In this example:

Code:
if (guess_button.read() == HIGH)

This is calling the function guess_button.read(). Presumably that returns HIGH or not. If it returns HIGH then the compare will result in a true value (that is, non-zero) and the "true" branch of the "if" will be taken, otherwise not.
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, this thread should be renamed, "Help me understand this crazy 'C' thing!?"  smiley-mr-green
Logged


Ohio - USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, Nick!! You are, and continue to be, the shining light at the end of my tunnel. smiley

One of my favorite sayings is "we can only know what we know". Up until I read:

Code:
So you have to search the code (or maybe the library) to see what the functions do.

in your post, I had no idea that it was even POSSIBLE to "search a library". This project is my first use of libraries, and the book has not very well explained exactly what they are for. smiley All I knew so far from what was that  I needed this library to use its debounce code. That said, when I read "search the library" after your clarifying what is and is not a function I realized the reason I could find NOTHING about this function anywhere in the book, index, etc. was because it must reside IN THE LIBRARY. Then, only because I have been brave enough in the past to gain experience using Windows "Open With" option to try to peek inside files which are not actually text, I was able to open the bounch.h file in the library with Wordpad, and actually look at what is in there. Lo and behold, it contains both "update" and "read" functions.

Mystery solved.

Now that I know what is really happening I went back and read that part of the book again. I do see there was mention in the discussion of the Bounce library of "call its update() method" (METHOD? What is a Method?? After lots of page flipping looking for the word "method" and how he applied it, I finally realized his is using it interchangeably with the word "function" ). "Function" appears in the index. "Method" does not.

I guess when I read it, since it didn't make sense (yet) I went on ahead with reading the remainder of project hoping I would eventually be told what he was talking about. Once it came down to the project working and my putting in all the comment lines I did (over a period of time) I had forgotten about that mention of "update() method" in that paragraph. It would have helped if he had elaborated a little more on it when he mentioned it.

Anyway, the really important lesson learned today is that main programs can call functions within libraries! smiley So when I can't find any reference to a function elsewhere as I work through the rest of the book, if the sketch is using a library file I can look there for it. The Arduino IDE appears not to have any way to look inside a library?

Thanks!!

Logged

      Zoandar
  ---v/\/\/\v---

Pages: 1 2 3 [4] 5 6   Go Up
Jump to: