146

Short answer: do not try to “handle” the millis rollover, write rollover-safe code instead. Your example code from the tutorial is fine. If you try to detect the rollover in order to implement corrective measures, chances are you are doing something wrong. Most Arduino programs only have to manage events that span relatively short durations, like debouncing ...


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


61

Know this is old but I ran onto it during my search for Nano(V3)'s not uploading so thought might help someone else. Problem is the bootloader - Arduino IDE BUT I Found an easy solution (right under my nose). I realized that my nano's had been uploading just fine then I had finally updated the Arduino AVR Boards from 1.6.20 to 1.6.21. I didn't think ...


47

My general rule for embedded systems is to only malloc() large buffers and only once, at the start of the program, e.g., in setup(). The trouble comes when you allocate and de-allocate memory. Over a long run session, memory becomes fragmented and eventually an allocation fails due to lack of a sufficiently large free area, even though the total free memory ...


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


29

As is my wont, I'm going to provide a bit of background technical information into the whys and wherefores of this this error. I'm going to inspect four different ways of initializing C strings and see what the differences between them are. These are the four ways in question: char *text = "This is some text"; char text[] = "This is some text"; const char ...


27

It's going to be extremely difficult to get any kind of Python script running directly on the Arduino. The reason is that it's an interpreted language, so you would need the interpreter on-board in addition to the plain text script. There's probably not going to be enough memory for all of that. Your best bet would probably be finding a way to compile a ...


27

TL;DR Short version: An unsigned long is 0 to 4,294,967,295 (2^32 - 1). So lets say previousMillis is 4,294,967,290 (5 ms before rollover), and currentMillis is 10 (10ms after rollover). Then currentMillis - previousMillis is actual 16 (not -4,294,967,280) since the result will be calculated as an unsigned long (which can't be negative, so itself will roll ...


26

Your question has 2 parts actually. 1/ How can I declare the constant size of an array outside the array? You can either use a macro #define ARRAY_SIZE 10 ... int myArray[ARRAY_SIZE]; or use a constant const int ARRAY_SIZE = 10; ... int myArray[ARRAY_SIZE]; if you initialized the array and you need to know its size then you can do: int myArray[] = {1, ...


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.


18

The part of the code on an ATmega core that does setup() and loop() is at follows: #include <Arduino.h> int main(void) { init(); #if defined(USBCON) USBDevice.attach(); #endif setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } return 0; } Pretty ...


18

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


18

Typically, when writing Arduino sketches, you will avoid dynamic allocation (be it with malloc or new for C++ instances), people rather use global -or static- variables, or local (stack) variables. Using dynamic allocation can lead to several problems: memory leaks (if you lose a pointer to a memory you previously allocated, or more likely if you forget to ...


18

I have taken a look at the algorithm used by malloc(), from avr-libc, and there seems to be a few usage patterns that are safe from the point of view of heap fragmentation: 1. Allocate only long-lived buffers By this I mean: allocate all you need at the beginning of the program, and never free it. Of course, in this case, you could as well use static ...


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.


18

The short answer: You don't. With enough know-how, you could probably extract the executable binary from the Arduino, but the source code is not installed on the device. You would need to run a decompiler on the binary. (Or read the machine code directly.) The output of a decompiler is usually pretty ugly however, and will look quite different from the ...


17

Yes, it is (somewhat) possible to program the Arduino using Python. One such project on Github is the Python Arduino Prototyping API v2. It provides very basic functionality such as digital I/O and analog I/O. This can be used for very simple projects. *This project is a bit of a hack at "programming" the board using the serial connection. It passes the ...


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

The __DATE__ and __TIME__ are set when the code is compiling so they will naturally be behind since the code still needs to finish compiling and then be flashed to the chip. See the Arduino Playground for an example of how to sync it to your computer over serial. TimeSerial.pde shows Arduino as a clock without external hardware. It is synchronized ...


15

It is not possible to declare and use classes declared in one .pde file in another .pde file from within the Arduino IDE. One workaround is to make the second file into a C++ source file (.cpp) and then add a #include "<filename>" directive in the beginning of the first file. This code compiles correctly: Tab 1: #include "test.cpp" TestClass obj; ...


15

If you really want to recurse (and as @jippie said it is a bad idea; subliminal message: don't do it) and want to know how much you can recurse, then you will have to perform some calculation and experiments; also you generally will have only an approximation of it as it depends a lot on the memory state at the time your recursive function will be called. ...


15

This isn't weird looking. It's what normal MCU code actually looks like. What you have here is an example of the concept of memory-mapped peripherals. Basically, the MCU hardware has special locations in the SRAM address space of the MCU assigned to it. If you write to these addresses, the bits of the byte written to address n control the behaviour of ...


14

As already pointed out, your Arduino is saying too much too fast. Adding delay() will slow it down, but still it keeps yelling at Processing. Ideally, you want Processing to ask for the value when it's convenient, and then receive one answer from your Arduino. Enter SerialEvent(). As opposed to loop() on your Arduino and draw() in Processing, everything ...


14

The extension you use for a header file doesn't technically matter because the compiler never sees it. You could name it with the extension .this-is-a-header-file and it would probably still work. The compiler only handles the C/C++ source files, typically named .c and .cpp. However, before that happens, the preprocessor goes through and looks for any #...


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


14

What are the generally practiced methods for the optimization program memory usage? First, note you are searching for ways to lower SRAM memory. This contains global (variable) memory and heap space (dynamic memory + stack memory). Avoid memory gaps, by not using dynamic memory (with free/malloc/new). Avoid using the String class. Avoid global memory in ...


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