How to program arduino with C++ [closed]

2019-03-08 02:21发布

问题:

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++. What I've been doing is 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. Thanks! ~michael

回答1:

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



回答2:

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



标签: c++ c arduino