Arduino String Compare From Serial Monitor

Posted on

Compare Strings Arduino

How to Arduino String Compare from Serial Communication? String Language Simple is a sentence. Comparing strings on Arduino programming is not as easy as comparing numbers such as int and float.

In comparing the string or string of Arduino Compare, there are several methods used, depending on where our strings you want to compare.

Operator for Compare

To be able to compare a value, we need a comparison operator. Likewise with strings. String comparison operators include:

  • “==” read “the same as”
  • “! =” read “not the same as”
  • “>” read “bigger than”
  • “<” read “is smaller than” “> =” read “greater the same as”
  • “<=” read “smaller the same as”
  • “Equals ()” is the same as the operator “==”

This command:

if (stringFirst == stringSecond) {

Equals with:

if (stringFirst.equals(stringSecond)) {

Basic Code Program

The following is the code for the predetermined string example. We will compare two string values.

We make stringFirst = “test” and stringSecond = “test”. If these two strings are true and the same, then Arduino will send notifications to the monitor series with the word “this word is the same”.

Here is the program:

String stringFirst = "test";
String stringSecond = "test";

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (stringFirst == stringSecond)
  {
    Serial.println("yes");
  }
  
  else Serial.println("not");
  delay(5000);
}

Please open the Arduino Monitor series, then the statement will appear. Now, please change the word “test” from one string into another word. Then upload the program again.

Look back at the Arduino Monitor series, you will see the results of his statement.

Compare from Serial Communication

String handling on data from this series is different from the set data. To read string data from the serial and compare it, there are two methods, namely:

  1. Read the string from the serial to the data ‘\ n’, then the data is cut and then compared.
  2. Read the string from the serial to the data ‘\ n’, then the data is changed directly to the char array, then compared

Now we will make a simple Arduino project, turn on the LED through the string communication series.

The example of the project to be used is, turning on 3 LEDs with different colors with the string command.

  • String “red on” to turn on the red light, “red off” to turn it off.
  • String “green on” to turn on the green light, “green off” to turn it off.
  • String “blue on” to turn on the blue light, “Blue Off” to turn it off.

Before starting the program, the circuit we will use is like this:

Read string data to contain ‘\ n’ data then cut the data

Now we will learn how to compare the string values sent through serials.

Please upload the following program:

/* www.chippiko.com
   Program Compare String From Serial To Turn On LED
*/

int redLed = 13;
int greenLed = 12;
int blueLed  = 11;

String read_string = " ";
void setup() {
  Serial.begin(9600);

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
}

void loop() {
  while (Serial.available() > 0) {
    read_string = Serial.readStringUntil('\n');
    read_string.trim();
    delay(100);

    if (read_string == "Red On") {
      digitalWrite(redLed, HIGH);
    }

    if (read_string == "Red Off") {
      digitalWrite(redLed, LOW);
    }

    if (read_string == "Green On") {
      digitalWrite(greenLed, HIGH);
    }

    if (read_string == "Green Off") {
      digitalWrite(greenLed, LOW);
    }

    if (read_string == "Blue On") {
      digitalWrite(blueLed, HIGH);
    }

    if (read_string == "Blue Off") {
      digitalWrite(blueLed, LOW);
    }
  }
}

After the program is uploaded, open the monitor series. Type the string, for example “Red On” then send. Notice on the LED whether the lights are on. If you don’t check the circuit.

Change String Data to Char Array

Now, with the same project, we will first change the string received to be char array. This is actually a better way.

/* www.chippiko.com
      Program Compare String From Serial To Turn On LED
*/

int redLed = 13;
int greenLed = 12;
int blueLed  = 11;

char bufferString[32];

void setup() {
  Serial.begin(9600);

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
}

void loop() {
  while (Serial.available() > 0) {

    Serial.readBytesUntil('\n', bufferString, 32);

    if (strcmp(bufferString, "Red On") == 0) {
      Serial.println(bufferString);
      digitalWrite(redLed, HIGH);
    }
    if (strcmp(bufferString, "Red Off") == 0) {
      Serial.println(bufferString);
      digitalWrite(redLed, LOW);
    }

    if (strcmp(bufferString, "Green On") == 0) {
      digitalWrite(greenLed, HIGH);
    }
    if (strcmp(bufferString, "Green Off") == 0) {
      digitalWrite(greenLed, LOW);
    }

    if (strcmp(bufferString, "Blue On") == 0) {
      digitalWrite(blueLed, HIGH);
    }
    if (strcmp(bufferString, "Blue Off") == 0) {
      digitalWrite(blueLed, LOW);
    }
    memset(bufferString, 0, sizeof(bufferString));
  }
}

I hope this Arduino String Compare Serial article is useful. Thank you for visiting the Chip Piko website.

Leave a Reply

Your email address will not be published.