Understanding this line " ? HIGH : LOW; "

Zoandar:
In Mazzimo's book, when he wanted to set up an LED on pin 13 he used the command

#define LED 13

But in Maik's Quick Start Guide book, he uses the command

const unsigned int LED_PIN = 13;

As they saying goes - "same difference". :slight_smile:

Indeed, well some years ago this was actively debated among even professional programmers. Among most people I know, it's pretty well settled that the const int construct is preferred, but #define certainly works.

As one of the earlier ranters pointed out, basing Arduino on C++ exposes the beginner to all sorts of these intricacies. (But as also correctly responded, the ecosystem would not be here if it were something else.)

:slight_smile:

I guess that command "const" sort of says it all, doesn't it? I looked back through the code as you were replying. Sorry about that mistake - it is a bad habit I have - referring to 'any' letter or name defined to hold a value as a variable when sometimes it is a constant. I know constants can't later be reassigned a different value in the same program where variables can change many times. I need to learn to use the correct term. :slight_smile: Hopefully it will implant in my mind as I write more code and use both forms. Thanks for setting me straight.

Is there any way to set this forum so it 'auto-saves' what I am typing in a post? I'm doing this on my LG Ally, and I had a scheduled reminder alarm go off just before I was ready to post. It dumped me out of Opera Mobile 10.2 and I lost all this and had to retype it. Or maybe some way to make Opera keep it? The Opera History had Post Reply in this forum, but the text box was empty.

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.

Some of these things being discussed are also on my list of peeves.

The whole "it's C++" thing is a bit misleading. I'm not arguing that Arduino is NOT C++, far from it. I have seen some get quite adamant about all this stuff saying, "It's C++... How dare you say just 'C' because the AVRGCC compiler embedded in Arduino does support things like OBJECT and CLASSES". Yes, arguably cool... but this will, in my opinion, just plain baffle a newcomer that wants to play with LED's.

The important thing to understand, I think, is that C++ supports the entire predecessor ANSI C standard library. All the standard C functions should be available but as a successor, C++ also defines additional library functions and new features that are best saved for more advanced programming.

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