Found 203 Articles for Arduino

Calculate time of operation in Arduino

Yash Sanghvi
Updated on 26-Oct-2023 02:34:16

23K+ Views

Often, you need to measure the time your microcontroller takes to perform a particular task. You can use the millis() function of Arduino to measure the time. This function returns the number of milliseconds passed since your board started running the current program. Therefore, to calculate the time taken by an operation, you can call millis() before and after your operation, and take the difference of the two values.An example implementation is given below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    long int t1 = millis();    task_whose_time_is_to_be_measured();    long int ... Read More

Defining new functions in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:26:57

166 Views

Defining new functions in Arduino is equivalent to defining the functions in C.The syntax is −Syntaxreturn_type function_name(arg_type arg)The only difference is that in C, the function needs to be declared at the top, if it is invoked before its definition. No such constraint exists in Arduino. The following code demonstrates this −Examplevoid setup() {    Serial.begin(9600);    Serial.println(); } void loop() {    // put your main code here, to run repeatedly:    for (int i = 0; i < 10; i++) {       long int w = square(i);       Serial.println(w);       delay(1000);   ... Read More

Convert string to lowercase or uppercase in Arduino

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

5K+ Views

In order to convert a string to lower/upper case, the in-built .toLowerCase() and .toUpperCase() functions can be used.Note: These functions change the original string itself, and don't return a new string with the changes.The implementation is shown below −Examplevoid setup() {    Serial.begin(9600);    Serial.println();    String s1 = "Hello World";    Serial.println(s1);    s1.toLowerCase();    Serial.println(s1);    s1.toUpperCase();    Serial.println(s1); } void loop() {    // put your main code here, to run repeatedly: }The corresponding Serial Monitor output is −OutputAs you can see, the changes have been made in s1 itself. The return of .toUpperCase() and .toLowerCase() is void.Read More

Replace characters in a string in Arduino

Yash Sanghvi
Updated on 24-Mar-2021 05:23:39

4K+ Views

The .replace() function in Arduino allows you to replace a character or a substring with another character/substring in Arduino.Note: This function replaces substrings in the original string itself, and does not return a new string containing the changes.Examples are given in the code below −Examplevoid setup() {    Serial.begin(9600);    Serial.println();    String s1 = "Hello World";    Serial.println(s1);    s1.replace('e', 'a');    Serial.println(s1);        s1 = "Hello World";    s1.replace("ll", "gg");    Serial.println(s1);    s1 = "Hello World";    s1.replace("li", "gg");    Serial.println(s1); } void loop() {    // put your main code here, to run ... Read More

Convert string to integer/ float in Arduino

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

3K+ 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

13K+ 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

13K+ 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

7K+ 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 or not in Arduino

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

605 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

Find if a string begins with a specific set of characters in Arduino

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

498 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

Advertisements