Hot answers tagged

29 votes
Accepted

deprecated conversion from string constant to 'char*'

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 ...
user avatar
  • 101k
24 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 ...
user avatar
  • 101k
22 votes

Arduino interruption (on pin change)

As a sort of prologue to this overly long answer... This question got me deeply captivated with the problem of interrupt latency, to the point of loosing sleep in counting cycles instead of sheep. I ...
user avatar
  • 35.5k
22 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 ...
user avatar
  • 1,795
20 votes
Accepted

Arduino: How to get the board type in code

Thanks to the help of Charlie Hanson and jantje I found the universal solution! Because it is a pity that every Arduino programmer has to go through the same pain to figure out how to get the board ...
user avatar
  • 461
20 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 ...
user avatar
  • 5,499
11 votes
Accepted

How to call C functions from Arduino sketch?

You can extern "C" #include like the following: extern "C"{ #include "crc16.h" }; void setup(){ } void loop(){ CalculateCRC16("<09M", 4); } And the crc16.h file could be (some minor fixes, ...
user avatar
  • 1,364
11 votes
Accepted

Is Arduino IDE enough to program with C or C++?

In terms of the core language features, there's virtually no difference. In the background, the Arduino IDE and Atmel/AVR Studio both use GCC to compile the code. GCC is an excellent toolset which ...
user avatar
11 votes
Accepted

Why doesn't my code work in a function, yet works inline?

The problem is that you are passing the value of the ports to the function, not the address of the ports. You need to either pass "by reference" or as a pointer, so that modifying the port variable ...
user avatar
  • 101k
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(...
user avatar
  • 211
11 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 ...
user avatar
10 votes
Accepted

Number of elements in an array char

You want strlen. char foo [100] = "bar"; Serial.println (strlen (foo)); // --> prints 3
user avatar
  • 35k
10 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. ...
user avatar
  • 101k
9 votes

Arduino if else code

Replace the line: if (A0 == HIGH) { by this one: if (digitalRead(A0) == HIGH) { If you plan on using the pin back as an analog input, use something like this: if (analogRead(A0) > 512) { ...
user avatar
  • 3,257
9 votes
Accepted

ATtiny85 minimum setup to blink led

This schematic shows a complete connection of a blinking led. Red cable = +5V (pin 8) White cable = Ground (pin 4) Resistor = 1 end at pin 5 (IO 0) and the other in the LED anode (+) Led anode (+) in ...
user avatar
  • 269
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 ...
user avatar
  • 191
8 votes
Accepted

Chain of, or one big if sentence

Putting them all in a single condition like that is absolutely fine. C/C++ uses "short circuit evaluation" which means it will stop as soon as it's determined the outcome. If everything is combined ...
user avatar
8 votes
Accepted

How to read code from Arduino Uno to Arduino IDE?

Can we read and get C codes from compiled codes from Arduino hardwares? While it is possible, even trivial, to disassemble machine code, it is very difficult to convert the assembly code into a ...
user avatar
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 ...
user avatar
  • 101k
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 ...
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 ...
user avatar
  • 808
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 * ...
user avatar
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 ...
user avatar
  • 251
7 votes
Accepted

How to prevent Arduino compiler from merging changing of port pin outputs into a single operation

I certainly can't duplicate what you are saying. The small code snippet: void setup() { PORTD |= _BV(0); PORTD |= _BV(1); PORTD &= ~(_BV(1)); } void loop() { } compiles into: ...
user avatar
  • 101k
7 votes
Accepted

Where can I see the output of printf?

If you're working with the standard Arduino environment and libraries then printf() won't display anything anywhere. The reason is that microcontrollers could be connected to any number of different ...
user avatar
7 votes
Accepted

SAM3X8E (Arduino Due) Pin IO registers

If you have a read of section 31 of the Datasheet, available from here, things may come a little clearer for you. Here's a summary of what I know: PIO stands for Parallel Input/Output and offers the ...
user avatar
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 ...
user avatar
  • 101k
7 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 ...
user avatar
  • 101k
6 votes

Arduino server to read from multiple clients

Without knowing your exact implementation details... You should be OK as long as you don't have multiple clients connected at the same time. You may think that there are multiple clients connected to ...
user avatar
6 votes

ATtiny85 minimum setup to blink led

Ignacio Vazquez-Abrams has already outlined the schematics. There must be a C program compiled and uploaded to it, just like an Arduino board would have. There are however some differences ...
user avatar

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