Pages: 1 [2] 3 4 ... 6   Go Down
Author Topic: Understanding this line " ? HIGH : LOW; "  (Read 7718 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

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:

#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?
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

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

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

With a bit of fiddling, you can use almost the whole of this:
Code:
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. smiley
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
« Last Edit: March 09, 2011, 03:58:22 pm by AWOL » Logged

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

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

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?
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
« Last Edit: March 09, 2011, 04:14:27 pm by AWOL » Logged

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

That looks a little more familiar! smiley

So what does "init" do? (another missing piece of info in the Arduino Reference).
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 09, 2011, 04:20:07 pm by AWOL » Logged

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
Serial << "Hello World!" << endl;
, which may seem the same, though some of the wider implications of "cout" are absent.
« Last Edit: March 09, 2011, 04:54:32 pm by AWOL » Logged

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

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

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

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:
#define LED 13

and then to light it he used

Code:
digitalWrite(13, HIGH)

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

Code:
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:
digitalWrite(LED_PIN, High);

As they saying goes - "same difference". smiley


Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
LED_PIN = 12;
or
Code:
LED = 12;
somewhere in your sketch
« Last Edit: March 09, 2011, 05:30:35 pm by AWOL » Logged

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

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