Pages: [1]   Go Down
Author Topic: Problem compare char with data array  (Read 569 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I asking for help to compare two values "data" or "s" with an incomming SMS. I have an incomming SMS contain a "1". The Serial monitor shows '1' from data, same for s shows '1' . But nothing happens. I want to jump into the if{ } when true (incomming data equal to '1'.)
I already tried different setups, but still same. Any idea why?

Code:
char data[256];
char s;

void setup()
{
    Serial.begin(9600);
    cell.begin(9600);
    delay(2000);
 }

void loop()
{
   cell.println("AT+CMGR=1");    //Reads the first SMS
   Serial.flush();
     for (x=0;x < 1;x++)
       {           
         data[x]='\0';                       
       }
        x=0;
        do
        {
          while(cell.available()==0);
          data[x]=cell.read(); 
          x++;           
           if(data[x-1]==0x0D&&data[x-2]=='"')
            {
              x=0;
            }
        }
        while(!(data[x-1]=='K'&&data[x-2]=='O'));

        data[x-3]='\0';             //finish the string before the OK
        Serial.println(data);    //shows the message
        char s = data[1];
        Serial.println(s);
        if(data[1] == 1)
        {
          Serial.println("TURN ON");
        }     
      delay(5000);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd prefer to see code that can compile, or the disclaimer "pseudo code"

Code:
if(data[x-1]==0x0D&&data[x-2]=='"')
Is your space bar broken?

Do you think it OK to read data before your array start?
« Last Edit: May 04, 2013, 06:14:33 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is not fully shown. It compile without errors and shows a '1' in the serial monitor when receive the SMS. So everything works. The problem is, I can't compare the incomming SMS first character date[1] with 1 in the if statement. I don't know why.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We can't know why, either, without seeing all your actual code.

-br
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code is not fully shown.
My answer isn't either.
Code:
if(data[1] == 1)

Code:
for (x=0;x < 1;x++)
       {            
         data[x]='\0';                        
       }
Why?
« Last Edit: May 04, 2013, 06:39:06 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my full code.
Code:
#include <SoftwareSerial.h>
#include <string.h>

SoftwareSerial cell(2, 3);

int n_sms,x,sms_start;
char data[256];
char s;

void setup()
{
    Serial.begin(9600);
    cell.begin(9600);
    delay(2000);
             
    for (int i=0;i < 5;i++)   //Wait until module is running
    {
      Serial.print(".");
      delay(3000);
    }
   
    Serial.println();
    cell.println("AT+CMGF=1");         // sets the SMS mode to text
    delay(1000);
    cell.println("AT+CMGD=1");        //Delete the first SMS after reading it
    Serial.println("System is running");
}

void loop()
{
   cell.println("AT+CMGR=1");    //Reads the first SMS
   Serial.flush();
     for (x=0;x < 25;x++)
       {           
         data[x]='\0';                       
       }
        x=0;
        do
        {
          while(cell.available()==0);
          data[x]=cell.read(); 
          x++;           
           if(data[x-1]==0x0D&&data[x-2]=='"')
            {
              x=0;
            }
        }
        while(!(data[x-1]=='K'&&data[x-2]=='O'));  // Skip the OK from SMS

        data[x-3]='\0';             //finish the string before the OK
        Serial.println(data);    //shows the message
        char s = data[1];         // Move first characters of incomming data[1] to s.
        Serial.println(s);         // print the value of s
        if(data[1] == 1)          // Compare if data[1] is equal to '1'
        {
          Serial.println("TURN ON");  // If value from data[1] is true (=1) then print.
        }
       delay(5000);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(data[1] == 1)
1 is not the same as '1'
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PROBLEM SOLVED!

I changed the if statement to following:
if ((( char) data[1] ) == '1');

It works!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's no need to cast something to the same type it already is.

That semicolon doesn't belong there, so I'm assuming you haven't fixed it.

You also need to revisit the code that reads before the start of your array.
That sort of stuff bites you on the derrière sooner or later.
« Last Edit: May 04, 2013, 07:55:11 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: