the code make my Arduino reset every loop???

I don’t understand why -
I have ask here once, and someone told me that I need to clean the memory - so I think I did it, but if someone can take a look and see what I did wrong -

#include <SoftwareSerial.h>
#include <stdio.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int ok_csq_pin=7;
int send_msg_pin=6;
int redled=13;
int greenled=12;
int time=1;
int low=0;
int high=0;
long startTime;                   // to store the start of the timer
long interval = 180000;           // defalt interval of sending msg in milliseconds (~~  3 min)
void setup()  
{
  pinMode(ok_csq_pin,INPUT);
  pinMode(send_msg_pin,INPUT);
  pinMode(redled,OUTPUT);
  pinMode(greenled,OUTPUT);
  Serial.begin(19200); // Open serial communications and wait for port to open:
  digitalWrite(greenled,HIGH);
  digitalWrite(redled,HIGH);
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  delay(1500);
  digitalWrite(greenled,LOW);
  digitalWrite(redled,LOW);
  startTime = millis(); //set startTime
  Serial.println("Modem connection is on!");
}

void loop ()
{
  if(millis() - startTime < interval) 
  { //What to do if didn't pass 10 min

    if (digitalRead(send_msg_pin)==HIGH)
    {
      Serial.println(digitalRead(send_msg_pin));
      sbdrt();
      run_time();
      Serial.println(millis()/60000);
      startTime = millis();
    }

  }
  else 
  { //3 min is done - need to send auto msg 

    Serial.println("Sending automatic msg - 3 min is pass");
    Serial.print("OLD Delay time is - ");
    Serial.println(interval);
    sbdrt(); // only see msg 
    startTime=millis();
    Serial.println(millis()/60000);

    Serial.print("The new delay time is - ");
    Serial.println(interval);

  }//end of auto sending msg


}//end of loop


void sbdrt()
{//start
  delay(500);
  Serial.println("sending AT+sbdrt");
  mySerial.println("AT+sbdrt");
  delay(5000);
  Serial.println(mySerial.available());
  int s=mySerial.available();
  char rt_full[s];
  if  (mySerial.available()>s-2)
  {//start of  answer

    for (int sbdrt_num=0;sbdrt_num<s;sbdrt_num++)
    {//start reading 
      rt_full[sbdrt_num]=mySerial.read();
      Serial.print(sbdrt_num);
      Serial.print("    ");
      Serial.println(rt_full[sbdrt_num]);
      rt_full[s]='\0';

    }//end of reading
    Serial.println("this is the full response - ");
    Serial.println(rt_full);

    char * pch_sbrt;
    int i=0;
    char *david_sbrt[2];
    int answer_sbrt[2];
    pch_sbrt = strtok( rt_full, ":");
    while (pch_sbrt !=NULL)  
    {
      pch_sbrt=strtok(NULL,",");
      david_sbrt[i]=pch_sbrt;
      i++;

    }// end of cutting the answer

    for (int y=0;y<2;y++)
    {
      answer_sbrt[y]=atoi(david_sbrt[y]);
    }

    Serial.println("this is the final answer");
    for (int v=0;v<2;v++)
    {//printing the int value of my msg 

      Serial.print(v);
      Serial.print("  -  " );
      Serial.println(answer_sbrt[v]);

    } 

    Serial.print("This is the msg   -  " );
    Serial.println(answer_sbrt[0]);
    Serial.print("This is the delay time in minuts - ");
    Serial.println(answer_sbrt[1]);
    long new_delay=60000*(answer_sbrt[1]);
    Serial.println("*********");
    Serial.print("The new dealy time in miilsec is -");
    Serial.println(new_delay);
    Serial.println("*********");
    if (new_delay!=0)
    {
      Serial.println("new delay  ");
       interval=new_delay;
    }
   
    if (answer_sbrt[0]==6)
    {
      Serial.println("the msg recive - green ");
      digitalWrite(greenled,HIGH);
      delay(1500);
      digitalWrite(greenled,LOW);
    }
    else if (answer_sbrt[0]==8)
    {
      Serial.println("the msg recive - red ");
      digitalWrite(redled,HIGH);
      delay(1500);
      digitalWrite(redled,LOW);

    }
    for (int temp=0;temp<s;temp++)//cleaning the memory
    {
      rt_full[temp]=NULL;
    }
     for (int temp1=0;temp1<2;temp1++)//cleaning the memory
    {
      answer_sbrt[temp1]=NULL;
    }
    memset(rt_full,0,s); //clean the memory 
    memset(answer_sbrt,'\n',2); //clean the memory 
  

    Serial.println("*****end of sbdrt******");

  }//end of reading 


}//end of sbdrt

and this is the output

Ready to work!
Modem connection is on!
Sending automatic msg - 10 min is pass
OLD Delay time is - 30000
sending AT+sbdrt
30
0 A
1 T
2 +
3 s
4 b
5 d
6 r
7 t
8

9

10

11

12 +
13 S
14 B
15 D
16 R
17 T
18 :
19

20

21 6
22 ,
23 2
24

25

26 O
27 K
28

29

this is the full response -
AT+sbdrt

+SBDRT:
6,2
OK

this is the final answer
0 - 6
1 - 2
This is the msg - 6
This is the delay time in minuts - 2


The new dealy time in miilsec is -120000


new delay
the msg recive - green
***Ready to work!

Thanks ,

 for (int temp=0;temp<s;temp++)//cleaning the memory
    {
      rt_full[temp]=NULL;
    }
     for (int temp1=0;temp1<2;temp1++)//cleaning the memory
    {
      answer_sbrt[temp1]=NULL;
    }
    memset(rt_full,0,s); //clean the memory 
    memset(answer_sbrt,'\n',2); //clean the memory

If you managed your strings properly, you wouldn’t need this junk.
A string can be terminated as you build it, and reset with a single write of zero to its first element.

o.k. so what I need to do? remove this part? - how do I know I mange my string correctly?

thanks .

int s=mySerial.available();
  char rt_full[s];
...
      rt_full[s]='\0';

Not like that, that’s for certain.
An ‘s’ element array does not have a subscript ‘s’

I'm sorry I don't understand what does it mean can you explain in simple English - what do you mean ,and what I need to understand

Thank .

OK, imagine I declare an array:

int myArray [5];

The array has 'n' = 5 elements, and the elements are numbered 0, 1, 2, 3 and 4. But not 5.

So, if I write myArray [5] = 42;, I would be doing something very silly.

of course ,but I don't do this (am I???) where I put data into place that isn't "belong to me"?

,but I don't do this

Of course you do - see reply #3.

didn't see it, and I have look and look so many times..

Thanks you for now ! I will put this part of the code in my all program and see how it's work. (and also look for mistakes like this again )

Thank

  • now you have 20000 messages because of me :-)

I only picked-out the most obvious one to me - there may be others still in there.

(Yup, that’s 20k posts, but I bet if I found and deleted all my redirection posts for misplaced threads, “please use code tags”, “don’t cross-post” etc, I’d have about 10k :frowning: )

Congrats on 20000 posts, AWOL!