# Error Using If statement on the serial buffer

**URL:** <https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885>\
**Category:** Programming\
**Created:** [December 8, 2022, 2:05am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885 "2022-12-08T02:05:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 8, 2022, 2:05am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/1 "2022-12-08T02:05:57Z")

</div>

Hi there, I am trying to check whether the Serial Buffer contains a specific string, if so i would like to turn on one LED, but for some reason i can read an print out the Serial Buffer but i cannot check whether it is the sentence i want it to be. Btw, i am using an ESP32 connected to an Arduino Mega.

This is the code for Arduino Mega:  
//Code for Arduino MEGA  
#include \<Wire.h\>  
#include \<LiquidCrystal\_I2C.h\>  
LiquidCrystal\_I2C lcd(0x27, 16, 2);

uint8\_t leds\_cycle[] = { 5, 6, 7};  
uint8\_t leds\_control[] = {0, 2, 3, 4};

int quant = 20;  
int led\_passed = 0;  
int led\_delay = 1000;  
int current\_led = 0;

void setup() {  
pinMode(2, OUTPUT);  
pinMode(3, OUTPUT);  
pinMode(4, OUTPUT);  
pinMode(5, OUTPUT);  
pinMode(6, OUTPUT);  
pinMode(7, OUTPUT);

lcd.begin();  
lcd.backlight();  
lcd.clear();  
lcd.setCursor(4,0);

Serial.begin(115200);

}

void loop() {

led\_passed += quant;

if (Serial.available()){

```
//Serial.println("It is working in the Serial available");

String Incoming = Serial.readString();
Incoming.trim();

String Incoming1 = Serial.readString();
Incoming1.trim();

Serial.println(Incoming);
Serial.println(Incoming1);

delay(1000);

if(Incoming == "Switch 1"){
  Serial.println("It is coming in the if statement");
    if(Incoming1 == "1")
      digitalWrite(2, HIGH);

    digitalWrite(2,LOW);
}

else if(Incoming.toInt() == "3"){
  String State = Serial.readString();

  Serial.print("Setting LED ");
  Serial.print(Incoming);
  Serial.print(" to state ");
  Serial.println(State);
  
    if(State == "true" || State == "TRUE"){
      digitalWrite(3, HIGH);
    }
   digitalWrite(3, LOW);

}

else if(Incoming.toInt() == "4"){
  String State = Serial.readString();

  Serial.print("Setting LED ");
  Serial.print(Incoming);
  Serial.print(" to state ");
  Serial.println(State);

    if(State == "true" || State == "TRUE"){
      digitalWrite(4, HIGH);
    }
   digitalWrite(4, LOW);

}

else {
led_delay = Incoming.toInt();

}

```

}

if (led\_passed \> led\_delay) {  
digitalWrite(leds\_cycle[current\_led], LOW);  
led\_passed = 0;

current\_led = current\_led \>= 2 ? 0 : (current\_led + 1);  
// Turn on next LED in a row  
digitalWrite(leds\_cycle[current\_led], HIGH);  
delay(3000);  
/\* Serial.print("Current LED: ");  
Serial.println(current\_led);  
Serial.print("led\_passed: ");  
Serial.println(led\_passed);  
Serial.print("led\_delay: ");  
Serial.println(led\_delay);  
Serial.println('\n');\*/

}

}

This is the code for ESP32:  
//Code for ESP32

#include \<WiFi.h\>  
#include \<DHTesp.h\>  
#include \<ThingsBoard.h\>  
#include \<HardwareSerial.h\>

#define COUNT\_OF(x)((sizeof(x)/sizeof(0[x])) / ((size\_t)(!(sizeof(x) % sizeof(0[x])))))  
#define WIFI\_USER "YOURSSID"  
#define WIFI\_PASS "YOURPASS"  
#define TOKEN "YOURTOKEN"  
#define THINGSBOARD\_SERVER "YOURTHINGSBOARDSERVER"

#define echoPin 13  
#define trigPin 12

#define DHT\_PIN 14

HardwareSerial SerialPort(2);

WiFiClient espClient;  
ThingsBoard tb(espClient);

int status = WL\_IDLE\_STATUS;

uint8\_t leds\_cycle[] = {5, 6, 7};  
uint8\_t leds\_control[] = {0, 2, 3, 4};

DHTesp dht;

int quant = 20;  
int led\_delay = 1000;  
int send\_delay = 2000;  
int led\_passed = 0;  
int send\_passed = 0;  
int current\_led = 0;

bool subscribed = false;

long duration;  
int distance;

RPC\_Response processDelayChange(const RPC\_Data &data)  
{  
led\_delay = data;  
SerialPort.print(led\_delay);

```
return RPC_Response(NULL, led_delay);

```

}

RPC\_Response processGetDelay(const RPC\_Data &data)  
{  
return RPC\_Response(NULL, led\_delay);  
}

RPC\_Response processSetSwitch1(const RPC\_Data &data)  
{  
Serial.println("Received the set GPIO RPC method");

bool onoff = data;  
SerialPort.println("Switch 1");  
SerialPort.println(onoff);

return RPC\_Response(NULL, onoff);  
}

RPC\_Response processSetSwitch2(const RPC\_Data &data)  
{  
Serial.println("Received the set GPIO RPC method");

bool onoff = data;  
SerialPort.print("Switch data: ");  
SerialPort.println(onoff);

return RPC\_Response(NULL, onoff);  
}

RPC\_Response processSetSwitch3(const RPC\_Data &data)  
{  
Serial.println("Received the set GPIO RPC method");

bool onoff = data;  
SerialPort.print("Switch data: ");  
SerialPort.println(onoff);

return RPC\_Response(NULL, onoff);  
}

RPC\_Callback callbacks[] = {  
{ "setValue", processDelayChange },  
{ "getValue", processGetDelay },  
{ "setSwitch1", processSetSwitch1 },  
{ "setSwitch2", processSetSwitch2 },  
{ "setSwitch3", processSetSwitch3 },  
};

void setup() {  
pinMode(trigPin, OUTPUT);  
pinMode(echoPin, INPUT);

WiFi.mode(WIFI\_STA);  
WiFi.begin(WIFI\_USER, WIFI\_PASS);  
Serial.begin(115200);

SerialPort.begin(115200, SERIAL\_8N1, 16, 17);  
InitiateWiFi();  
dht.setup(DHT\_PIN, DHTesp::DHT22);

}

void loop() {  
delay(quant);

led\_passed += quant;  
send\_passed += quant;

// Reconnect to WiFi, if needed  
if (WiFi.status() != WL\_CONNECTED) {  
reconnect();  
return;  
}

if (!tb.connected()) {  
subscribed = false;

```
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
  return;
}

```

}

// Subscribe for RPC, if needed  
if (!subscribed) {

```
if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
  return;
}

subscribed = true;

```

}

// Check if it is a time to send DHT22 temperature and humidity  
if (send\_passed \> send\_delay) {

```
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor

TempAndHumidity lastValues = dht.getTempAndHumidity();    
if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
  
} else {
  tb.sendTelemetryFloat("temperature", lastValues.temperature);
  tb.sendTelemetryFloat("humidity", lastValues.humidity);
}

tb.sendTelemetryFloat("Distance", distance);
send_passed = 0;

```

}

// Process messages  
tb.loop();

}

void InitiateWiFi(){

while (WiFi.status() != WL\_CONNECTED){  
delay(500);

}

}

void reconnect(){  
status = WiFi.status();

if (status != WL\_CONNECTED){  
WiFi.begin(WIFI\_USER, WIFI\_PASS);  
while (WiFi.status() != WL\_CONNECTED){  
delay(500);

```
}

```

}  
}

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 8, 2022, 2:19am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/2 "2022-12-08T02:19:44Z")

</div>

> [@junyulim](#):
>
> `else if(Incoming.toInt() == "3"){`

You can't compare an integer to a character string. Perhaps you meant:  
`else if(Incoming == "3"){`  
or  
`else if(Incoming.toInt() == 3){`

> [@junyulim](#):
>
> ```arduino
> String Incoming = Serial.readString();
> Incoming.trim();
> 
> String Incoming1 = Serial.readString();
> 
> ```

It is unlikely that "Incoming1" will contain any characters since the input buffer has been empty for at least a second after reading "Incoming".

---

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 8, 2022, 2:24am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/3 "2022-12-08T02:24:19Z")

</div>

Hello, thanks for the reply. Actually i noticed that, I've just been testing on one of the if else statement actually, which this line:

if(Incoming == "Switch 1"){  
Serial.println("It is coming in the if statement");  
if(Incoming1 == "1")  
digitalWrite(2, HIGH);

```
digitalWrite(2,LOW);

```

}

Perhaps you could help me check whether i got any error with the first if else statement, that would be great.

---

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 8, 2022, 2:28am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/4 "2022-12-08T02:28:40Z")

</div>

Actually, Incoming1 contains data as I used Serial.println(Incoming1); in the following line to check what was i receiving.

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [December 8, 2022, 4:15am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/5 "2022-12-08T04:15:27Z")

</div>

You might miss some brackets {} and an else.  
If Incoming1 is equal to "1", DO2 will be set high and immediately thereafter it will be set low...

Please use code tags! And align your code properly (use CTRL-T).

---

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 8, 2022, 4:29am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/6 "2022-12-08T04:29:44Z")

