We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

30

Optimisation Low-level programming for embedded systems is quite different from programming for general purpose devices, such as computers and cell phones. Efficiency (in terms of speed and space) is far more important because resources are at a premium. That means the very first thing to do if you run out of space is to look at what parts of your code you ...


24

When you upload your code to your Arduino, say an Uno for example, it'll tell you how many bytes it uses up out of the 32K available. That's how much flash memory you have (think computer hard disk). While your program is running, it's using whats called SRAM, and there is much less of that available. Sometimes you'll notice your program behaving oddly at a ...


17

In addition to what others have said (on which I fully agree), I would advise to read this adafruit article about memory; it's well written, explains a lot of things about memory and provide hints on how to optimize it. At the end of the read, I think you would get a quite complete answer to your question. To sum it up, you have 2 possible optimization ...


15

When booted, the Arduino will run whatever program was put on it last. Solution: Don't turn it on if you don't want it to run. When out of the box, the Arduino usually runs the Blink program. So you can just load that and call it a day if you want to "reset" it. Or have a loop program with a sleep command. If you've been messing with the USB bootloader ...


10

AVR is a modified Harvard architecture family, so code is stored in flash only, whereas data exists primarily in RAM when being manipulated. With that in mind, let's address your questions. Why on earth do I have to copy the damn content to RAM before accessing it? You don't need to per se, but by default code assumes that data is in RAM unless the code ...


9

