Go Down

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

Zoandar

OK, so turning a totally 'blind' eye to what I am actually seeing - i.e. pretending I have never seen ANY C++ before, here is the first code from the first C++ tutorial on the first Google hit I got for "C++ beginner tutorial".

Code: [Select]


#include <iostream>

using namespace std;

int main()
{
  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
  cin.get();
}




How much or how little of it actually is usable by Arduino?
Zoandar
  ---v/\/\/\v---

AWOL

With a bit of fiddling, you can use almost the whole of this:
Code: [Select]
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Zoandar

Well, I tested this code in Arduino. I got the following error messages on the verification:

iostream - no such file or directory

(it didn't like int main() but does not say why (love these cryptic messages!!)

'cout' not declared

'cin' not declared

Also, iostream, cout, and cin return "no reference" when I try to find them in the Reference section using Help/find in reference.

So basically, the whole sketch is a bust. This helps drive home my earlier point. Whenever I have tried to look outside of the Arduino Reference to understand the language, I only find things that have nothing to do with Arduino's version of C++. A complete beginner trying to use the arduino would stall out on the first page of that C++ Beginner tutorial because it starts right out teaching 'misinformation' when applied to Arduino.
Zoandar
  ---v/\/\/\v---

Zoandar


With a bit of fiddling, you can use almost the whole of this:
Code: [Select]
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";



Thanks, but since I am a beginner, I would not know what 'fiddling' to do to make it usable. :)
Zoandar
  ---v/\/\/\v---

AWOL

#19
Mar 09, 2011, 09:43 pm Last Edit: Mar 09, 2011, 09:58 pm by AWOL Reason: 1
Quote
So basically, the whole sketch is a bust.


A sketch doesn't have a "main", so you're confusing it with a program.

"cout" and "cin" are niceties provided by the operating system the Arduino doesn't have.

"iostream" is usually directed at something like "cout".

So, really there is no problem - you're trying to access stuff you haven't got.

Personally (being old-school C) I don't like the overloaded "<<" operator because it (to me) is short on meaning, but if you're interested see:
http://arduiniana.org/libraries/streaming/
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Zoandar


Quote
So basically, the whole sketch is a bust.


A sketch doesn't have a "main", so you're confusing it with a program.


Does this mean I cannot name a function as "main" in Arduino? I seem to recall from my ancient Basic days that there were some words I was not allowed to use for certain things. Is there a list of these somewhere for Arduino?
Zoandar
  ---v/\/\/\v---

AWOL

#21
Mar 09, 2011, 10:07 pm Last Edit: Mar 09, 2011, 10:14 pm by AWOL Reason: 1
Quote
Does this mean I cannot name a function as "main" in Arduino?

That's correct because the Arduino provides a "main" for you.
It also provides a function called "init".

Note that these are not names reserved by the language, like "for" or "return".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AlphaBeta

Here is the Arduino equivalent:
Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial.println("HEY, you, I'm alive! Oh, and Hello World!");
}

void loop() { /*nothing to do in the loop*/ }

Zoandar

That looks a little more familiar! :)

So what does "init" do? (another missing piece of info in the Arduino Reference).
Zoandar
  ---v/\/\/\v---

AWOL

#24
Mar 09, 2011, 10:17 pm Last Edit: Mar 09, 2011, 10:20 pm by AWOL Reason: 1
Quote
So what does "init" do?

It initialises all the stuff that you don't normally have to worry about, like the timers that run "millis" and "micros".

That's the great thing about Arduino - what you don't know (mostly) can't hurt you.
Until you really want to know.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AlphaBeta

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.

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.
Zoandar
  ---v/\/\/\v---

AWOL

#27
Mar 09, 2011, 10:34 pm Last Edit: Mar 09, 2011, 10:54 pm by AWOL Reason: 1
Quote
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
Code: [Select]
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
Code: [Select]
Serial << "Hello World!" << endl;, which may seem the same, though some of the wider implications of "cout" are absent.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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

Code: [Select]
#define LED 13

and then to light it he used

Code: [Select]
digitalWrite(13, HIGH)

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

Code: [Select]

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
Code: [Select]

digitalWrite(LED_PIN, High);


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


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

AWOL

#29
Mar 09, 2011, 11:27 pm Last Edit: Mar 09, 2011, 11:30 pm by AWOL Reason: 1
Quote
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
Code: [Select]
LED_PIN = 12; or
Code: [Select]
LED = 12; somewhere in your sketch
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up