Skip to main content
Share Your Experience: Take the 2024 Developer Survey
25 votes
Accepted

How do I convert a float into char*?

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 ...
John Burger's user avatar
  • 1,875
25 votes

Using String instead of C string, yet another attempt to touch a loaded issue

It's not that String itself is evil, it's more that it's very easy to abuse it and cause heap fragmentation. Used judiciously it's fine and a useful tool. Used in "bad" ways and you open the ...
Majenko's user avatar
  • 105k
21 votes

How do I extract code from an arduino?

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 ...
Duncan C's user avatar
  • 5,702
12 votes

How do I extract code from an arduino?

You never can get the actual code back ... by reading the flash you might be able to get the executable/runtime code, but this doesn't look at all similar as your code, although it's functionally the ...
Michel Keijzers's user avatar
11 votes
Accepted

how fast does loop() run in Arduino

Loop runs as long as loop needs to run. Instructions in a CPU run sequentially. The more instructions there are the longer it takes to run. The more code you put in loop the longer loop will run. ...
Majenko's user avatar
  • 105k
11 votes

How do I directly access a memory mapped register of AVR with C

It's worth exploring how PORTD is defined in the actual AVR header file. The relevant file is iom328p.h, and it's defined as: #define PORTD _SFR_IO8(0x0B) _SFR_IO8 is defined as #define _SFR_IO8(...
ajb's user avatar
  • 211
9 votes

How do I extract code from an arduino?

You read the contents of flash and save it to a file. It can then be re-flashed if your need is not to edit, but duplicate. This reads all flash, including the bootloader, and also PROGMEM data. Use ...
mckenzm's user avatar
  • 191
8 votes
Accepted

Most memory efficient way to program

That is an incredibly hard question to give a definitive answer to. In general using OO consumes more memory, but that is the price you pay for greater flexibility and ease of use. The difference is ...
Majenko's user avatar
  • 105k
8 votes

Binary message formatter

Passing messages across computers and/or microcontrollers with different architectures can be a real pain (endianness...). So do not fall into NIH / reinventing the wheel traps but keep standing on ...
scls's user avatar
  • 221
8 votes
Accepted

Arduino port relocation (PORTD to PORTB)

You cannot set pin states in multiple ports simultaneously, if that's what you're asking. At the instruction set level, setting pin states is a single that writes to an 8-bit register which ...
uint128_t's user avatar
  • 819
8 votes
Accepted

Print with round towards zero instead of round to nearest

