Programming Arduino in plain C or C++

Hello to all of you out there! I am glad that there is a huge community of Arduino users and developers.

I spent about 15 min searching this forum for an answer to my simple question, but with no luck... I apologize for not spending more time, but I am eager to find an answer.

I am a C and C++ programmer and I have to program a little robot using lots of Arduinos. The problem is that the IDE is not sufficient for me. I want to program in plain C++, but still have access to Arduino core API like beginSerial, pinMode, digitalWrite, digitalRead and all the functions available through the IDE. Because I am very new to Arduino but have a very strong knowledge in C++ it will be a lot easy for me to include some header files and access the Arduino API in my C++ program as I would use it from the IDE.

To give you an idea... I want to make a multithreaded program were I use about 10 USB/serial ports to communicate simultaneously with 10 Arduinos. So, is there a way to use the same functions from the IDE in my C++ program? (I saw some avrlibc code, but is too complex for my needs and don't have time to learn it)

I thank you in advance! (And I apologize if this is a silly question.)

There is a makefile available somewhere on the site. It's a bit old and hasn't been updated for version 0010 of the API, but it's easy to port... as a matter of fact... here's mine (it may have some extras left in there) http://pastie.textmate.org/private/fl1fwwsqqfqm4tl2mmwsyw. It works nicely with 0010. I never use the IDE anymore, it's terrible when you're used to great stuff like TextMate (shameless plug). You'll have to edit the path to the Arduino directory and add any libraries you use manually (there are examples for TWI and OneWire). Also note that if you're not going to use the bootloader and use a ISP programmer you will need to remove the "-F" argument of the Avrdude flags or you're very possibly fry your chip. (-F is needed if you use the bootloader and upload your sketch using serial/usb). Enjoy! Hope this helps

EDIT: oh yeah if you're under windows, you could try integrating the API with AVR Studio, at that point you'd have a powerful API, IDE and Debugger...

EDIT 2: Oh yeah just as side note, when you use the IDE, you're still using "straight" C/C++.

Thank you very much!

I'll keep you informed about my progress...

To give you an idea... I want to make a multithreaded program were I use about 10 USB/serial ports to communicate simultaneously with 10 Arduinos. So, is there a way to use the same functions from the IDE in my C++ program? (I saw some avrlibc code, but is too complex for my needs and don't have time to learn it)

Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?

--Phil.

Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?

I have to say, I did ask myself the same question...

Yes, you can pretty much just write "real" C++ or C code within the arduino environment and it will compile fine.

However, be aware that the Arduino hardware environment is very much "bare metal" with no operating system, so many of the capabilities you might be used to in host-side C++ programming will be unavailable, since they are provided by a combination of the host operating system and posix libraries that are not running on the Arduino (and probably wouldn't fit there at all.) So if you want to have your windows-side multithreaded app talk to 10 arduinos, each of which is running C++ code with complex data structures, user-defined classes, and other C++ features you haven't read about in the Arduino literature, you're probably in good shape. If you want to run multi-threaded code ON the arduino, then you're in trouble.

Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?

Of course I am not planning to burn such a complex program on the Arduino board! :slight_smile: The program will run in Linux on a FOX Board or on an ASUS EEE PC. I will use standard multithreading functions, mutexes, semaphores and so on to synchronize the boards.(receiving some data from a board, sending data to another)

For the moment I'm just making plans. It's possible to encounter problems when I'll try to get it work...

Anyway, I want to thank you. That Makefile was really useful. I now understand how it all works. :wink:

Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.

If you want to run multi-threaded code ON the arduino, then you're in trouble.

Actually there are one or two OS project for the AVR which supports multithreading. But that's out of the scope of this forum.

AVRx
http://picoos.sourceforge.net/features.html

Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.

Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...
I know that my question sounded silly, but I am a programmer with no experience at all in controller programming. I found about Arduino a few days ago and found it very interesting, simple and fun to work with. That's because this huge community of users and because it's open sourced. I'm now trying to understand/learn the AVR library. :wink:

If my project will be a success I will share my ideas. :wink:

Actually there are one or two OS project for the AVR which supports multithreading. But that's out of the scope of this forum.
AVRx
pico]OS - pico]OS - Features

Very, very interesting! Thanks xSmurf! :slight_smile:

Anyway, I want to thank you. That Makefile was really useful. I now understand how it all works. :wink:

Pretty much the only thing it doesn't do that the IDE does is auto prototypes, "hot linking" of libraries and burning the bootloader (but there's already a makefile for that). So yeah, use at will!

(Sorry for the double post)

Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...

I'm still not sure I got this... Which program? The one that runs on the host machine? If so, that's not possible. What you're compiling is AVR object code. It will not run on a PC (x86) and talk to the parallel/serial port...

Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.

Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...

If the purpose of the arduinos is to perform i/o on behalf of the central control program then I can't see that using those low level commands is the best way to do it. Why not interface at a higher level with commands like move, turn, get position etc and let the arduinos map these to physical i/o devices.