Skip to main content
Tweeted twitter.com/StackArduino/status/1327083805371469826

How to compare a string coming from serial monitor with some predefined text stored as a local variable? if iIf I say:

int led = 2;
String a = " abcds"; 

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

void loop() {
  String b = Serial.read();
  Serial.println(b);

  if (b != a) {
    digitalWrite(2,LOW);
  }
  else
  {
    digitalWrite(2,HIGH);
  }
}

just as an example, this code will not compile because on the serial iI receive bytes and iI want to compare with a string. So my question is... how should be done?

Thanks in advance !

How to compare a string coming from serial monitor with some predefined text stored as a local variable? if i say:

int led = 2;
String a = " abcds";
void setup(){
Serial.begin(9600);
}

void loop(){
String b = Serial.read();
Serial.println(b);

if(b != a){
 digitalWrite(2,LOW);
}
else
{
digitalWrite(2,HIGH);
}

just as an example, this code will not compile because on the serial i receive bytes and i want to compare with a string. So my question is... how should be done?

Thanks in advance !

How to compare a string coming from serial monitor with some predefined text stored as a local variable? If I say:

int led = 2;
String a = " abcds"; 

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

void loop() {
  String b = Serial.read();
  Serial.println(b);

  if (b != a) {
    digitalWrite(2,LOW);
  }
  else
  {
    digitalWrite(2,HIGH);
  }
}

just as an example, this code will not compile because on the serial I receive bytes and I want to compare with a string. So my question is... how should be done?

Became Hot Network Question
Source Link

How to compare a string

How to compare a string coming from serial monitor with some predefined text stored as a local variable? if i say:

int led = 2;
String a = " abcds";
void setup(){
Serial.begin(9600);
}

void loop(){
String b = Serial.read();
Serial.println(b);

if(b != a){
 digitalWrite(2,LOW);
}
else
{
digitalWrite(2,HIGH);
}

just as an example, this code will not compile because on the serial i receive bytes and i want to compare with a string. So my question is... how should be done?

Thanks in advance !