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

How to retrieve the data type of a variable?

I use a simple stupid approach... // serial print variable type void types(String a) { Serial.println("it's a String"); } void types(int a) { Serial.println("it's an int"); } void ...
SnakeNET's user avatar
  • 209
7 votes

Convert to and from Unix Timestamp

I think that the two most used libraries are the Adafruit RTClib and the pjrc.com TimeLib. They both have functions to convert the epoch, and they both are reliable. But both lack the timezone and the ...
Jot's user avatar
  • 3,246
7 votes

cast double to long leads to unexpected results

The problem is that float numbers are seldom an exact representation. So the float is stored as the closest number that is exactly representable as a float, namely 10446438 × 2−18 = 39....
Michel Keijzers's user avatar
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
Accepted

Own type definition using a struct does not name a type

This is a bug in the Arduino IDE: when converting your .ino file to a .cpp file, it added a declaration for the function ledFactory() before the declaration of the type led_t. As it appears to be ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

String to unsigned long conversion

Arduino String class provides method c_str(). So you don't have to convert it to C string, as it's already stored as a C string internally. And as mentioned in comments, the second parameter of ...
KIIV's user avatar
  • 4,752
5 votes

Set and extract individual bytes of a number (lowByte() and highByte())

Any more sophisticated byte exchange can be done with binary operators. Extracting information You'll need some constants like MASK SHIFT 0xFF000000 24 ...
Thomas Weller's user avatar
5 votes
Accepted

Read one byte from file and convert to decimal

Ok, I got it working now, here's what was happening: Turns out the file wasn't being opened. Why? seems like there's a limitation on the filename length, I noticed this after adding the lines: root....
melev's user avatar
  • 71
4 votes
Accepted

inttypes vs Arduino defined integral types

"Arduino": char, int, long; byte, word, unsigned long inttypes: int8_t, int16_t, int32_t; uint8_t, uint16_t, uint32_t A better classification would be: C and C++: char, int, long, unsigned long ...
Edgar Bonet's user avatar
  • 43.3k
4 votes
Accepted

Why char 0x80 == 0xFFFFFF80?

To illustrate the reasons better let's look at the actual content of the data. 0x80 is 0b10000000 in binary. A char is a signed variable, which means that the most significant bit is the sign bit, ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Const data optimisation for Arduino

This question is not really about the Arduino. It is about GNU C/C++ compiler optimizations. By adding the keyword const the compiler can do a lot of optimization. One is that the value does not ...
Mikael Patel's user avatar
  • 7,969
4 votes
Accepted

Comparing custom variable type definitions

In response to your answer: why don't you think it is convenient? As you already noticed, you can use the enum values directly in your sketch. Anyway, it is better to use a switch statement: Serial....
Michel Keijzers's user avatar
4 votes
Accepted

Problem in defining a variable as multiplication

unsigned long onPeriod = 60ul * 1000; ul makes 60 to unsigned long and then the calculation has place for result. without ul the constants and the result are int and it overflows
Juraj's user avatar
  • 18k
4 votes
Accepted

Print unsigned long long in ESP8266

Okay, I figured out... this two prints the right number: sprintf(dt,"%llu", t); snprintf(dt, 18, "%llu", t);
Yván Ecarri's user avatar
4 votes
Accepted

Change data-type String

irsend.sendNEC(strtoul(irrequest.c_str(), NULL, 10)); did the trick with the help of Juraj. Much thanks! I only had the set 10 to 16 to get it to work. Working code: // Configuration const char* ...
Rick's user avatar
  • 61
4 votes
Accepted

Can I use 1-byte datatypes to store digital input?

You receive the data as int, but you store it as byte. For example, when you call the serial library function to receive a data byte, the library will return either the data or a -1 if a problem has ...
jsotola's user avatar
  • 1,515
4 votes
Accepted

Why use \" in arduino code?

Why there are (escapes) " ---" used in the code? Why can't we use the string normally? Because escapes are required. In C " means 'The start or end of a string', but \" means 'A ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Conditional compilation depending on sizeof(double)

The double data type is almost universally 8-bytes long, so you could simply #if !__AVR__ # define HAS_64_BIT_DOUBLE #endif Gcc provides a more specific macro though: __SIZEOF_DOUBLE__, which has the ...
Edgar Bonet's user avatar
  • 43.3k
4 votes
Accepted

Arduino data-types

There are two issues here. One is that the Arduino core uses Serial to handle serial I/O, rather than the traditional libc interface (printf(), puts() and co.). The avr-libc does provide the ...
Edgar Bonet's user avatar
  • 43.3k
4 votes

String() vs char for simple flow control

Using Serial.readString() will make your program very slow. This is because readString() does not know when to stop reading. When the serial input buffer gets empty, it will wait just in case more ...
Edgar Bonet's user avatar
  • 43.3k
4 votes
Accepted

How to send multiple bytes with Wire without copying

No, this function overwrite of Wire.write() is meant for buffers, meaning connected spaces in memory, which can be covered by simply incrementing the buffer. That is not the case for distinct ...
chrisl's user avatar
  • 16.3k
3 votes

How to retrieve the data type of a variable?

Building on @snakeNET's answer (which I regard as function overload rather than polymorphism)... A more generic way would be to pass in a pointer to Print, allowing any Print related class to be used (...
brewmanz's user avatar
  • 155
3 votes
Accepted

Understanding SevSeg

I don't understand the purpose of the B0 at the beginning of each byte. That's just Arduino's rather weird way of doing binary constants. For example, in the file binary.h are lines and lines of ...
Nick Gammon's user avatar
  • 38.3k
3 votes
Accepted

How does an 8bit MCU make use of data-types over 8bits?

The basic answer is that the processor does math in 8-bit chunks, so it takes more instructions than it would on a 32-bit processor. A good answer is found in EE.SE
jose can u c's user avatar
  • 6,974
3 votes
Accepted

DS18B20 sending variable via Enthernet shield

Having re-read the question just before posting this answer, I find it odd that the DHT temp and humidity (also floats) work for you - how odd Use dtostrf char tStr[9] = { 0 }; // buffer to store ...
Jaromanda X's user avatar
3 votes
Accepted

How to map 6 bytes of raw data to long long type?

The trick is to use a union data type and get full control of the mapping. The issue of data representation and endian is now under your control: uint64_t foo(uint8_t* A, uint8_t* B, uint8_t* C) { ...
Mikael Patel's user avatar
  • 7,969
3 votes

Is the strange problem I'm having to do with comparing floats?

You already know, from previous answers, why the comparison doesn't give you the result you expect: you assumed that any multiple of 0.1 is also a multiple of 0.001, which is true for real numbers but ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

Why does Arduino IDE recommend storing 0-255 numbers in "byte" instead of "char"?

Is byte datatype a solely Arduino construct? Yes. Much of the Arduino "language" is "bent" to look like Java, since it originated from Processing, which is Java. So we have data types the "look like" ...
Majenko's user avatar
  • 105k
3 votes
Accepted

Int64_t aka long long acts as int32_t and overflows at 2^31

1 << 31 is interpreted by the compiler as (int)1 << (int)32. See the implicit types of integer literals. The rules of usual arithmetic conversions state that no implicit conversion is ...
Edgar Bonet's user avatar
  • 43.3k
3 votes

Construct Strings with UTF-8 characters from data

As you have noticed, Serial doesn't know how to deal with wchar_t. If you are building your strings algorithmically from Unicode code points, you need to convert those code points to UTF-8 for ...
Edgar Bonet's user avatar
  • 43.3k

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