![]() In this case the numbers of zeros of the divider and module before the if statement corresponds to the half the length of the largest number that fit the uint64 type (in Decimal, then ten zeroes). If you use another int type (32 bit, 16 bit, etc.) as a val type you need to change (reduce) the divider and module number in relation to the above example. 99 length is 2 1294 length is 4 etc.) is odd we need to put the if statement code with the MSBval result and then use it in the serial.Ĭonversely (as showed in the code), if the number is even we need to put the if statement code with the LSBval and then use the result. The biggest problem lies in the length of the decimal number. Serial << (buffer) << F("]") << endl // Data to Serial Monitor Sprintf(buffer, "%08lu", (uint32_t)(val)) // Formatting zeros Sprintf(buffer, "%02u", MLSBval) // Formatting zeros Sprintf(buffer, "%010lu", MLSBval) // Formatting zeros Since it seems to be the only complete solution to solve this kind of problem that I was able to find on the web then, if anyone finds any bugs or like to improve it, feel free to reply to (or edit) this post. If desired, it can be generalized to be used with all types of integers (depends on your Arduino board microcontroller type).įor now is tested only with uint64_t on Arduino Uno R3 (ATmega328P). This solution is not very elegant, but at least works well for my purpose. ![]() For higher numbers the formula should not work correctly, but for some reason (I'm still trying to figure out why) when I send the result on the Serial it returns the right number even with the highest number (255 255 255 255 255 255 255 255 ).Ĭheck the accepted answer for more solutions that do not relies on Streaming.h library. Serial << F("") << endl Īs you can see I have not yet found the right solution. How do I print a 64 bit number in decimal given 32 bit integer I'm using streaming.h for serial communication. So, is there an algorithm that given a number as input, the returned result are two numbers that are the breakdown of the previous binary number in MSBs and LSBs? So when I send it in sequence on the Serial I see a 142, but instead what I would is 226. Thus I try to split the number in two parts with a bitwise mask: 1110 <- MSBs (14 in Decimal) Then send each number in sequence on the Serial port, but obviously the resulting number is different from the original 64-bit number.įor example, I use an unsigned 8-bit number as follows: 11100010 <- 226 in DecimalĪnd I need to send it to my serial port, but it can only handle 4-bit numbers at max. To do this I was thinking of dividing the number in two blocks of 32 bit each with a bitwise operation. Thus, I need to print that (64-bit) number as Decimal on Serial port only. How can I overcome these limitations and print all possible 64 bit unsigned integers as decimal numbers?įor an Arduino project I need to send a number on Serial that fits on a uint64_t variable type, but for some reason the Arduino Serial port can't handle a type bigger than 32 bit (but the device supports a 6-bit type). Serial.The compiler provided for Arduino seems to generate code that can work with 64 bit numbers, but the I/O classes, for instance Serial.Print(), seem to not work beyond 32 bit numbers. #define DHT22_PIN 10 //digital 10, that is Where the first is temp, second is humidity, and the rest are variables. Once the board reads the sensor values, I'll just make up some values for the purpose of demonstration, I want it to print to the serial port the following (note: the DHT-22 outputs an actual value for the temp and humidity so it would be like 15.4256 and I want it rounded to 2 decimal places) Hopefully, you can tell me!īasically, I have 7 integer values read from 7 different analog pins plus a temperature and humidity reading from a DHT-22. As I was working on the code for my latest project this morning, I have run into an obstacle maybe it is really easy to fix and I just missed the solution or maybe it is complicated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |