So lately I've been playing around with my Arduino and I was wondering if there was some way I could program the Arduino in C++. I've been programming it using the C++/Processing language in Vim and using a makefile to compile and upload to the Arduino.
But my goal is to be able to use classes and all the great C++ features (or at least sum) to program it. Eventually I would even love to program it in raw C and I'm just having troubles finding out how to do either. It would be great if someone could point me in the right direction or help me out.
In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++).
Arduino sketches are written in C++.
As of version 1.6. 6, the Arduino IDE enables C++11 by default. For older versions, read on: It is very easy to change the flags for any element of the toolchain, including the assembler, compiler, linker or archiver.
Here is my experience: I'm building a robotic smart toy for autistic children using Arduino, sensors, motors, led and bluetooth. I wrote my own libraries to do exactly what I needed using C++. But I found out that the Arduino IDE Compiler is an older version that does not support the new C++11 features.
So I had to find myself a way to compile C++11 code and upload it to my Arduino. It turns out to be "pretty" basic: I needed a Makefile, the avr-gcc 4.8 toolchain and voilà! The makefile job is done by Sudar (https://github.com/sudar/Arduino-Makefile) and it works great. I had to customise it a bit to make it work for my project though.
Here is some documentation I've written for my project. You should take a look, it might be useful for you. https://github.com/WeAreLeka/moti/blob/master/INSTALL.md
Hope it helps! Cheers :)
EDIT 08/16/2014:
Because I got a lot a requests similar to this one from friends and other devs, I decided to set up some kind of framework to get up and running with your Arduino projects quickly and easily.
This is the Bare Arduino Project
Hope it could be of any help! If you find bugs or stuff that I could make better, feel free to fill and issue. :)
The language supported by the Arduino IDE is basically C++ with some additional features implemented by the Arduino programmers. Also, in a sketch you just code the setup
and loop
routines (there are a few others that you will eventually get to as you become a more advanced programmer).
In a sketch you can define classes in a library and include that library using the Arduino IDE. The Arduino IDE implements an Atmel compiler that creates code for the Arduino's processor (there are several models). You can work outside of the Arduino IDE (sounds like you are) but you still need to have a compiler that targets the correct Atmel processor.
Finally, C++ classes can become large, so at some point your source will outgrow what the processor can store. So, the Arduino classes are sparse and to the point!
To start with, I would use the Arduino IDE and write sketches (which are mostly C++ anyway). And as the occasion permits you can code your own libraries in C and/or C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With