Hi,
I am newbee in Arduino development.
I am trying to read charecters from software serial and comparing it with predefined string.
But, I am not able to compare the string - I mean, the comparison is not success.
I am not sure why.
Hence, I need some help from experts here.
my code is as below:
void loop() {
if(SIM900.available() >0)
{
inchar = SIM900.read();
if(inchar != '\n') {
str += inchar;
}else {
if(str == "RING") {
Serial.println("phone is ringing");
str.remove(0);
}
}
}
}
Thanks in advance for the help.
Praveen.
system
April 29, 2016, 3:36am
2
Why not print out str, and what it REALLY contains?
Thanks Paul.
I did that. I added print in the else block. I got the console o/p as below:
///////////////////////////////////
RING
+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0
NO CARRIER
///////////////////////////////////
But, still the compare check is failing.
I am not getting "phone is ringing" printed on console.
inchar might also contain e.g. '\r'. In which case str contains that as well and the compare will fail.
if (inchar != '\n' && inchar != '\r') {
...
...
When posting code, please use code tags;
** **[code]** **
your code here
** **[/code]** **
will result in
void loop()
{
if (SIM900.available() > 0)
{
inchar = SIM900.read();
if (inchar != '\n') {
str += inchar;
} else {
if (str == "RING") {
Serial.println("phone is ringing");
str.remove(0);
}
}
}
}
Thanks sterretje!.
let me try that.
Robin2
April 29, 2016, 6:59am
6
You need to post a complete program so we can see how variables are defined.
I'm guessing that your variable str is an instance of the String class. The String class can cause memory corruption in the small memory of an Arduino. Use C strings (a char array terminate with 0.
Have a look at the examples in Serial Input Basics and use the strcmp() function to compare strings.
...R
system
April 29, 2016, 2:17pm
7
I did that. I added print in the else block. I got the console o/p as below:
You got a bunch of anonymous crap that we can't make heads or tails of.
Print the String with identification AND with start and end markers:
Serial.print("The phone sent: [");
Serial.print(str);
Serial.print("]");
If str really does contain "///////////////////////////////////
RING
+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0
NO CARRIER
///////////////////////////////////", I can not, for the life oe me figure out how you expect that match "RING".
I would be far more likely to believe that str was printed more than once to show all that output.
Paul,
okay.. please find the code snippet below:
#include <arduino.h>
#include <SoftwareSerial.h>
char inchar;
SoftwareSerial SIM900(7, 8);
int numring=0;
int comring=3;
int onoff=0; // 0 = off, 1 = on
String str;
String str2 = "RING";
void setup()
{
Serial.begin(9600);
SIM900.begin(9600);
SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
delay(100);
}
void loop() {
if(SIM900.available() >0)
{
inchar = SIM900.read();
if( (inchar != '\n') ) {
str += inchar;
}else {
Serial.println(str);
if(str == "RING") {
Serial.println("phone is ringing");
str.remove(0);
}
}
}
}
and the output on the serial console is :
RING
RING
RING
+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0
RING
+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0
RING
+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0
NO CARRIER
system
April 30, 2016, 12:06am
9
You CLEARLY missed the part about printing the str value WITH identifier AND start and end markers. No one can help you until you pay attention!