problem calling external functions

hi everyone,

i am sure that is a stupid question but i am new to programming and not sure how to do this.

in my sketch i am calling a function named sendSms(), and inside of it i want to execute another function named waitForAnswer. The sendSms function does its job, but when its time to execute waitForAnswer it doesn’t give any time for the user to reply.

Can i handle it with a simple delay() on top of the second function?

Here is the code i am using:

void sendSms()
 {
  Serial1.print("AT+CMGF=1\r");    //SEND THE SMS IN TEXT MODE
  delay(100);
  Serial1.println("AT + CMGS = \"+30xxxxxxxxxx\"");    
  delay(100);
  Serial1.println("New Data:");     
 // Serial1.print(dat);               
  Serial1.println(data.temperature);              
  Serial1.println(data.humidity);               
  Serial1.print(data.soil);              
  delay(100);                       
  Serial1.println((char)26);        
  delay(100);                       
  Serial1.println();          
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please wait");  
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("For Answer");
  delay(20000);
  waitForAnswer();  
 }

 
void waitForAnswer()  {

if (Serial1.available() > 0)    //If a character comes in from the Serialular module...
  {
    delay(10);
    inchar = Serial1.read();
    if (inchar == '0')        
    {

      inchar = Serial1.read(); //read next char  '#'
      if (inchar == '#')
      {
        inchar = Serial1.read(); //read next char 
        if (inchar == 'a')
        {
          digitalWrite(relay_1, LOW);
        }
        else if (inchar == 'A')
        {
          digitalWrite(relay_1, HIGH);
        }
        else if (inchar == 'b')
        {
          digitalWrite(relay_2, LOW);
        }
        else if (inchar == 'B')
        {
          digitalWrite(relay_2, HIGH);
        }
         else if (inchar == '0')
        {
          digitalWrite(relay_1, LOW);
          digitalWrite(relay_2, LOW);
        }
         else if (inchar == '1')
        {
          digitalWrite(relay_1, HIGH);
          digitalWrite(relay_2, HIGH);
        }
             Serial1.println("AT+CMGD=1,4");    // delete all SMS

      digitalWrite(5, HIGH);
      digitalWrite(5, LOW);

      //delay(8000);

      
      }
    }

  }
  
}

Thank you in advance!

but when its time to execute waitForAnswer it doesn't give any time for the user to reply.

Just naming a function waitForAnswer() is not enough to make it wait for an answer. You have to actually code it that way.

How long do you want to wait? How will you know that you have gotten a complete answer?

That is what i want to do. check somehow if the user have send an sms and then return to the first function.
Can you please help me with this?

Can you please help me with this?

No, because you did not answer the questions that I asked.

what do you mean?
i want the function to wait until the user send an sms with the correct pattern.

i want the function to wait until the user send an sms with the correct pattern.

And how will the Arduino know that the pattern is correct?

Noticethatallthroughthisthread,untilnow,Ihaveusedfunkylittlemarkersattheendofwordsandsentences,soyou'dknowwhentheendofapacketasarrived.Whatmarkstheendofthepacketsyouwanttoreceive?Thatis,howdoyouknowwhentoquitwaiting?

i guess i 'll have to store them in an array?

if i knew how to do this i wouldn't ask in this forum.. i don't want you to write me the whole code, just some help so i know how to start.

So i have to declare the last chars of all the possible correct messages the user might send. in an array? and how to check if the last char is a char from the array?

i guess i 'll have to store them in an array?

That would be a good guess.

if i knew how to do this i wouldn’t ask in this forum.

If you knew how to describe what you want, it would certainly be easier.

If you could say something like “I’d like a function that can read all serial data that arrives, until I see an X, or until YYY seconds has passed”, then we could help you.

When you say “I want to wait for a response…”, but you can’t define when “a response” has arrived, we can’t help you.

Once of the aggravating things about the GSM devices is that they simply quit talking when they are done

It would certainly be useful if one could know that the device had quit talking because it had sent a complete message, vs. it had quit talking because power was cut, or because it was busy doing something else, and will resume talking when it can.

But, until people start insisting that end-of-packet markers are important, manufacturers are not going to change.

So i have to declare the last chars of all the possible correct messages the user might send. in an array?

I guess that is one way. Another would be to say “When you send a text message that contains a pattern, mark the start of the pattern data with a < and mark the end of a pattern with a >”. (You can, of course, use any start and end markers that you like.)

And now PaulS has mentioned start and end markers, you can start reading Serial Input Basics - updated