There are two things to do if you run out of storage: Somehow "optimize" your code so it needs less storage; or at least uses less of the particular kind of storage that you ran out of (and uses more of the kind of storage that you still have plenty of). Or, Add more storage. There are lots of tips online on how to do the first (and for the vast majority ...


9

If safety is a major concern, then you should almost always load a stop* sketch onto the board before calling it a day. This would be a good idea, for example, when your board is semi-permanently a part of a larger system. You could do so by: Upload the Bare Minimum sketch found in File -> Examples -> Basic in the arduino-ide Upload a sketch with a large ...


8

This is how Print::print prints from program memory in the Arduino library: size_t Print::print(const __FlashStringHelper *ifsh) { const char PROGMEM *p = (const char PROGMEM *)ifsh; size_t n = 0; while (1) { unsigned char c = pgm_read_byte(p++); if (c == 0) break; n += write(c); } return n; } __FlashStringHelper* is an empty class ...


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

You are saving a String object in EEPROM, which is useless. A string object does not store the contents of your string. Instead, it just stores: the memory address where the actual contents (the characters) is stored the amount of memory allocated for this contents the number of characters that actually make the string This is what you are storing to, and ...


6

The microcontroller is executing one command at a time from..where? Flash memory directly. The AVR core can only execute instructions from Flash memory. It uses a two-bus system known as Harvard Architecture, where the Instruction Bus is connected to Flash and is used to execute instructions, and the Data Bus is connected to SRAM, EEPROM, all the ...


5

After further digging, this appears to be from Mikal Hart's Flash library. The reason this library was developed is: Storing static program data in flash/PROGMEM is a tricky part of Arduino programming. To save precious RAM, a novice user already at odds with unfamiliar C++ syntax must digest such daunting concepts as prog_char, PSTR(), PROGMEM, ...


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

An easy way of accessing any type of data in PROGMEM is to use this small library: PROGMEM_readAnything.h #include <Arduino.h> // for type definitions template <typename T> void PROGMEM_readAnything (const T * sce, T& dest) { memcpy_P (&dest, sce, sizeof (T)); } template <typename T> T PROGMEM_getAnything (const T * sce) ...


5

Paul covered the most important points. This answer is intended to be a complement to his. First, you should keep in mind the general rules, which are more important than the more specific ones. The general rule for saving RAM is: be aware of what you are storing, and do not store anything you do not really need. The general rule for saving flash is: be ...


4

I researched this once when I was getting consistent, exactly 85 degC readings from my sensors. The Maxim DS18b20 data sheet (rev 042208, page 4, in a footnote to Table 1) says: "The power-on reset value of the temperature register is +85°C. Double check that the Arduino code is follows the sensor communication protocol correctly.


4

The flash array is from Mikal Hart's flash library as you show, and what it does is that they declare an array or a matrix (table) object in the PROGMEM memory: #define FLASH_ARRAY(type, name, values...) \ static const type name##_flash[] PROGMEM = { values }; \ _FLASH_ARRAY<type> name(name##_flash, sizeof(name##_flash) / sizeof(type)); which is an ...


4

There is a helper macro in avr-libc that is designed just for this purpose: #define pgm_read_float_near(address_short) __LPM_float((uint16_t)(address_short)) Read a float from the program space with a 16-bit (near) address. Note The address is a byte address. The address is in the program space. Use it like: PROGMEM float pi=3.141592653; // ...


4

This application already exists. It's called AVRDUDE: http://www.nongnu.org/avrdude/ You can download it compiled for Windows at the link above. This is the very flashing tool that the Arduino IDE uses for the AVR boards such as Nano. You can get the AVRDUDE command the Arduino IDE uses to upload to the Nano by following these steps: Start the Arduino IDE ...


4

Yes you can. This Arduino IDE add on lets you make calls to code store in the bootload of memory so you can make changes to the flash memory. https://majek.sh/en/writing-to-internal-flash-on-arduino/ The number of times you rewrite a cell are limited. I'd suggest you are better off with adding a FRAM memory chip. Get a 5V part with SPI interface. Fast reads,...


3

The Due (more specifically the SAM3X) uses its own proprietary protocol. It's all embedded in the bootloader, which is hard wired in the ROM and can never be changed (idiotic if you ask me). I know of two systems for communicating with it. Atmel's SAM-BA program, and the open source BOSSA. The command line version of BOSSA (bossac) is used by the Arduino ...


3

There should be no difference between turning on your computer and plugging in a USB cable. The embedded devices typically plugged into a USB ports such as mice, keyboards, wireless adapters, hard drives, etc don't have their FLASH memory corrupted when the computer is turned on so it is unlikely that your Arduino would. To confirm there isn't something ...


3

If I understand your question correctly, you need to integrate this data into a .ino script. Probably as a byte array. Then the script can read the array and Serial.write its content. You could write a small tool in Python or else on your computer, that takes the text file and converts it into a .h header. Include that into your .ino script. Or use your ...


3

I don't think you need a file system: that is usually needed when you want to manage many files. Your weather data log sounds like a single file. What you seem to need is to access EEPROM as a stream, exactly like you would for an open file. There are stream wrappers around some EEPROM libraries, e.g. this one for ESP. I suggest you check it out and see if ...


3

You do not need the level shifter. The input pins of the ESP8266 contain over-voltage (though not over-current) protection. All you need to do is limit the current that can flow through the over-voltage protection circuit, and all that needs is a series resistor in the region of 100Ω or so. And the Arduino already has that protection between the ATMega16U2 ...


3

The answer is "yes, you can use extra AVR Flash memory as non-volatile EEPROM-like Flash memory storage, very similar to an STM32 microcontroller." One of these days I'm going to write an Arduino library to make this really easy for the beginner. For now, however, I'm going to simply post links to valuable references that contain the full answers and that I ...


3

D3 (GPIO0) and D4 (GPIO2) both need to be high on startup for the user program to be executed. The relay board connected to those pins are preventing a sufficient high on those pins. If you need to use those pins then try using a stronger pull-up resistor on those pins. Try 330-470 ohms. Either that or you will need to add additional circuitry between ...


3

It can be done. Your Digispark has SPI pins, which are ISP pins. For the esp8266 there is an ESP8266AVRISP library for OTA upload to AVR. You can use the example of the library. You must wire the SPI pins and ground together. Wire reset pin of Digispark to io 5 (D1) of NodeMcu. You should use level conversion, but esp8266 tolerates 5 V TTL (not recommended ...


3

I've tried this code with arduino and it worked (i taught, of course, i can't add the "-Wl,--section-start=.app_start=0xFA0" flag to the compilation process so the function not gonna be moved to other section) So i set the arudino IDE to show every command from the compilation to the end of the upload process. The problem lies in the usage of avr-objdump, ...


3

Just to have the full working code, I ended up with this: #include <EEPROM.h> char serial_characters[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}; // 7 chars for serial and one for string end '\0' char serial_number[8]; void setup() { Serial.begin(115200); EEPROM.get(0, serial_number); // check found format if (serial_number[0] == 'S' &...


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