We’re rewarding the question askers & reputations are being recalculated! Read more.

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 ...


5

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().


4

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

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 ...


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

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


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

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);


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

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 ...


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 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

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

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; ...


1

If you have a fixed format you can use strtok() to slice the string into individual segments, then convert those segments to integers. They can then be used with TimeLib.h with the setTime() function. You can then query TimeLib for the unix timestamp with now(). Edit: using PaulStoffregen's TimeLib, this code sets current time and returns current unix ...


1

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);


1

There is a subtle distinction between an array of characters and a C string. All C strings are arrays of characters, but not all arrays of characters are C strings. A C string is an array of characters terminated with a NULL. Therefore C strings cannot contain NULL (zero value) characters. There are a whole set of C library functions that operate on C ...


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

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

No, there is no way of connecting an Arduino to a SATA port. You require USB or UART. SATA is a specific dedicated port, whereas USB is, as the name suggests, Universal. So USB can become SATA but SATA can never become USB.


1

You can use something along the lines of this: simulate this circuit – Schematic created using CircuitLab When the 5V Rx is driven to 5V D1 protects the 3.3V Tx pin. When the 5V Rx is driven to 0V R1 protects the 3.3V Tx pin from excessive current draw. When the 5V Tx is driven to 5V D2 protects the 3.3V Rx pin and R2 pulls the pin to 3.3V. When the ...


1

Buy some of these, it's a simple hardware solution to your problem then. https://www.amazon.co.uk/gp/product/B0148BLZGE/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1 I know you said you don't want to use a level converter but it's the simplest way around the problem. You stated that you're an electronics beginner AS AM I, which is exactly why when I came ...


1

What your code is aiming at isn't particularly clear to me, but I will assume that for some obscure reason you want to accept an input String containing a series of ASCII-encoded hex digits, and create an array of half that many bytes with reconstituted byte values. First, here are several criticisms of the code shown. It's likely length(input) won't ...


1

Looks like is just plain NEC IR protocol. So use something like irsend.sendNEC(0xB04F, 16) for POWER_ON.


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