Hot answers tagged

7

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 those individual strings into integers: char array[] = "10,11,12,1,0,1,0"; int intArray[7]; // or more if you want some extra room? int ipos = 0; // Get the ...


7

Using atol(String.c_str()) looks OK to me. If there was a String.toLong() it would be written that way anyway. In fact, looking at the code for String.toInt() that's exactly what it does: long String::toInt(void) const { if (buffer) return atol(buffer); return 0; } So the answer is: use String.toInt().


7

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.84999847412109375. When you multiply this by 100, you get 16322559 × 2−12 = 3984.999755859375. And than casting to an int or long results in 3984, not 3985. so whenever you ...


6

On an Arduino board with the 328 processor, an INT is stored as a 2 byte value, so the max number is 32,767. You want to use a LONG which is a 4 byte number which has a max of 2,147,483,647. When the variable you are using is not big enough, the number rolls over, which is why you end up getting a negative number.


5

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 strtoul is: endptr Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. ...


3

Assuming you are talking about the function bool painlessMesh::sendBroadcast( String &msg, bool includeSelf = false) Then you'll see that ultimately, the library passes msg into the function buildMeshPackage where it constructs a JSON message (see here) String ICACHE_FLASH_ATTR painlessMesh::buildMeshPackage(uint32_t destId, uint32_t fromId, ...


3

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 the string - length 9, because at most you need 8 chars "-23.5678" , plus null terminator dtostrf(sensors.getTempCByIndex(0), 7, 3, tStr) Note: dtostrf returns ...


3

There are several possible issues here: can you send the number, as a string, from the PC to the ATtiny? can you convert the string to an actual number? can you give this number to the RTC? The phrasing of your question sounds like your issue is with the conversion. However that is easily handled with atol(). Here is a test program I tried on my Uno: void ...


3

He is describing a boost converter which steps up voltage at the expense of current. To quote from the Wikipedia page on that subject: A boost converter (step-up converter) is a DC-to-DC power converter that steps up voltage (while stepping down current) from its input (supply) to its output (load). It is a class of switched-mode power supply (SMPS) ...


3

Print the integer part, print a comma, then print the value minus the integer part. float value = 123.45; int ival = (int)value; int frac = (value - ival) * 100; Serial.print(ival); Serial.print(","); if (frac < 10) Serial.print("0"); Serial.println(frac);


3

C strings need to be NUL-terminated. char strcode[5]; ... ... ... strcode[4] = '\0'; ...


3

OK, so after the lengthy requests for clarifications, it appears that you are receiving a number in ASCII decimal ("12") and want to parse it into an integer. You may use the parseInt() method of the serial object, like: int c = SerialBT.parseInt(); This can however make you program quite slow, as parseInt() uses a timeout to know when to stop ...


2

avr-libc lacks strtoull() that I would normally use for such a conversion. You will have to do it manually. Fortunately it's quite simple - just start with y=0 and, for each character in the string, multiply y by 10 then add the numeric value of the character in the string: String x = "68976543210"; unsigned long long y = 0; for (int i = 0; i < x....


2

If you want help with optimisation then "Code Review" would be the better forum. Its very difficult to optimise code that's hard to read. You have used an inconsistent formatting style (which might be because of the post but your failure to fix it indicates a lack of interest in the answers. You passed all you function arguments by value, you should pass ...


2

May be overkill for this simple usage but, if you are printing numbers in more than one place in your program, it may be convenient to implement a filter that replaces all dots with commas. Here is one such filter: // Replace dots with commas. class CommaForDot : public Print { public: CommaForDot(Print &downstream) : downstream(downstream) {} ...


2

If you change str += String(buffer[i]); to str += (char)buffer[i];, it will print the ascii characters to the serial monitor. The following sketch can be used to see the difference in binary compiled size using these two methods of "adding to the string". // 4026 to 3758 bytes. #define SIZE_BUFFER 18 #define MAX_SIZE_BLOCK 16 byte buffer[SIZE_BUFFER] ...


2

byte and char are the same. If you set 0 as string terminator after last character in the buffer, you get a zero terminated string. If you really must use String, you can create an instance with a constructor that takes zero terminated string. buffer[tam] = 0; String str((char*) buffer); Let in the buffer a place for the zero. (char*) is cast that says ...


2

I2C and RS485 are such vastly different protocols with such vastly different signalling needs that they cannot share the same cables. Further, I2C is not designed for connecting with long cables - it's meant for use between chips on a PCB. The simplest "Arduino" solution would be to create a new RS485 device on your RS485 bus using another MAX485 and a ...


2

The IPAddress class has a member function bool fromString(const char *address).


2

Please see the comments in the code: // SDMacAdress is zero terminated // (it should be because you use strcp not strncp) // char arr[12]; you need one extra byte for the zero char arr[13]; strcpy(arr, SDMacAddress); auto getNum = [](char c) { // you forgot upercase HEX letters (A-F) // don't use sophisticated code // the best programmers produce ...


2

If you want 4 decimal places, you may try something like this: gps_string = String(gps.location.lat(), 4) + " x " + String(gps.location.lng(), 4);


2

After parsing the time into numbers, you can use the standard C time library time.h. More specifically, use the mktime function to convert a struct tm into a time_t, and use difftime to get the seconds since the start of the Unix expoch: #include <time.h> void loop() { struct tm ltm = {0}; ltm.tm_year = 2019 - 1900; ltm.tm_mon = 11 - 1; ...


2

Assuming little-endian bools (least significant bool first), you should use simple bit-shifting, not floating point power calculations: for(int id=start_index;id<=stop_index;id++){ result |= b_array[id] ? (1 << run) : 0; run++; } The ternary operation (b_array[id] ? (1 << run) : 0) means: If b_array[id] is true, then Or result ...


2

With some inspiration from timemage's comment, I decided in the long run to implement my own power function that takes two int arguments and returns an int. Rather than use the pow function in the <math.h> library which returns a double when I only need an int. int power(int x, int y) { return y == 0 ? 1 : x * power(x, y - 1); } Now the program ...


1

Epoch are seconds from 1970-01-01. Get it from JSON as unsigned long. They are a number in the returned JSON (no ") from https://openweathermap.org/ doc: "sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049}, to convert it to string with TimeLib.h: #include <TimeLib.h> #include <ArduinoJson.h> void setup() { Serial.begin(115200)...


1

C and C++ programs get compiled in multiple passes through various programs/steps. One of the first is the C Preprocessor, which handles the #define preprocessor directive. It's basically a big find/replace function. After the preprocessor is done, anywhere you had D8 outside of a string literal, would have the value 13. It's not a variable or a string -- ...


1

Answering this question based on the help from above. It works with this code. Thank you all for the help. The clock does not reply the newly set time instantly, but after a few seconds. #include <DS3232RTC.h> #include <DigiUSB.h> char foo[10]; void setup() { RTC.begin(); DigiUSB.begin(); } void loop() { if (DigiUSB....


1

According to the datasheet the LM317 has a minimum Vin to Vout of 3v so you'd only be able to get 2v out of it if you put 5v in. I'd recommend using either an AMS1117 3.3 or one of the cheap modules based on that chip that has an LED and capacitors on board. They definitely deliver 3.3v from 5v in.


1

You can get 5, 4 channel bi-directional logic level converters for less than $1 USD Logic Level Module Not only will they "translate" 3.3V to 5V and 5V to 3.3V, but they also work with higher voltages as well. For example, a vehicle with a 12V electrical system to 5V and vice versa works too.


1

Your problem is using pow. It works in floats. It may come as some surprise, but pow(2,3) gives 7 when you put it into an int. Don't waste time on pow for powers of 2. Just use a bitshift. It's way smaller and faster.


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