element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum reading from an array
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 400 subscribers
  • Views 248 views
  • Users 0 members are here
Related

reading from an array

Former Member
Former Member over 12 years ago

hey guys whats up, so i am parsing an sms message into an array, i am able to get the sms message, but i am having trouble comparing the sms to values, for eg,the sms is 9, i am getting problems comparing the value.

Below is my code

 

char character = 0;
char data[255];
int x=0;
int openingroof =53;
int closingroof =51;
int turningbeans = 52;
int buzzer = 49;
int manual_drawopenled =50;
int manual_drawcloseled = 48;
int smsled = 47;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  delay(60000);


pinMode(openingroof,OUTPUT);
pinMode(closingroof,OUTPUT);
pinMode(turningbeans,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(manual_drawopenled,OUTPUT);
pinMode(manual_drawcloseled,OUTPUT);
pinMode(smsled,OUTPUT);


Serial1.println("AT+CMGF=1");
}


void smsrecieve()
{
  
  Serial1.println("AT+CMGR=1"); //Reads the first SMS
Serial1.flush();
for (x=0;x < 255;x++){ 
data[x]='\0'; 
} 
x=0;
do{
while(Serial1.available()==0);
data[x]=Serial1.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


}


void loop()
{


smsrecieve();


if (data=="9")
{
  digitalWrite(smsled,HIGH);
  
}


}

 

What happens is data is the array and the sms message is "9". Serial printin data will also show 9. Any ideas?

  • Sign in to reply
  • Cancel
Parents
  • shabaz
    0 shabaz over 12 years ago

    Hi,

     

    (Assuming Arduino-language is the same as C), it looks like the immediate problem is that the you're trying to compare a string:

    if (data=="9")
    {
      digitalWrite(smsled,HIGH);
     
    }

    This won't give you what you're looking for. You need to do something like

    if (data[0]=='9')

    which will check the first byte. Alternatively, you can look for a string compare function.

     

    Also, the smsreceive() function looks very irregular, because you're checking things like data[x-2] when you know that x may be under-range for the array. I can see you've tried to compensate for that by defining a dummy character before you define the array.

     

    A better way to implement this sort of stuff is to create a state machine. In C one simple way consists of switch..case statements. There are some more advanced methods, but the switch..case method is fine for simple scenarios and is easy to understand. Here is how it works:

    Your states would be enumerated somewhere, and then you'd use a single byte to store your state.

    So, (for example) if you were expecting (say) the characters ", 9, ", <carriage return> (I don't know if that's what you were expecting, but

    let's use this as an example), then your possible states may be called START, INSTRING, DONESTRING, FINISHED.

    You'd use a switch(statevar) command, and have the case: statements checking for the different state values.

    You'd move to state INSTRING as soon as you see a quote arrive, but only if you were in state START initially.

    You'd move to state DONESTRING as soon as you see a quote arrive, but only if you were in state INSTRING.

    You'd move to state FINISHED whenever you saw a carriage return, regardless of state

    You'd quit the smsreceive() function whenever you reached FINISHED state.

    You get the idea..

     

    Something like:

     

    mystate=START;

    do

    {

      inchar=readchar();

      switch(mystate)

      {

        case START:

          if (inchar=='"')

            mystate=INSTRING;

          break;

        ... etc

      }

    }while(mystate!=FINISHED);

     

     

    State machines solve these types of programming problems quite well.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • shabaz
    0 shabaz over 12 years ago

    Hi,

     

    (Assuming Arduino-language is the same as C), it looks like the immediate problem is that the you're trying to compare a string:

    if (data=="9")
    {
      digitalWrite(smsled,HIGH);
     
    }

    This won't give you what you're looking for. You need to do something like

    if (data[0]=='9')

    which will check the first byte. Alternatively, you can look for a string compare function.

     

    Also, the smsreceive() function looks very irregular, because you're checking things like data[x-2] when you know that x may be under-range for the array. I can see you've tried to compensate for that by defining a dummy character before you define the array.

     

    A better way to implement this sort of stuff is to create a state machine. In C one simple way consists of switch..case statements. There are some more advanced methods, but the switch..case method is fine for simple scenarios and is easy to understand. Here is how it works:

    Your states would be enumerated somewhere, and then you'd use a single byte to store your state.

    So, (for example) if you were expecting (say) the characters ", 9, ", <carriage return> (I don't know if that's what you were expecting, but

    let's use this as an example), then your possible states may be called START, INSTRING, DONESTRING, FINISHED.

    You'd use a switch(statevar) command, and have the case: statements checking for the different state values.

    You'd move to state INSTRING as soon as you see a quote arrive, but only if you were in state START initially.

    You'd move to state DONESTRING as soon as you see a quote arrive, but only if you were in state INSTRING.

    You'd move to state FINISHED whenever you saw a carriage return, regardless of state

    You'd quit the smsreceive() function whenever you reached FINISHED state.

    You get the idea..

     

    Something like:

     

    mystate=START;

    do

    {

      inchar=readchar();

      switch(mystate)

      {

        case START:

          if (inchar=='"')

            mystate=INSTRING;

          break;

        ... etc

      }

    }while(mystate!=FINISHED);

     

     

    State machines solve these types of programming problems quite well.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube