Hot answers tagged

104

Introduction to SPI The Serial Peripheral Interface Bus (SPI) interface is used for communication between multiple devices over short distances, and at high speed. Typically there is a single "master" device, which initiates communications and supplies the clock which controls the data transfer rate. There can be one or more slaves. For more than ...


63

There is an Arduino Eclipse plugin named sloeber! And Eclipse is an awesome cross-platform open-source IDE! Stino is good. It requires Sublime Text 2 which has an indefinite free trial. Visual Micro provides a full build system with debugger for Arduino in Microsoft Visual Studio. For advanced users it also allows the underlying Arduino source code to be ...


60

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


51

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


32

How the IDE organizes things First thing, this is how the IDE organizes your "sketch": The main .ino file is the one of the same name as the folder it is in. So, for foobar.ino in foobar folder - the main file is foobar.ino. Any other .ino files in that folder are concatenated together, in alphabetic order, at the end of the main file (regardless of where ...


32

TL;DR : When writing an Interrupt Service Routine (ISR): Keep it short Don't use delay () Don't do serial prints Make variables shared with the main code volatile Variables shared with main code may need to be protected by "critical sections" (see below) Don't try to turn interrupts off or on What are interrupts? Most processors have interrupts. ...


28

The backslash in your comments are the problem. As per C++ language definitions, a \ at the end of the line is interpreted as a "line continuation". Thus, your comment gets continued in the next line and your variable declaration and initialization is commented out. This is apparent when opening your code with a good syntax highlighting program, like ...


26

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


25

Expanding on Fake Name's answer, there is also Visual Micro's Arduino plugin for Atmel Studio (built off Visual Studio). It shares the tools, sources and libraries with the Arduino IDE but provides all the code completion and other features of Visual Studio. The Visual Micro plugin also works in all versions of Visual Studio. Provides Arduino usb debugging ...


24

It's important to note that const int does not behave identically in C and in C++, so in fact several of the objections against it that have been alluded to in the original question and in Peter Bloomfields's extensive answer are not valid: In C++, const int constants are compile time values and can be used to set array limits, as case labels, etc. const ...


22

vim can be used for Arduino development, but it will take some configuration (as is the way of vim). You'll need the syntax file and this plugin that enables you to compile and deploy from vim. Grant Lucas produced a great write-up on getting your environment situation squared away: Using Vim for Arduino development Here's a quick post on setting up ...


20

You can use AVR Studio to write programs for Arduinos. There is a plugin that lets you link against and use the arduino libraries from within Atmel Studio. If you've used Microsoft Visual Studio, Atmel Studio is basically a reskin of Visual studio with different compiler backends that target the ATmega series of devices, so you'll feel right at home.


19

There is a half way solution as well since the Arduino IDE has support for external a editor, then the Arduio IDE just compiles and uploads. Now you can use whatever IDE/editor you like to edit the code, and then you switch back to the Arduino IDE to press the compile and upload button when you are done.


19

There is a function in the standard Arduino library called dtostrf(). I think of it as "Decimal to String Float". You pass in the float, how wide you want the whole number to be (if it will fit), the number of decimals of precision - and the buffer you want it to fill. Note that! You need to provide the buffer, and you need to be careful to provide more ...


18

Because you have a ; at the end of your #define. #define Uin A0; analogRead(Uin); becomes: analogRead(A0;); The Arduino website mentions this in their reference. For more in-depth detail about the C pre-processor you can read the (rather large) manual here.


17

As taken from the accepted answer from When should you use a class vs a struct in C++? The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and ...


17

One rather good IDE extension is the Arduino Mode for Emacs. It allows using a highly versatile and extensible IDE such as Emacs for writing, compiling and uploading code to the Arduino. The documentation and instructions can be found here.


17

Most Arduinos (like the Uno or Nano) have very few RAM, thus you first need to make sure that you never allocate too much memory. Dynamically allocating memory can also lead to heap fragmentation (heap being the part of memory where dynamic allocation happens). In most cases you would want to allocate memory of different sizes (for example arrays of ...


16

--- Update 170412 I wrote my original answer three years ago from the perspective of there existing a distinct "Arduino C++". The language used in the IDE is standard C++, because it is implemented by the GNU C++ compiler. "Apparent" differences creep in because the IDE will do some pre-processing to help newcomers to the language avoid a couple of 'gotcha's,...


16

Asynchronous serial (usually referred to as serial) communications is used to send bytes from one device to another. A device could be one or more of the following: Arduino PC GPS RFID card reader LCD display Modem Other Clock rate and sampling of data Unlike SPI / USB / I2C serial communications does not have a clock signal. The sampling clock is an ...


15

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


14

First, it's volatile not Volatile. I cover these concepts in my page about Interrupts however to avoid giving a link-only answer I'll repeat the relevant bits. What are "volatile" variables? Variables shared between ISR functions and normal functions should be declared volatile. This tells the compiler that such variables might change at any time, and thus ...


14

See my answer here: Classes and objects: how many and which file types do I actually need to use them? - specifically: How the IDE organizes things. I know that at a minimum you have to include the Arduino header file Yes you would need to do that. but beyond that, what would cause a compilation error if compiling said .ino to .cpp file using, say, ...


13

It's a misconception that using constructors or C++ in general requires lots of RAM. Plus, to be clear, the AVR processors like the Micro and Uno have separate RAM and PROGMEM (program memory). So, code for functions does not, in itself, use RAM. As an example, this small program for the Micro: int main () { } That uses only 206 bytes of PROGMEM and 0 ...


12

UPDATE-> Codebender is being shut down. There is a brilliant open source, web based IDE called Codebender It is like Github for Arduino and has a great community working from it with thousands of examples and the dev team are exceptional. With codebender, you can: program an existing sketch to your device clone an existing sketch modfy it to your ...


11

Majenko created and maintains UECIDE. His contributions are pretty epic; I've seen first hand his coding prowess as we worked together to get the Adafruit 1.8" TFT Joystick shield working on the chipKIT platform.


11

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


11

Yes, the analog pins must be addressed using A0, A1,... when using them for digital I/O. Depending on the board you are using A0,A1,etc. are mapped to different values (for instance it looks like A0 is 18 on some boards but 14 on others. One solution for looping over the analog pins would be this: static const uint8_t analog_pins[] = {A0,A1,A2,A3,A4}; // ...


11

Just change void sendSMS() to void sendSMS(const String& thisIsAString) You can then access the parameter inside the function with thisIsAString. No, you do not need a prototype.


11

I would just like to add a few points to Nick Gammon's answer: You do not need to rename a .ino file in order to compile it: if you explicitly tell the compiler it's C++ (option -x c++), it will ignore the unusual file extension and compile it as C++. You do not need to add #include <Arduino.h> in the .ino file: you can tell the compiler to do that ...


Only top voted, non community-wiki answers of a minimum length are eligible