Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

What are the advantages of each language when using the Arduino?

I'm thinking this is a good general question, but I'll add a bit about why I'm asking if anyone wants to give me a tip.

I'm experienced in preprocessed languages like JavaScript, PHP, and have fiddled with languages like Java and Visual Basic. In other words I know programming techniques and both classical and prototypal object orientation, but nothing about communicating directly with hardware.

I'm making an octocopter, and am thinking that an object oriented approach will be the easiest. (The software will have very many features...) However I have never written in C++.

Since this is a Q&A site that's supposed to help others, only the general question presented at the beginning is of much importance, but I'd appreciate any comments on my situation.

share|improve this question
1  
Avoid Arduino IDE as it does not deserve its IDE name, it is even a very poor editor: it may be good for beginners and simple sketches but one should rapidly go to something better . –  jfpoilpret Mar 20 at 0:22

6 Answers 6

up vote 12 down vote accepted

My personal experience as professor (programming, mechatronics) is that if you have previous programming experience and you are aware of concepts as OOP, it is better to go for C/C++. The arduino language is really great for beginners, but have some limitations (e.g. you must have all your files in the same folder). And it is basically a simplification of C/C++ (you can practically copy&paste arduino code to a C/C++ file, and it will work). Also it makes sense that you can go and use a full well known IDE as eclipse:

http://playground.arduino.cc/Code/Eclipse

Initially it is required a bit more of setup and configuration of your dev environment, but IMHO it is worth it for programmers with experience in any other language.

In any case, it won't harm you to start using the arduino language and the arduino IDE for a few days to get familiar with the arduino hardware and then move to C/C++ with Eclipse for really developing your project.

share|improve this answer
    
Thank you for your thoughts. I'll use the Arduino IDE to learn how to control all the different hardware pieces like sensors and LCD screens etc. Then I'll move up to C++ to make the software for the octocopter. Do you know a good and short book for people who know other languages? The "problem" with many books I've found is they include so much that I don't need because I'm on a small Atmel microcontroller rather than a UI computer. –  Friend of Kim Mar 19 at 15:26
1  
There is a nice book "C Programming for Arduino" by by Julien Bayle which is useful. It is C not C++ language but should help you to understand the basis. –  franchuti688 Mar 19 at 15:55
    
Thats a good request: a short and good book with the basics of C++ for programmers of other languages. Sorry I cannot give any recommendations, i am not aware of any good one. In fact, I wrote a book, with a practical focus (develop a videogame) for my students, but it is only in spanish. For a good and practical C++ book I would recommend Deitel&Deitel How to program C++, you could skip many advanced chapters. –  drodri Mar 19 at 16:07
    
C++ supports OO features, C language does not. –  Chris O Mar 19 at 17:33
1  
@FriendofKim Not exactly what I meant (though technically you could, the Due is fast enough to support a software solution for non-megabit serial communication, and it does have 5 U(S)ARTS in total while the Arduino libraries only provide 4 of them but iirc the Due board does not provide pins for one of the USARTs anyway); the Arduino libraries configure the USARTs to use asynchronous mode, but I needed synchronous at up to 6Mbps (and I had to use SPI mode as the input had to be treated as a raw bitstream so no stop bits/etc.), which also involved modifying the default PIO controller config. –  JAB Mar 21 at 13:18

In theory...
There isn't really an Arduino language as such. It's really just C++ with some domain-specific libraries. These add on various features, such as functions you can call to control the hardware. If you didn't have those functions, you'd need to fiddle directly with special registers to control everything. That's how embedded programming is usually done. It's fast, but it can be quite hard to learn and understand.

In addition to the functions, the libraries add alternative names for some types. For example, boolean and byte are not in the C++ standard. However, they are directly equivalent to bool and unsigned char.

All of these things mean you can probably port general C++ code directly to Arduino without difficulty. However, going back the other way may require some minor editing.

In practice...
Having said all of that, programming for Arduino isn't exactly the same as general C++ programming. A lot of the differences are common to all embedded programming though (such as limited memory and processing power).

