19 votes
Accepted

How to retrieve the data type of a variable?

In a typical C++ program, you would use the typeid operator, like this: std::cout << typeid(myVar).name(); However, that requires a compiler feature called Runtime Type Information (RTTI). It'...
user avatar
16 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 ...
user avatar
  • 169
8 votes
Accepted

arduino - millis()

Whenever you write an equation in C/C++, the data types being operated on have a very real effect on the equation's output. Each type like int, float, and unsigned long have different behaviors, and ...
user avatar
  • 4,309
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 ...
user avatar
  • 3,077
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....
user avatar
6 votes

How can I convert Arduino String to C string type?

Try unsigned char z[100]; IP.getBytes(z, 100); z[IP.length()] = 0; int n = atoi(z); To retrieve bytes inside the string. This assumes IP string's length is shorter than 100.
user avatar
  • 163
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 ...
user avatar
  • 1,475
5 votes
Accepted

square root of large number

You need 2 typecasts. So atan(-ax/sqrt((float)ay*ay+(float)az*az)); Otherwise it will use int16_t for the right side of the addition and overflow. Though for speed you might want to typecast them ...
user avatar
  • 11.1k
5 votes
Accepted

Cast from long to int

Yes, it just blindly chops off the upper 16 bits. That means that the sign can change between the two. For instance, 123456789 gets truncated to -13035 and -123456789 truncates to 13035. To see ...
user avatar
  • 102k
5 votes
Accepted

Multiplication, division. What is wrong?

300 and 1000 are integers. What long V2 = (300 * 1000)/30000; really does is take the integers 300 and 1000 multiply them. This calculation overflows, and equals -27680. Dividing that by 30000 ...
user avatar
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 ...
user avatar
5 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 ...
user avatar
  • 36.2k
4 votes

Serial.print() is slower than it should be

I need a sample rate of 6 kHz OK, so that means you need to sample and send every 1/6000 seconds (167 µs). I have a lengthy discussion about the ADC hardware. Amongst other things is a table of ...
user avatar
  • 35.1k
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 ...
user avatar
  • 36.2k
4 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 ...
user avatar
  • 4,137
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, ...
user avatar
  • 102k
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 ...
user avatar
  • 7,821
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....
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
user avatar
  • 16.2k
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);
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* ...
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 ...
user avatar
  • 1,125
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 ...
user avatar
  • 102k
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 ...
user avatar
  • 36.2k
3 votes
Accepted

Can I have help interperting this code? char variable somehow storing big numbers

It's passed as a char*. This can be derived from either a string literal (e.g. "212555121") or from a String via its c_str() method.
user avatar
3 votes
Accepted

Assemble or typecast byte array to float

I tried the code below and it worked for me, don't miss the delay() part: Sender Code: void setup() { Serial.begin(9600); } void loop() { float yaw = 10.5; float pitch = 20.15; float ...
user avatar
  • 640
3 votes
Accepted

Convert uint8_t array to uint32_t array

@mpflaga's answer gives a great alternative, but I thought I'd explain why your code works (grossly simplified). All the reinterpret cast does is let the compiler know how wide of a variable it ...
user avatar
  • 186
3 votes

Convert uint8_t array to uint32_t array

I prefer to use unions. example: union fourbyte { uint32_t dword; uint16_t word[2]; uint8_t byte[4]; }; union fourbyte val; val.dword = 0x03020100; Serial.println(val.byte[0], HEX); Serial....
user avatar
  • 2,443
3 votes
Accepted

How to convert byte array to float

You can choose to treat the byte array as a float array by typecasting. Your attempt isn't far from a solution: ((float *)data.data)[0] = lon; // uses data.data[0] ... data.data[3] ((float *)data....
user avatar
  • 1,343

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