Understanding this line " ? HIGH : LOW; "

So I would advise that a "beginner" start with a basic C book or guide first and not C++ and then see how far that gets them. Save delving into the C++ "objects and classes" environment for when the basics of C are better understood.

Total agreement here!

led_state = (led_state == LOW) ? HIGH : LOW;

It checks a conditional and then assigns the output to led_state.

Take everything to the right of the first =

(condition to check) ? return this if equates to true : return this if equates to false;

Thus, if led_state is equal to LOW, then set it to HIGH. If it's not equal to LOW, set it to LOW.

Remember the words in all capitals LOW,HIGH are constants.

Similarly, I could do something like:

result = ( amount >= 10 ) ? TRUE : FALSE;

which is the same as:

if (amount >= 10) {
result = TRUE;
}
else {
result = FALSE;
}

Simpson_Jr:

Zoandar:
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). :slight_smile:
Thanks.

I wouldn't like to keep you from your learning experience, I'm more or less in the same boat, but... http://www.mcselec.com/ has an easy basic-compiler for most 8-bit AVR chips. It's been a while since it was last updated, but there's still quite a lot of support for it. Some of the projects made with it are simply amazing.

I haven't hooked it up to the test-bench to compare with C/C++, it may... consume a little more memory or run a little slower. I haven't noticed big differences though...

One thing I really like about it is the amount of chips supported. From Attinys with 6 pins and just 1 KB
to 256KB atmegas. Arduino may also be able to support those chips, but loads.... aren't yet.

Fastavr and GreatCowbasic also use Basic, but haven't tested those.

Thanks, but I was sort of joking when I wrote that. I'll keep these in mind for later though. Might come in handy!

gorbs:
led_state = (led_state == LOW) ? HIGH : LOW;

It checks a conditional and then assigns the output to led_state.

Take everything to the right of the first =

(condition to check) ? return this if equates to true : return this if equates to false;

Thus, if led_state is equal to LOW, then set it to HIGH. If it's not equal to LOW, set it to LOW.

Remember the words in all capitals LOW,HIGH are constants.

Similarly, I could do something like:

result = ( amount >= 10 ) ? TRUE : FALSE;

which is the same as:

if (amount >= 10) {
result = TRUE;
}
else {
result = FALSE;
}

So it works much like the old MS Basic's IF..THEN...ELSE command. I used to use that a lot. Thanks for the info!

pwillard:
So I would advise that a "beginner" start with a basic C book or guide first and not C++ and then see how far that gets them. Save delving into the C++ "objects and classes" environment for when the basics of C are better understood.

Arduino examples that obfuscate should also be verboten and redacted.

So if I were to read a basic C tutorial it would not immediately launch me into code that the Arduino can't use?

Well, still not, I am afraid. I went here:

http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Form%20of%20a%20C%20program

and looked at the beginning of the tutorial on the structure of a C program. Although it is similar, the Arduino IDE doesn't like their lack of things like using the "void" command before using a function (subroutine) name, etc. From a grass-roots point of view, still confusing when trying to learn how to write code for Arduino. Had I not learned today about not needing the "main" function from you good folks, I would have been stumped right there, because it is not listed in the Arduino Reference (since it isn't used) yet it is the first thing the C tutorial says is needed.

Is there anywhere someone has put together a site that lists every usable programming command for Arduino, along with example code for each command? That would be a very good tool to find. Sadly the Arduino 0022 Reference only comes close.

being new at this..2 days..C or C++ is hard for me after doing Visual Basic or PBasic for basic stamps....I would save snippets of code..I plan to do the same in C....sooner or later , I will grasp it

I am keeping a log of the things I am encountering and learning. My memory isn't that great. :slight_smile:

I just realized as I am typing in the next project in the book I am reading that it seems constants always are UPPERCASE and variables or functions are lowercase. Is that 'standard' for Arduino?

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?

 digitalWrite(LED_BIT0, result & B001);

I know that "&&" means boolean "AND". I see a listing in the reference which starts with "&" and talks (cryptically) about pointers, but I don't think this is a pointer. I think it is doing something with the output value and a binary number (in this case "1") together. I just don't know "what" it is doing:

//BinaryDice/DiceWithButton.pde

const unsigned int LED_BIT0 = 12;
const unsigned int LED_BIT1 = 11;
const unsigned int LED_BIT2 = 10;
const unsigned int BUTTON_PIN = 7;

void setup() {
  pinMode(LED_BIT0, OUTPUT);
  pinMode(LED_BIT1, OUTPUT);
  pinMode(LED_BIT2, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
  randomSeed(analogRead(A0));
}
int current_value = 0;
int old_value = 0;

void loop() {
  current_value = digitalRead(BUTTON_PIN);
  if (current_value != old_value && current_value == HIGH) {
    output_result(random(1, 7));
    delay(50);
  }
}

void output_result(const long result) {
  digitalWrite(LED_BIT0, result & B001);
  digitalWrite(LED_BIT1, result & B010);
  digitalWrite(LED_BIT2, result & B100);
}

Zoandar:
I just realized as I am typing in the next project in the book I am reading that it seems constants always are UPPERCASE and variables or functions are lowercase. Is that 'standard' for Arduino?

Purely personal preference. You are free to make constant names using any legal identifier characters. By convention, constants are uppercase.

Thanks!

Zoandar:
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.

Yankee:
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.

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

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.

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.

Zoandar:
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.

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".

[quote author=Nick Gammon link=topic=54769.msg393100#msg393100 date=1299737345]GOSUB[/quote]Now thats a word I've not heard in a long long time!

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.

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.

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. :slight_smile: 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"?

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.
    }
  }
}