Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to program an Arduino with C++ [closed]

Tags:

c++

c

arduino

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.

like image 279
Michaelslec Avatar asked Aug 30 '13 01:08

Michaelslec


People also ask

Can I code Arduino with C?

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

Should I use C or C++ for Arduino?

Arduino sketches are written in C++.

Does Arduino support C ++ 11?

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.


2 Answers

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

like image 70
ladislas Avatar answered Oct 19 '22 14:10

ladislas


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

like image 5
JackCColeman Avatar answered Oct 19 '22 15:10

JackCColeman