Reading multiple strings

I am basically trying to read a string through the serial port. I have used "Serial.readstringUntil" syntax to read a string and then compare to some data. I am successfully able to do that. Now in the same program I want to read another string again and then compare it with some other data. But it fails since the comparison is done between the previously loaded string. What can be the issue. Not a pro! Sorry. Here is the code.

int x;
String str;
String data="on";
String data2="off";
void calfunc();
void setup()
{
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  pinMode(13, OUTPUT);

  Serial.println("Enter");


}
void loop() 
{
  if(Serial.available() > 0)
  {
    str = Serial.readStringUntil(' ');
    //x = Serial.parseInt();
    //Serial.println(str);
    if(str.equals (data))
    {
      Serial.println("Access Granted. ");
      calfunc();
    }
    else if(str.equals(data2))
    {
      digitalWrite(5, HIGH);
      digitalWrite(13, HIGH);
      Serial.println("Access Denied");
    }
  }
  delay(100);
}

void calfunc(){
  digitalWrite(5, LOW);
  digitalWrite(13, LOW);
  
  
  Serial.println("For Temperature press 1");
  Serial.println("For Moisture press 2");
  delay(100);

  char a=Serial.read();
  if(a=='1')
  {
    Serial.println(analogRead(A0));
  }
  else if(a=='2')
  {
    Serial.println(analogRead(A1));
  }
  else
  {
    Serial.println("Wrong Choice");
    delay(500);
    return;
  }
  return;
}

Here is a better lesson that I could post on the forum.
It does not use C++ String but instead C string arrays which are far more suited to microcontrollers.

How to process incoming serial data without blocking

http://gammon.com.au/serial

The examples do allow for continued entries and provide a set of techniques to get you to ‘next level’ coding.

The demo that I attached to Reply #4 in this Thread shows how to compare simple strings (small s).

...R

meet4108: I am basically trying to read a string through the serial port. I have used "Serial.readstringUntil" syntax to read a string and then compare to some data. I am successfully able to do that. Now in the same program I want to read another string again and then compare it with some other data. But it fails since the comparison is done between the previously loaded string. What can be the issue.

is your problem processing the serial input in the calfunc() function? how long do you give yourself to put a char into the serial buffer?

void calfunc(){
  digitalWrite(5, LOW);
  digitalWrite(13, LOW);
  Serial.println("For Temperature press 1");
  Serial.println("For Moisture press 2");
  delay(100);
  char a=Serial.read();
  if(a=='1')
  {
    Serial.println(analogRead(A0));
  }
  else if(a=='2')
  {
    Serial.println(analogRead(A1));
  }
  else
  {
    Serial.println("Wrong Choice");
    delay(500);
    return;
  }
  return;
}

You will not likely be able to get your next character into the serial monitor fast enough to get the serial read done.

Normally we check to see if there's a character in serial before we read it. Throwing in delays and assuming is just bug farming.

GoForSmoke: Normally we check to see if there's a character in serial before we read it. Throwing in delays and assuming is just bug farming.

exactly...

if you want to process the calfunc() like you are, consider waiting for an entry:

void calfunc()
{
  boolean Flag = true;
  digitalWrite(5, LOW);
  digitalWrite(13, LOW);
  Serial.println("For Temperature press 1");
  Serial.println("For Moisture press 2");
  while (Flag == true)
  {
    if (Serial.available())
    {
      char a = Serial.read();
      if (a == '1')
      {
        Serial.println(analogRead(A0));
      }
      else if(a == '2')
      {
        Serial.println(analogRead(A1));
      }
      else
      {
        Serial.println("Wrong Choice");
      }
      Flag = false;
    }
  }
}

there certainly are better ways to do what you are doing, but this can get you there.

I tried the above ways and means but it always returns “Wrong choice” as soon as i enter "on ". It doesnt even accept another character.
Here is the modified code:

String str;
String data="on";
String data2="off";
void calfunc();
void setup()
{
  Serial.begin(9600);
  Serial.println("Enter Password");
}
void loop() 
{
  if(Serial.available() > 0)
  {
    str = Serial.readStringUntil(' ');

    if(str.equals (data))
    {
      Serial.println("Access Granted. ");
      calfunc();
    }
    if(str.equals(data2))
    {

      Serial.println("Access Denied");
    }
  }
  delay(100);
}

void calfunc(){
  boolean stat=true;
  while(stat==true)
  {
    Serial.println("For Temperature press 1");
    Serial.println("For Moisture press 2");

    if(Serial.available())
    {
      char a=Serial.read();
      if(a=='1')
      {
        Serial.println(analogRead(A0));
      }
      else if(a=='2')
      {
        Serial.println(analogRead(A1));
      }
      else
      {
        Serial.println("Wrong Choice");
        delay(500);
      }
      stat=false;
    }
  }
}

its not really looking like what I showed you...

so try this

String str;
String data="on";
String data2="off";
boolean readyToGo;
void calfunc();
void setup()
{
  Serial.begin(9600);
  Serial.println("Enter Password");
}
void loop() 
{
  if (readyToGo)
  {
    Serial.println("Enter Password");
    readyToGo = false;
  }
  {
  if(Serial.available())
  {
    str = Serial.readStringUntil(' ');

    if(str.equals (data))
    {
      Serial.println("Access Granted. ");
      calfunc();
    }
    if(str.equals(data2))
    {

      Serial.println("Access Denied");
    }
  }
  }
}

void calfunc()
{
  boolean stat=true;
  Serial.println("For Temperature press 1");
  Serial.println("For Moisture press 2");
  while(stat==true)
  {
    if(Serial.available())
    {
      char a=Serial.read();
      if(a=='1')
      {
        Serial.println(analogRead(A0));
      }
      else if(a=='2')
      {
        Serial.println(analogRead(A1));
      }
      else
      {
        Serial.println("Wrong Choice");
        delay(500);
      }
      stat=false;
      readyToGo = true;
    }
  }
}

When you enter 1 or 2, is that all that gets sent?

I have Serial Monitor add a newline character ( '\n' == 10 ) at the end of every line because I want to know where the line ends. I don't have to do that but since my code expects that, it deals with that.

I'm pretty sure that Nick covers all that on that blog page, but who wants to read all that?