A cheap trick to round a number to one decimal place (in "chop-off mode") is to multiply it by 10, convert it to an integer, and divide by 10.0f again: float myVal = 123.89f; myVal = (long)(myVal * ...
Maximilian Gerhardt's user avatar
8 votes
Accepted

How to convert byte *payload to String

Depending on what is setting the message on the topic, payload may, or may not be, a NULL-terminated string. This is why they provide the length parameter. You could consider comparing the data ...
Majenko's user avatar
  • 105k
8 votes

Using String instead of C string, yet another attempt to touch a loaded issue

The short answer is that fragmentation crashes are infrequent because they require very specific conditions to arise—and that's the problem. Imagine you have a crash that occurs once every few days ...
Artelius's user avatar
  • 251
7 votes
Accepted

How to convert array of chars to array of ints?

If you have a C string containing ASCII numbers with a common delimiter (comma in this case) you can use the strtok() function to split it into individual strings. Then you can use atoi() to convert ...
Majenko's user avatar
  • 105k
6 votes

max7219 chained display showing mirrored text

Since version 3.0 MD_MAX72xx.h no longer supports that definition. Now the hardware type is defined as a parameter on creation (in the "Example" file you are using - not in MD_MAX72xx.h) #define ...
Den1al's user avatar
  • 161
6 votes

How can I concatenate multiple byte Arrays into one Array

You can concatenate with memcpy. You just need to set the pointer at the right place inside the c1 array. memcpy(c1, a1, sizeof(a1)); memcpy(c1+sizeof(a1), a2, sizeof(a2)); memcpy(c1+sizeof(a1)+...
gre_gor's user avatar
  • 1,682
6 votes
Accepted

Max length of char arrays?

The main issue with your code is: sizeof(s) That doesn't return the string length - it returns the size of the char pointer - which is two bytes (on an 8-bit system, 4 on a 32-bit system). Instead ...
Majenko's user avatar
  • 105k
6 votes
Accepted

How to compare a string

version using String (not recommended, but it makes simpler to understand the following C-string version) #define LED 2 const char* a = "abcd"; void setup() { Serial.begin(115200); pinMode(LED, ...
Juraj's user avatar
  • 18k
6 votes
Accepted

Arduino wrap or subclass print() to work with multiple Serial

You can create a class derived from Print that forwards its output to either or both Serial and Serial1. The only method you need to implement for this to work is write(uint8_t): class DualPrint : ...
Edgar Bonet's user avatar
  • 43.3k
6 votes

arithmetic operation outcome differs from the expected outcome

Once again an example to Why should I learn C/C++ first before learning Arduino. The main point here is: know your data types. Your calculation millisec*frequency/1000 would work if it was a compile ...
Kwasmich's user avatar
  • 1,513
6 votes

How can I set up outputs without using digitalWrite?

digitalWrite() is just a convenience function to hide away complexity from the user, so that she/he can just use a pin number and a value. This complexity takes some time to execute. If this is too ...
chrisl's user avatar
  • 16.3k
6 votes
Accepted

Which Arduinos support ATOMIC_BLOCK?

This answer covers: Which Arduinos support ATOMIC_BLOCK? And how can I duplicate this concept in C with __attribute__((__cleanup__(func_to_call_when_x_exits_scope))) and in C++ with class constructors ...
Gabriel Staples's user avatar
6 votes

How to upload code without closing puTTY

There is an open source project that will do what you want. http://com0com.sourceforge.net/ The com0com will create virtual com ports for the programs to connect to. The hub4com will allow you to ...
jsotola's user avatar
  • 1,515
6 votes
Accepted

How to write nonblocking code, for polling sensor at 100 Hz

should I be declaring last_ms as a global variable instead of static? Local static is better, as it limits the scope of the variable to just where it is needed. Consider making it global only if the ...
Edgar Bonet's user avatar
  • 43.3k
6 votes
Accepted

How arduino PORTB pins are addressed if I want to access them with pointer?

The address of PORTB can be found in the ATmega328P datasheet, section “I/O-Ports”, subsection “Register Description”. It is repeated near the end of the section “Register Summary”, where it is ...
Edgar Bonet's user avatar
  • 43.3k
6 votes
Accepted

lcd.createChar() only allows for 8 custom characters

These displays only allow up to 8 custom defined characters. The hardware just only has the character RAM for at most 8 characters. They will be mapped to char codes 0 to 7 and 8 to 15. The character ...
PMF's user avatar
  • 1,296
5 votes

Peer to peer communication

Peer to Peer Example Written by Stewart Meyer (engineer retired from the FAA). This is first time that I have given an answer, hope you can read this: Not only can the ESP8266 connect to a WiFi ...
Stewart Meyer's user avatar
5 votes

Basic makefile for avr-gcc

The accepted answer is great as it has given me a valuable lesson in all kinds of debugging tools (avr-objdump -D has become a close friend). Namely, the line: ${OBJCOPY} -O ihex -R .eeprom $< $@ ...
Robert Špendl's user avatar
5 votes
Accepted

Arduino's digitalWrite, precautions for direct port manipulation?

I figure they don't want an interrupt to possibly change any of the bits in the output register while it executes *out = *out & ~bit? Correct. That is known as a critical section. And what ...
Majenko's user avatar
  • 105k
5 votes
Accepted

AVR (Arduino Uno) Serial.print and Serial.println only printing 1 or 2 chars

You have two issues here: The first is that you are using the Arduino core library and failed to initialize it. This is done automatically if you rely on the core's supplied main(), i.e. you stick ...
Edgar Bonet's user avatar
  • 43.3k

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