Go Down

Topic: Arduino Restarts? (Read 449 times) previous topic - next topic

Good Evening Gentlemen!
My problem is that the arduino restarts when i text the keyword "CHECK STATUS".
Is there something wrong with my string manipulation? And if there is, please tell me what to do.
Thank you! God Bless!
Ill post the code after this, it exceeds the max characters.

#1
Feb 23, 2013, 04:33 pm Last Edit: Feb 23, 2013, 04:38 pm by horikitamaki Reason: 1
Code: [Select]
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"

SMSGSM sms;

boolean started=false;

int pinone = 4;
int pintwo = 5;
int pinthree = 6;
int pinfour = 7;
int feedone = 8;
int feedtwo = 9;
int feedthree = 10;
int feedfour = 11;

boolean one = false;
boolean two = false;
boolean three = false;
boolean four = false;

void setup()
{

 pinMode(pinone, OUTPUT);
 pinMode(pintwo, OUTPUT);
 pinMode(pinthree, OUTPUT);
 pinMode(pinfour, OUTPUT);
 pinMode(feedone, INPUT);
 pinMode(feedtwo, INPUT);
 pinMode(feedthree, INPUT);
 pinMode(feedfour, INPUT);

 Serial.begin(9600);
 Serial.println("GSM Shield testing.");

 if (gsm.begin(9600))
 {
   Serial.println("\nstatus=READY");
   started=true;  
 }

 else Serial.println("\nstatus=IDLE");
 delay(1000);
}

void loop()
{
 char position;
 char phone_num[20] = {'\0'};
 char sms_text[100] = {'\0'};

 if (started)
 {
   position = sms.IsSMSPresent(SMS_UNREAD);
   
   if (position)
   {
     sms.GetSMS(position, phone_num, sms_text, 100);
     delay(1000);
     Serial.println("Data Received");
     delay(1000);
     Serial.println(phone_num);
     Serial.println(sms_text);
     delay(1000);
     get_cmd(sms_text);
     delay(1000);
     gsm.SimpleWrite("AT+CMGD=1,4");
     delay(1000);
     Serial.println("SMS DELETED");
     delay(1000);

   }

   clearstring_num(phone_num);
   delay(250);
   clearstring_txt(sms_text);
   delay(250);
 }

}

void clearstring_num(char *strArray_num)
{
 int j;
 for (j = 20; j > 0; j--)
 strArray_num[j] = 0x00;
}

void clearstring_txt(char *strArray_txt)
{
 int j;
 for (j = 100; j > 0; j--)
 strArray_txt[j] = 0x00;
}


