Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for my Arduino board to jump to a function based on an if condition, where a string (content) is checked to see if it matches the word FALLEN. However, the if condition never occurs regardless of which way I phrase the condition.

/*****************************************************************
XBee_Serial_Passthrough.ino

Set up a software serial port to pass data between an XBee Shield
and the serial monitor.

Hardware Hookup:
  The XBee Shield makes all of the connections you'll need
  between Arduino and XBee. If you have the shield make
  sure the SWITCH IS IN THE "DLINE" POSITION. That will connect
  the XBee's DOUT and DIN pins to Arduino pins 2 and 3.
*****************************************************************/
// We'll use SoftwareSerial to communicate with the XBee:
#include <AltSoftSerial.h>
#include <GSM.h>

#define PINNUMBER ""

//AltSoftSerial altSerial;

// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
AltSoftSerial XBee(8, 9); // RX, TX

GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

String parse="FALLEN";

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config 
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);

  Serial.println("SMS Messages Sender");

  boolean notConnected = true;

  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
}

void loop()
{
  /*if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }*/

  String content = "";
  char character;

while(XBee.available()) {
      character = XBee.read();
      content.concat(character);
      delay (10);
  }

if (content != "") {
    Serial.println(content); //Serial monitor to show content (FALLEN).

    if(content=="FALLEN"){ //Check if content is "FALLEN", if true, jump to void sos().
      Serial.println("DSTOM"); //Check if the if statement even runs.
      sos();
    }

  }

}

void sos(){

  Serial.println("DSOTM"); 
  //readSerial(remoteNumber);

 // int phone = 97308124;
  char remoteNumber[20] = "97308124";
 // String number;
 // number=String(phone);
 // number.toCharArray(remoteNumber,20);

  char txtMsg[200] = "SENTBOYS";
//  String talk = "SENTBOYS";

//  talk.toCharArray(txtMsg,200);

  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n"); 

}

I have tried different syntax of string checking including "content==parse", "content.equals(parse)" and "content=="FALLEN";

Any help will be greatly appreciated.

EDIT: I fixed it by using a character instead of a string.

//  String content = "";
  char character;

  char parse='N';

while(XBee.available()) {
      character = XBee.read();
     // content.concat(character);
      delay (10);
      Serial.print(character);
      Serial.print(character);

      if(character=='N'){

        Serial.println("DSTOM");

      }

In this case, the characters a printed 1 by 1 (with a small delay between each character).

share|improve this question
    
First things first, are you sure the read string is exactly "FALLEN" or only that it contains "FALLEN"? Adding some debug Serial.print at the end of the while(XBee.available) loop could help. – jfpoilpret Nov 8 '14 at 6:36
    
I believe the output is read character by character, then concatenated into a string. Said string is that written. – user4985 Nov 8 '14 at 9:07
    
I think what jf was getting at is that if there are any characters sent within 10ms on either side of the word FALLEN then they will be in content and the comparison will fail – BrettAM Nov 10 '14 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.