</div>

According to Arduino Language Reference "The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement."  
Additionally, I purposely added the statement Serial.println("It is coming in the if statement") to check whether it is going into the if statement.

Thanks for the alignment recommendation tho.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 8, 2022, 9:57am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/7 "2022-12-08T09:57:08Z")

</div>

> [@junyulim](#):
>
> According to Arduino Language Reference....

It's right, but ...  
Are you don't clue, that this combination of the statements is senseless?

> [@junyulim](#):
>
> ```arduino
> if(Incoming1 == "1")
> digitalWrite(2, HIGH);
>       
> digitalWrite(2,LOW);
> 
> ```

---

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 9, 2022, 1:59am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/8 "2022-12-09T01:59:10Z")

</div>

Sorry, I don't quite get your point. How is it senseless? Because I am sending "Switch 1" and "1" on the ESP32 to the Arduino Mega, therefore, the Arduino Mega should receive "Switch 1" and "1" and store it in Incoming and Incoming1 respectively. With that, I am using the If statement to check whether Incoming contains "Switch 1" and the following If statement to check whether Incoming1 contains "1". If both of the conditions are satisfied, the LED should be set, however, if only the first If statement is satisfied, the LED would be set LOW or turned off. May you elaborate further if I misunderstood your point. Thanks 😃.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [December 9, 2022, 2:09am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/9 "2022-12-09T02:09:11Z")

</div>

> [@junyulim](#):
>
> How is it senseless?

Why do you think that pin 2 won't be set low immediately after it is set high when the condition is met.

This was presented to you in post #5. You chose to focus on the brackets and not the else.

---

<div class="post-metadata">

**Author:** ![junyulim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/junyulim/32/918931_2.png) [@junyulim](https://forum.arduino.cc/u/junyulim)\
**Post date:** [December 9, 2022, 3:03am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/10 "2022-12-09T03:03:06Z")

</div>

Thanks for pointing it out there, I get what you mean. I am missing an else statement there, therefore, the LED is set on and set off immediately.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 7, 2023, 3:03am UTC](https://forum.arduino.cc/t/error-using-if-statement-on-the-serial-buffer/1062885/11 "2023-06-07T03:03:40Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
