Understanding this line " ? HIGH : LOW; "

I would say that the init handles things that you normally have to worry about on other platforms, but the Arduino does some things behind the scenes.
The init is an important part of this 'behind the scenes' and you should be thankful it's there. You normally do not need to know what it does.

The only thing to know, is that there are some things you can not do before the init has been called, so when the time comes fro writing libraries/classes then you need to refrain from doing certain things in the constructors as they might run before init has.

Thanks. It seems like I ran across it in the first book I read, but I would probably have to search the book to find where I saw it.

It seems like I ran across it in the first book I read

It is very difficult, but you have to separate the language from the environment.
The language permits and constructs such devices as
cout << "Hello World!" << endl;, but the environment actually implements them, and on the Arduino, "cout" is not part of the environment.

If you look at the link I posted earlier, you'll see it is quite possible to write Serial << "Hello World!" << endl;, which may seem the same, though some of the wider implications of "cout" are absent.

Zoandar:
Thanks. It seems like I ran across it in the first book I read, but I would probably have to search the book to find where I saw it.

Actually, I just looked back through that book, and it is not in there anywhere. I think the confusion in my memory was that when I first saw an "int" statement, I mistook the command to mean "initialize" instead of "integer". Sometimes it is hard to drop those early misconceptions as more is learned. :slight_smile:

I guess each person has their preferences in how they like to program, too. In Mazzimo's book, when he wanted to set up an LED on pin 13 he used the command

#define LED 13

and then to light it he used

digitalWrite(13, HIGH)

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

const unsigned int LED_PIN = 13;

This had me scratching my head until I figured out what he was going to do with it, using LED_PIN as a variable name. So to light it he used

digitalWrite(LED_PIN, High);

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

This had me scratching my head until I figured out what he was going to do with it, using LED_PIN as a variable name

No, really most definitely NOT a variable name in either case.

If you have any doubts, try LED_PIN = 12; or LED = 12; somewhere in your sketch

No doubt the wrong choice of terminology on my part? Sorry about that. Should I have said "constant" or "function"? Still learning. :slight_smile:

Constant - yes, function - no.

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.