Hot answers tagged

17

I use the command-line interface to the arduino command. I run it like this: arduino --upload sketch/sketch.ino --port /dev/ttyUSB* There is a page which describes other command-line tools, like inotool. That page also has an example Makefile. These alternatives seem enticing, but apparently, as of this writing, none of them work. I'm assuming this is due ...


13

Yes. Here is the syntax: #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) //Code here #endif You can also do something like this for the Mega: #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //Code here #endif Assuming the implementation for the ATtiny is correct, your code should be like this: #if defined (...


13

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


13

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


12

Thanks to @Majenko I looked some place new: documented in the arduino plugin of VS Code Arduino Extension there is an option to set an output directory. Note though that according to this it should not be in the workspace or subfolders. So in arduino.json settings file add: "output": "../ArduinoOutput"


9

Find the io__.h file for your microcontroller, on Linux it is located in /usr/lib/avr/include/avr, on Windows it will be in a somewhat similar location. Scroll down to the part that says /* Constants */. There are couple interesting macros defined there, FLASHEND being the one you should be interested in. You can use it for example as follows: #if FLASHEND ...


9

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


9

I would use conditional compilation, like this: #ifndef(ARDUINO) // Non-Arduino code. #endif


8

To clarify the answer, for those new in arduino world +StudioCode (source: https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino ) The following settings are as per sketch settings of the Arduino extension. You can find them in .vscode/arduino.json under the workspace. { "sketch": "example.ino", "port": "COM5", "board"...


7

Short answer: No; From the atmega328's data sheet (though it applies to all AVR's): AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program ...


7

Arduino compiles in a lot of standard libraries, interrupts, ... etc. For example the pinMode and digitalWrite functions use a lookup table to figure out at run time which GPIO registers to write data to. Another example is that Arduino keeps track of time, it defines some interrupts by default and all this functionality requires some space. You'll notice ...


7

If you add a debugging print you will see what is happening: void setup() { Serial.begin (115200); Serial.println (); pinMode(13, OUTPUT); int len = 5000; byte *data = (byte *)malloc(len * sizeof(*data)); Serial.print ("data = "); Serial.println ((int) data); } Output: data = 0 The malloc failed, it returned NULL, the rest of the program ...


7

I would suggest Googling for Makefile projects. I did one a while back for the Blink program, by basically seeing what got generated by the IDE and replicating that in a more general way. # # Simple Arduino Makefile # # Author: Nick Gammon # Date: 18th March 2015 # where you installed the Arduino app ARDUINO_DIR = C:/Documents and Settings/Nick/Desktop/...


7

Is there a way to compile arduino code without automatically uploading it, so that I can put the pre-compiled binary up for download on a website? Yes, you can save the .hex file produced by the compiler. If you turn on verbose output from the compiler you can find the temporary directory where it is stored. Simply copy from there to a folder for download ...


6

My source files are *.ino, but after compilation there aren't any other files placed in the folder. Correct. The Arduino IDE performs compilation in a separate temporary directory, whose location you can see if you enable verbose compilation in the IDE options. Does the compiler generate intermediate assembler, and if so, where does it leave them? By ...


6

As I turns out, the avr-gcc (GCC) 4.9.1 goodies weren't being used at all! The arduino package was using a decrepit version of gcc, prakhar@sim74stic ~ $ /usr/share/arduino/hardware/tools/avr/bin/avr-g++ --version avr-g++ (GCC) 4.3.2 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO ...


6

The way the Arduino IDE works is that it compiles your code (the code you write in the IDE) as the "main" code. Then it pulls code from all of the libraries you have imported and compiles that along with the main code. To do what you are suggesting would require you to create a library for Arduino. Here is some more information on Arduino libraries: http://...


6

The YUN is a combo. Part Arduino and Part OpenWRT(Linux). Your question is in reference to the Arduino. Where this is actually a ATmega32u4 similar to a Leonardo and not an UNO(ATmega328p). The 32u4(Leo) communicates via Virtual Serial Ports over the USB (short answer: this needs to be supported) , where the UNO has a real Serial Port (aka UART). Below are ...


6

Adding hysteresis behavior to your code is not difficult. You just need to store the state you're in and make the thresholds for transitioning into another state dependent on that. You can use an enum to store the state: enum BatteryStates { Red, Orange, Green }; Then, instead of defining and checking against one threshold value, define two thresholds ...


6

It's no different to working with Make and any other form of GCC. Just set your CC variable and CFLAGS variable accordingly and work as per normal. For instance, I just knocked this one up: CC=avr-gcc OBJCOPY=avr-objcopy CFLAGS=-Os -DF_CPU=16000000UL -mmcu=atmega328p PORT=/dev/ttyACM0 led.hex: led.elf ${OBJCOPY} -O ihex -R .eeprom led.elf led.hex ...


6

See my answer How the IDE organizes things. There is no easy way to avoid having to put an include in the main .ino file, unless you write your own build process (or perhaps use another toolchain). I think you could put SPI.h (and other things, like Wire.h) in the main .ino file, even if they aren't used due to your configuration parameters. The linker ...


5

The Yun's OS (Linino) is based on OpenWRT, and the official toolchain from OpenWRT does not appear to have changed much. It could probably be done, the question is if you'd really want to though. The limited resources (storage, RAM, CPU) means you would most likely not fit all the parts of the toolchain for C/C++ compilation (perhaps with the exception of ...


5

No. The foundation of a Harvard architecture such as AVR is to only allow code that exists within program space to be executed, and EEPROM is not within program space. It is possible, however, to write a virtual machine that will run from flash. This VM can then read program-become-data from anywhere and take action based on it.


5

attiny is missing an option required to allow the Manchester library to function properly on ATtinyX5 devices, specifically definition of __AVR_ATtinyX5__ when a device is selected. In fact, it's missing quite a few things. The package I use for ATtinyX5 support is arduino-tiny. I have verified that it defines that symbol properly. I recommend that you ...


5

C++ doesn't support named parameters, and I doubt it ever will. It has strict calling conventions which mean you couldn't re-order the parameters at call-time without adding an unnecessary layer of data duplication/indirection (which would hurt performance). It would also need a significant change to compiler architecture, as variable/parameter names are ...


5

Update: This seems to be a linker bug. Quick fix: upgrade your Arduino installation. See the end of this answer for details. What follows is the first version of this answer, with a detailed explanation of the error message. You have a problem linking the interrupt vector table with your program. I quite understand what the error message means, but I do not ...


5

As @st2000 said, it's best to start with the first error shown as that error may be causing other errors that will go away once you fix the first one. You should be aware of the difference between warnings and errors. Warnings may or may not be related to the error preventing your code from compiling so I'd just focus on the error at first. So scroll the ...


5

You need to declare your function before you use it. So either the whole function body needs to be before the usage, or you must add a function prototype declaration. void myfunction(); myfunction(); void myfunction(){ return 0; }


4

Having struggled through this myself, I can confirm that Joel's solution works. There are quite a lot of posts around that suggests that you can't get the Manchester to work with Arduino1.0x and you need 0020. But you can. The key is to use the arduino-tiny from the link above, put the tiny folder which you get from there in /hardware and then rename it ...


4

Your question actually has 2 distinct parts: Why does arduino-cmake generate the warning "Policy CMP0022 is not set: INTERFACE_LINK_LIBRARIES..."? Why does make, using the generated MakeFile, fail when building WiFi example? First off, the arduino-cmake "Policy CMP0022" is just a warning, not an error. As per this cmake article, that CMP policy has been ...


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