void get_cmd(char *command)
{
 char sms_ctext[100] = {'\0'};
 sprintf(sms_ctext, "%s", command);
 int i;
 
 if (strcmp(sms_ctext,"ON 1") == 0 )
 {
   if(!one)
   {
     i = digitalRead(feedone);
     
     if (i == 0)
     {
       digitalWrite(pinone,HIGH);
       sms.SendSMS("+639XXXXXXX", "Port One is ON");
       delay(1000);
       Serial.println("Port One is ON");
       delay(1000);
       one = true;
     }
     
     else
     {
       sms.SendSMS("+639XXXXXXX", "Switch ONE is ON, Turn OFF the switch before proceeding.");
       delay(1000);
     }
   }
 }

 else if (strcmp(sms_ctext,"ON 2") == 0 )
 {
   if(!two)
   {
     
     i = digitalRead(feedtwo);
     
     if (i == 0)
     {
       digitalWrite(pintwo,HIGH);
       sms.SendSMS("+639XXXXXXX, "Port Two is ON");
       delay(1000);
       Serial.println("Port Two is ON");
       delay(1000);
       two = true;
     }
     
     else
     {
       sms.SendSMS("+639XXXXXXX", "Switch TWO is ON, Turn OFF the switch before proceeding.");
       delay(1000);
     }
   }
 }

 else if (strcmp(sms_ctext,"ON 3") == 0 )
 {
   if(!three)
   {
     
     i = digitalRead(feedthree);
     
     if (i == 0)
     {
       digitalWrite(pinthree,HIGH);
       sms.SendSMS("+639XXXXXXX", "Port Three is ON");
       delay(1000);
       Serial.println("Port Three is ON");
       delay(1000);
       three = true;
     }
     
     else
     {
       sms.SendSMS("+639XXXXXXX", "Switch THREE is ON, Turn OFF the switch before proceeding.");
       delay(1000);
     }
   }
 }

 else if (strcmp(sms_ctext,"ON 4") == 0 )
 {
   if(!four)
   {
     
     i = digitalRead(feedfour);
     
     if (i == 0)
     {
       digitalWrite(pinfour, HIGH);
       sms.SendSMS("+639XXXXXXX", "Port Four is ON");
       delay(1000);
       Serial.println("Port Four is ON");
       delay(1000);
       four = true;
     }
     
     else
     {
       sms.SendSMS("+639XXXXXXX", "Switch FOUR is ON, Turn OFF the switch before proceeding.");
       delay(1000);
     }
   }
 }
 
 
 
 else if (strcmp(sms_ctext,"OFF 1") == 0 )
 {
   
   i = digitalRead(feedone);
   
   if (i == 0)
   {
     sms.SendSMS("+639XXXXXXX", "Switch ONE is ON, Turn OFF the switch before proceeding.");
     delay(1000);
   }
   
   else
   {
     digitalWrite(pinone,LOW);
     sms.SendSMS("+639XXXXXXX", "Port One is OFF");
     delay(1000);
     Serial.println("Port One is Off");
     one = false;
   }
 }

 else if (strcmp(sms_ctext,"OFF 2") == 0 )
 {
   
   i = digitalRead(feedtwo);
   
   if (i == 0)
   {
     sms.SendSMS("+639XXXXXXX", "Switch TWO is ON, Turn OFF the switch before proceeding.");
     delay(1000);
   }
   
   else
   {
     digitalWrite(pintwo,LOW);
     sms.SendSMS("+639XXXXXXX", "Port Two is OFF");
     delay(1000);
     Serial.println("Port Two is Off");
     two = false;
   }
 }

 else if (strcmp(sms_ctext,"OFF 3") == 0 )
 {
   
   i = digitalRead(feedtwo);
   
   if (i == 0)
   {
     sms.SendSMS("+639XXXXXXX", "Switch THREE is ON, Turn OFF the switch before proceeding.");
     delay(1000);
   }
   
   else
   {    
     digitalWrite(pinthree,LOW);
     sms.SendSMS("+639XXXXXXX", "Port Three is OFF");
     delay(1000);
     Serial.println("Port Three is Off");
     three = false;
   }
 }
   
 else if (strcmp(sms_ctext,"OFF 4") == 0 )
 {
   
   i = digitalRead(feedtwo);
   
   if (i == 0)
   {
     sms.SendSMS("+639XXXXXXX", "Switch FOUR is ON, Turn OFF the switch before proceeding.");
     delay(1000);
   }
   
   else
   {  
     digitalWrite(pinfour,LOW);
     sms.SendSMS("+639XXXXXXX", "Port Four is OFF");
     delay(1000);
     Serial.println("Port Four is Off");
     four = false;
   }
 }
 
 else if (strcmp(sms_ctext,"CHECK STATUS") == 0 )
 {
   check_status();
   delay(1000);
 }
 
 else if (strcmp(sms_ctext,"OFF ALL") == 0 )
 {
   off_all();
   delay(1000);
 }
 
 else
 {
   sms.SendSMS("+639XXXXXXX", "Invalid Command or Message Not Read");
   delay(1000);
   Serial.println("Invalid Command/s");
   delay(1000);
 }
}

void check_status()
{
   
   int uno, dos, tres, quatro;
   
   char stat_one[25];
   char stat_two[25];
   char stat_three[25];
   char stat_four[25];
   char info[150];
   
   
   uno = digitalRead(feedone);
   switch (uno)
   {
     case 0: strcpy(stat_one, "Port One: OFF\n");
             strcat(info,stat_one);
             break;
     case 1: strcpy(stat_one, "Port One: ON\n");
             strcat(info,stat_one);
             break;
     default: break;
   }

   dos = digitalRead(feedtwo);
   switch (dos)
   {
     case 0: strcpy(stat_two, "Port Two: OFF\n");
             strcat(info,stat_two);
             break;
     case 1: strcpy(stat_two, "Port Two: ON\n");
             strcat(info,stat_two);
             break;
     default: break;
   }
   
   tres = digitalRead(feedthree);
   delay(250);
   switch (tres)
   {
     case 0: strcpy(stat_three, "Port Three: OFF\n");
             strcat(info,stat_three);
             break;
     case 1: strcpy(stat_three, "Port Three: ON\n");
             strcat(info,stat_three);
             break;
     default: break;      
   }
   
   quatro = digitalRead(feedfour);
   switch (quatro)
   {
     case 0: strcpy(stat_four, "Port Four: OFF\n");
             strcat(info,stat_four);
             break;
     case 1: strcpy(stat_four, "Port Four: ON\n");
             strcat(info,stat_four);
             break;
     default: break;
   }
   
   delay(1000);
   sms.SendSMS("+639XXXXXXX", info);
   delay(1000);
}

void off_all()
{
   int first;
   int second;
   int third;
   int fourth;

   first = digitalRead(feedone);
   delay(100);
   delay(100);
   second = digitalRead(feedtwo);
   delay(100);
   delay(100);
   third = digitalRead(feedthree);
   delay(100);
   delay(100);
   fourth = digitalRead(feedfour);
   delay(100);
   delay(100);    
   
   if ((first == 1)||(second == 1)||(third == 1)||(fourth == 1))
   {
     sms.SendSMS("+639XXXXXXX","Please Turn Off All Switches for command to take place.");
   }
 
   else
   {
     digitalWrite(pinone, LOW);
     one = false;
     delay(500);
     digitalWrite(pintwo, LOW);
     two = false;
     delay(500);
     digitalWrite(pinthree, LOW);
     delay(500);
     three = false;
     digitalWrite(pinfour, LOW);
     four = false;
     delay(500);
     delay(500);
     sms.SendSMS("+639XXXXXXX","All Ports are now OFF");
     delay(1000);
    }
}



Arrch

Considering check_status() puts another 250 bytes onto the stack, I'm guessing you're running out of memory. Search the forum for "free memory" to check how much memory you have left before you call the function.

Any ways on how can i save RAM?

zoomkat


Any ways on how can i save RAM?


You might try the F() macro like below.

Code: [Select]
sms.SendSMS("+639XXXXXXX", "Switch THREE is ON, Turn OFF the switch before proceeding.");

to

Code: [Select]
sms.SendSMS(F("+639XXXXXXX", "Switch THREE is ON, Turn OFF the switch before proceeding."));
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
Any ways on how can i save RAM?

Why do you make a copy of the literal string?

Code: [Select]
      case 0: strcpy(stat_one, "Port One: OFF\n");
              strcat(info,stat_one);

could be:
Code: [Select]
      case 0: strcat(info, "Port One: OFF\n");
That saves 100 bytes there.

Since info contains nothing before you append data to it, there is no reason for info to by 6 times as large as the message to be appended. At most, it needs to be 4 times as large.

Thank you!

By the way, what does this F() do?


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy