Convert String to Integer and Float in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:22:58

4K+ Views

In order to convert a string to an integer or a float, the .toInt() and .toFloat() functions can be used. Of course, the string should actually correspond to the integer or floating-point value. For instance, "1.87" can be converted to float. But it doesn't make sense to convert "Hello" to float. The below example code illustrates the conversions −Examplevoid setup() {    Serial.begin(9600);    Serial.println();    // put your setup code here, to run once:    String s1 = "235";    String s2 = "1.56";    String s3 = "Hello";    int i1 = s1.toInt();    int i2 = s2.toInt();   ... Read More

Concatenate Strings in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:20:34

16K+ Views

String concatenation in Arduino is quite straightforward and also robust. You simply use the + operator to join strings. However, it doesn't end with joining two strings. You can concatenate characters, and even integers and floats to strings (Arduino converts the integers and floating-point numbers to string internally). Examples can be seen in the below code.Examplevoid setup() {    Serial.begin(9600);    Serial.println();    // put your setup code here, to run once:    String s1 = "Hello ";    String s2 = "Bond!";    String s3 = s1 + s2;    Serial.println(s3);    s3 = s1 + 7;    Serial.println(s3); ... Read More

Convert Character Array to String in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:20:05

16K+ Views

In order to convert a character array to a string, the String() constructor can be used. An example is shown below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    char buf[10] = "Hello!";    Serial.print("Char array: ");    Serial.println(buf);    String s = String(buf);    Serial.print("String: ");    Serial.println(s); } void loop() {    // put your main code here, to run repeatedly: }The output of the Serial monitor is shown below −Output

Convert String to Character Array in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:17:46

10K+ Views

There are several libraries built for Arduino whose functions take in character arrays as inputs instead of strings. Thankfully, Arduino has an inbuilt method (toCharArray()) to covert a String to a Character Array. A sample implementation is given below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    String s1 = "Hello World!";    char buf[30];    s1.toCharArray(buf, 6);    Serial.println(buf);    s1.toCharArray(buf, s1.length());    Serial.println(buf); } void loop() {    // put your main code here, to run repeatedly: }As you can see, the toCharArray function takes in two arguments, ... Read More

Check If Two Strings Are Equal in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:17:17

788 Views

In order to check if two strings are equal, the .equals() function can be used. This returns a Boolean. True if both the strings are equal, else false. An example code is given below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    String s1 = "Hello";    String s2 = "Hello";    String s3 = "World";    if(s1.equals(s2)){       Serial.print("s1 equals s2");       }    if(s1.equals(s3)){       Serial.print("s1 equals s3");       }    if(s2.equals(s3)){       Serial.print("s2 equals s3");    } ... Read More

Check If a String Begins with Specific Characters in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:16:42

832 Views

The startsWith() function in Arduino helps you determine if a string starts with a specific set of characters. It returns true if the string starts with the substring you specified, else it returns false. An example implementation is given below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    String String1 = "AB_Test";    String String2 = "CD_Test";    String substr1 = "AB";    String substr2 = "AB*";    if(String1.startsWith(substr1)){          Serial.print("String1 starts with substr1");       }    if(String2.startsWith(substr1)){          Serial.print("String2 starts with substr1");   ... Read More

Find Substring Within a String in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:53:17

19K+ Views

In order to check if a specific substring exists within a string in Arduino, the indexOf() function can be used. This returns the index of the first occurrence of the character or a string that you are searching for within another string. In case the character or string does not exist within the given string, the function returns -1.Example implementations are given in the code below −Examplevoid setup() {    Serial.begin(9600);    Serial.println();    String s1 = "Hello World";    Serial.println(s1.indexOf('e'));    Serial.println(s1.indexOf("Wor"));    Serial.println(s1.indexOf('z'));    Serial.println(s1.indexOf("Wer")); } void loop() {    // put your main code here, to run ... Read More

LED Blinking Using Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:49:16

641 Views

In order to blink an LED using Arduino, we first connect perform the hardware connections. Choose a pin of your board that supports digital output. We are using the Arduino Uno board, and we will choose pin 7. The circuit will look like this −As you can see, one end of a resistor is connected to pin 7 of Arduino Uno. The other end the resistor is connected to the longer leg (positive) of the LED. The shorter leg of the LED is connected to GND.The value of the resistor can be of the order of 100 Ohms. We'll choose ... Read More

Change the Resolution of AnalogRead in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:48:47

3K+ Views

By default, the analogRead in Arduino follows a 10-bit resolution (this may be different for different boards). However, sometimes, you may not need such a high resolution. Often, people wish to set the resolution to 8-bits, to save on storage. This is because an 8-bit value will take just one byte of storage, whereas anything above that will take two bytes of storage.Luckily, Arduino has an inbuilt function to change the resolution of the analog to digital conversion. The function is analogReadResolution() and it takes in the argument as the resolution in bits.So, if you wish to set an 8-bit ... Read More

Basic AnalogRead in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:46:52

376 Views

Converting analog values to digital is a common requirement from microcontrollers in general, and Arduino is no different. Arduino IDE has a built-in analogRead function to facilitate the conversion of analog values to digital.From the programming perspective, the only thing you require to know is the pins of your microcontroller that support ADC. On the Arduino UNO board, the pins A0 to A5 support ADC.Now, let us assume that you've connected your A0 pin to an analog wire (maybe the junction between an LDR and a resistor, or the central leg of a potentiometer).The basic Arduino code to print the ... Read More

Advertisements