It's also worth noting that if you're using the official Arduino IDE then there are all sorts of annoying quirks and limitations on how you setup your code. There are workarounds in all cases (as far as I'm aware), but they are sometimes quite frustrating.

For full flexibility, use a third-party IDE (such as Eclipse) with a plug-in to support Arduino. That should give you all the advantages of C++, along with the Arduino libraries.

share|improve this answer

What are the advantages of C++ vs the Arduino language when using Arduino? I'm experienced in preprocessed languages like JavaScript, PHP, and have fiddled with languages like Java and Visual Basic.

First, the Arduino compiler/IDE accepts C and C++ as-is. In fact many of the libraries are written in C++. Much of the underlying system is not object oriented, but it could be.

Thus, "The arduino language" is C++ or C.

C++ is not garbage collected. It does manage variables in scope - if you write:

int led = 13;

void blinkTimes(int value)
{
   int i;

   for(i=0;i<value;i++)
   {
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      delay(1000);
   }
}

Then you'll find that led and i don't grow or leak, no matter how many times you call blinkTimes.

If i were a class, it would similarly be disposed of once the function ended. So as long as you aren't using new or similar memory allocation functions to create new objects, then you won't have to worry about leaks.

You may still run out of memory, if you create huge classes and use a lot of them in deeply nested functions, but in general you aren't going to run into trouble until you start dealing with new and free functions.

If you are using new, then you'll have to call delete at appropriate times. C++, and by extension Arduino, has no automatic garbage collection, you have to explicitly manage your own memory.

share|improve this answer
    
Okay, if instantiated objects (with new) aren't garbage collected, I'll have to learn that as well. I suppose any book on C++ will cover that. Thank you for a good answer! –  Friend of Kim Mar 19 at 18:44

The Arduino language is C++, but it is very different from most C++ varieties. The Arduino language has a lot of abstraction built in, especially in the hardware interfaces, which makes it very simple to use. If you have a background in Java, C and C++ should be very similar.

The main differences between Arduino and C++ are in the memory storage. Usually a modern computer has more than 2GB of RAM, while the Arduino Uno has 2kB (1 million times less). The Arduino also uses 8-bit instructions in stead of the 32 bit ones a computer uses. This will mainly affect the amount of information you can store in a variable.

If you are very new to the Arduino world, you should look up a tutorial, as there are many good ones out there.

share|improve this answer
    
You mention memory. I have the Due which has got 96KB of RAM. But the last thing I want is for the copter to crash because of a memory leak (having a very expensive camera mounted on the copter). –  Friend of Kim Mar 19 at 15:29
    
Is memory in C++ handled automatically (like the garbage collector in Java) if not specified otherwise? –  Friend of Kim Mar 19 at 15:30
1  
The C++ standard makes no guarantees about memory sizes - c++ already runs on embedded hardware without modification. –  Michael Thorpe Mar 19 at 16:00
    
@MichaelThorpe I mean, do you have to specifically release memory when you don't need it anymore? Let's say you define 20 variables inside an infinite loop, will the memory be automatically released for every loop? –  Friend of Kim Mar 19 at 16:07
1  
@FriendofKim In C++, there are two different ways of instantiating variables/objects. If you instantiate them locally (e.g. int blah = 5;) then they will be destroyed automatically when they go out of scope (i.e. at the end of the loop or function). However, if you instantiate them on the heap (e.g. int *blah = new int(5);) then you need to release them yourself. It's not usually wise to use heap data in embedded programming though. –  Peter R. Bloomfield Mar 19 at 16:43

C++, if those are your two choices:

  • C++ is a standardized language. It is widely deployed in many environments including embedded systems and therefore is more thoroughly tested than the very-like-C++ language that is "Arduino". This is especially important for mission-critical / safety-critical applications such as you are planning. Crashed code means a crashed 'copter and even if it doesn't hurt someone, it will break your expensive machine.

  • Being standard, C++ is portable. Need to upgrade your processor? Everything but silicon-specific code will port to the new one. Need to change your toolset, development system, host OS? C++ will be supported everywhere. Though Arduino IDE will run anywhere Java is supported, it is the only tool that uses Arduino C++ and it is a very limited tool. If you want to use Eclipse, the AVR tools, go barefoot on the command-line, develop within Emacs, or whatever other environment you prefer, standard C++ will be supported.

  • The Arduino IDE does things behind your back - specifically, it #includes .h files when it thinks you need them. Even if it is correct, you really want to write, or at least see and understand, everything the compiler is going to see. Programming languages aren't made for computers (computers eat bits for breakfast); they're made for people, specifically the people who follow you on the project, the most important of whom is ... you!, when 6 months after you wrote a module, you have to come back to enhance it, or more likely, fix it. You really want to be able to see everything the compiler sees.

Do limit your use of C++ language features to those whose implementation you fully understand. There are some features that compile to more resource-intensive code than is obvious from reading the source code. The optionally generated .lss (merged source and assembly) listing can give you some good insight into what the C++ compiler did when you weren't looking.

To your question about memory: C++ does not garbage-collect. Stack-based languages like C and C++ do allocate temporary storage on the stack for automatic variables at function entry, which then gets released when the function returns, but this is not true garbage-collection. Objects created in heap- or global memory live until you explicitly delete them. Make sure you know where, when, and for how long different kinds of objects will be created. You really don't want your code new-ing and delete-ing objects willy-nilly. They will be built in heap memory, fragmenting it and causing it to grow up into the stack. That's when your code - and your nice 'copter - will crash.

C does less for you so it can do less to you. It's not a bad choice. C with a few of C++'s features can be an even better choice, given you choose the extra features judiciously.

share|improve this answer
    
This is a REALLY good answer. It seems clear that you like C++ and that I should like it as well! ... But seriously, I'll jump right onto C++ as soon as I've understood how the "communication" with the different sensors etc. work. –  Friend of Kim Mar 19 at 18:49
    
Do you know any good books on the subject? (Suited for me who have been programming for years in other languages, and only need the "embedded part" of C++.) –  Friend of Kim Mar 19 at 18:50
    
Do you think Accelerated C++ is a good book for this? –  Friend of Kim Mar 19 at 18:58
2  
I wish I could help you with the 'right' book, but I came to C++ with a whole lot of C behind me and Kernighan & Ritchie's The C Programming Language is still my go-to book (bad pun) for anything not explicitly C++, and Ellis & Stroustrup's The Annotated C++ Reference Manual for C++. But for writing to hardware, you'll do well to look at some of the device libraries contributed by users at Arduino.cc. Start off tweaking I/O ports with pinMode(), digitalWrite() and digitalRead() functions, then their analog i/o counterparts, and eventually, ... –  JRobert Mar 19 at 21:21
1  
... plan to write directly to I/O registers for speed and memory-space improvements. Programs like blinky.cpp and hello.cpp are good places to start messing around with hardware. Add a simple multimeter, and a few LEDs with the right current-limiting resistors permanently soldered to one leg and you should be able to see real, if simple, results from your efforts. After that, read data-sheets for any hardware that interests you to learn what signals you can read / must write to them to make them play. Temperature sensors are easy to use and doesn't everyone need a fridge logger? Have fun! –  JRobert Mar 19 at 21:30

In my experience, it is best to avoid new and delete when running on machines with limited memory.

  • The memory management itself uses valuable program and RAM space
  • ISR vectors are set at compile tme. It is difficult (impossible?) for a class instance to claim an ISR at run time
  • Generally you will know at compile time what class instances you need - e.g. 3 button debouncers, a display driver and a 2-line display driver
  • Memory management introduces unknown delays when you use new or delete.

There may be cases where it is justified to use dynamic instance construction and destruction, but I suspect they are rare.

share|improve this answer
    
Being completely new to C++, I have a followup question regarding the STD. Is it a bad idea memory wise to use it? If not, how can it be added (Eclipse)? I've set up the AVR compiler and its working well. –  Friend of Kim Mar 31 at 9:53
    
All what you say is correct but I don't see how this is an answer to OP question. new and delete are available in Arduino IDE too. –  jfpoilpret Mar 31 at 11:06
    
It's not an answer, but very helpful information that I really appreciate. –  Friend of Kim Mar 31 at 13:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.