Ohio - USA
Offline
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #15 on: March 09, 2011, 03:23:00 pm » |
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". #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
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #16 on: March 09, 2011, 03:27:22 pm » |
With a bit of fiddling, you can use almost the whole of this: 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.
|
|
|
|
Ohio - USA
Offline
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #17 on: March 09, 2011, 03:39:37 pm » |
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
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #18 on: March 09, 2011, 03:41:27 pm » |
With a bit of fiddling, you can use almost the whole of this: 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. 
|
|
|
|
|
Logged
|
Zoandar ---v/\/\/\v---
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #19 on: March 09, 2011, 03:43:11 pm » |
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.
|
|
|
|
Ohio - USA
Offline
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #20 on: March 09, 2011, 04:04:40 pm » |
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
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #21 on: March 09, 2011, 04:07:00 pm » |
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.
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #22 on: March 09, 2011, 04:07:35 pm » |
Here is the Arduino equivalent: 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
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #23 on: March 09, 2011, 04:14:13 pm » |
That looks a little more familiar!  So what does "init" do? (another missing piece of info in the Arduino Reference).
|
|
|
|
|
Logged
|
Zoandar ---v/\/\/\v---
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #24 on: March 09, 2011, 04:17:58 pm » |
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.
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #25 on: March 09, 2011, 04:22:22 pm » |
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
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #26 on: March 09, 2011, 04:26:23 pm » |
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
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: March 09, 2011, 04:34:27 pm » |
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.
|
|
|
|
« 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.
|
|
|
|
Ohio - USA
Offline
Full Member
Karma: 0
Posts: 104
Arduino rocks
|
 |
« Reply #28 on: March 09, 2011, 04:52:19 pm » |
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 #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". 
|
|
|
|
|
Logged
|
Zoandar ---v/\/\/\v---
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19044
I don't think you connected the grounds, Dave.
|
 |
« Reply #29 on: March 09, 2011, 05:27:59 pm » |
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
|
|
|
|
« 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.
|
|
|
|
|