Found 203 Articles for Arduino

Reserve memory in Arduino for string manipulation

Yash Sanghvi
Updated on 29-May-2021 13:32:27

598 Views

It may happen that a string may change length dynamically during the execution of a program.If you want to ensure that there is always enough memory available for your string, you can reserve some memory using the reserve() function.SyntaxString1.reserve(n_bytes);Where String1 is the string for which you are reserving memory, and n_bytes (unsigned int) is the number of bytes to be reserved in the memory.ExampleString s1 = "Hello"; void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    s1.reserve(20);    s1 = s1+" World!";    Serial.println(s1);    s1 = s1+" I'm now trying ... Read More

How to Remove Characters from a String in Arduino?

Yash Sanghvi
Updated on 29-May-2021 13:31:15

5K+ Views

The remove function in Arduino helps you remove one or more characters from within a string.SyntaxmyString.remove(index, count)Here, index refers to the index from where removal has to start. Note that indexing in Arduino starts with 0. Thus, within string "Hello", 'H' is at index 0, 'e' is at index 1, and so on.The count argument is optional, and it specifies the number of characters to remove. If you don’t specify the count, then all characters starting from index till the end of the string will be removed. If you specify count as say, 3, then 3 characters starting from index position will ... Read More

How to Trim a String in Arduino?

Yash Sanghvi
Updated on 29-May-2021 13:11:42

3K+ Views

Sometimes, a string can contain leading or trailing whitespaces. Arduino has a trim() function that removes all these leading/trailing whitespaces from the string.SyntaxString1.trim()Where String1 is the string that you need to trim. Please note that this function doesn’t return anything. String1 itself is modified.ExampleThe following example illustrates this −void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    String string1 = " Hello World! ";    Serial.println(string1);    string1.trim();    Serial.println(string1); } void loop() {    // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is ... Read More

Get the last occurrence of a substring within a string in Arduino

Yash Sanghvi
Updated on 29-May-2021 13:28:49

572 Views

Just like indexOf() helps identify the first occurrence of a substring within a string, the lastIndexOf() function helps identify the last occurrence. This is because lastIndexOf() performs backward search, while indexOf() performs forward search.syntaxmyString.lastIndexOf(substr)Where substr is the substring to search for in myString. It can be a character or a string.Just like indexOf(), this function also accepts an optional from argument, in case you want the backward search to begin from a specific index. The syntax in that case is −SyntaxmyString.lastIndexOf(substr, from)Just like indexOf(), this function either returns the last index of the substring within the string, or it returns -1 if no match ... Read More

Get the first occurrence of a substring within a string in Arduino

Yash Sanghvi
Updated on 29-May-2021 13:27:49

458 Views

The indexOf() function within Arduino scans a string from the beginning, and returns the first index of the specified substring within the string. The syntax is −SyntaxmyString.indexOf(substr)where substr is the substring to search for. It can be of type character or string.Optionally, you can provide a different starting point to begin the search from, in which case, the syntax is −SyntaxmyString.indexOf(substr, from)where from is the index from where the search should start. This function returns the index of the first occurrence of the substring within the string, or -1, if no match is found.Examplevoid setup() {    // put your setup code here, ... Read More

Check if two strings are equal while ignoring case in Arduino

Yash Sanghvi
Updated on 29-May-2021 13:11:04

611 Views

We know that String1.equals(String2) can be used to find out if String1 and String2 are equal in Arduino. However, this function is case sensitive. So, if there is a difference in the case of even a single character, this function will return false. A strategy that people use to perform case insensitive comparison of two strings is to convert both the strings to lower case and then compare. However, Arduino has a function to compare two strings while ignoring case. As you’d have guessed, the function is equalsIgnoreCase.ExampleAn example implementation is given below −void setup() {    // put your ... Read More

Structs in Arduino program

Yash Sanghvi
Updated on 15-Sep-2023 02:21:17

28K+ Views

A struct is simply a collection of different types of variable. Structs in Arduino mimic the structs in C language. So, if you are familiar with C structs, Arduino structs shouldn’t be an issue. The struct declaration syntax is as follows −Syntaxstruct structName{    item1_type item1_name;    item2_type item2_name;    .    .    .    itemN_type itemN_name; }An example is given below −Examplestruct student{    String name;    int age;    int roll_no; }The elements of a struct are accessed using the . (dot) notation. This notation can be used for both reading the elements of a struct, or changing ... Read More

Suspend/Resume tasks in FreeRTOS using Arduino

Yash Sanghvi
Updated on 29-May-2021 13:04:26

700 Views

If you wish to suspend a task in FreeRTOS, there is a function vTaskSuspend() that can be used. The syntax is −Syntaxvoid vTaskSuspend( TaskHandle_t xTaskToSuspend );As you can see, it takes the handle of the task to suspend as the argument and returns nothing. A suspended task can be resumed using vTaskResume(). The syntax is −Syntaxvoid vTaskResume( TaskHandle_t xTaskToResume );This again takes the handle of the task to be resumed, and returns nothing.In order to see an example, we will walk-through the code given in −https://exploreembedded.com/wiki/Task_Suspend_and_ResumeAs you can see, four Task handles are declared initially, and the tasks are created in the ... Read More

Semaphore and Mutex in FreeRTOS using Arduino

Yash Sanghvi
Updated on 29-May-2021 13:09:18

1K+ Views

Semaphore and Mutex are tools/mechanisms to bring about synchronization between tasks in FreeRTOS. Often, two tasks need to share a resource, or one task needs to tell another that it is idle/waiting. Semaphores and Mutex help here. In this article, we will look at the concepts of semaphore and mutex.SemaphoreA semaphore is a synchronization mechanism between tasks. More specifically, it is a signalling mechanism. A task in the waiting state may receive a semaphore which tells it to do some work. Once the task has done that work, it will give the semaphore back. In practice, this is maintained by ... Read More

Change the default location for saving sketches in Arduino IDE

Yash Sanghvi
Updated on 29-May-2021 13:08:43

734 Views

By default, on a Windows machine, Arduino saves all the sketches in C:\Users\\Documents\Arduino. Now, for whatever reason, you may want to change this default location. A common reason is that the C: has limited storage and you’d like to save sketches to a drive which has enough free space.In order to change the default location, go to File → Preferences.In the dialog box that opens up, the first input field is the Sketchbook location. Click on the ‘Browse’ button next to it and choose your desired path.After changing the path, click OK. Now, if you try to save a new ... Read More

Advertisements