SD conflict to Gear motor code with GSM

Hi, I'm getting a problem with my code.
I'm using GSM to trigger/turn on Gear motor and then save to SD card but the GSM automatically turn off.
note: GSM is connected to Arduino 1
and Gear motor pins and SD card are connected to Arduino 2, so im using I2C to communicate with GSM. :slight_smile:

if i tried SD or gear motor separate, they are both working fine just when i combined the two, i got GSM problem.

Here's the code for Gear Motor to turn on. no problem here.

int pwm=9;
int servobot=8;
int ldrbot=A4;
int ldrstate=0;
int old_ldrstate;  
void Setup()
{
}
Void loop()
{
}
void receiveEvent(int howMany)
{
while(Wire.available() > 0)
  {
    char c = Wire.read();
    numbers[i++] = c;
    numbers[i]='\0';
  }
 strcat(myBigArray, numbers);
    strcat(myBigArray, str);
    char letter;
if (SD.exists(myBigArray)) 
    {
      myFile = SD.open(myBigArray);
      if (myFile) {
        // read from the file until there's nothing else in it:
        while (myFile.available()) {
          ldrstate=digitalRead(ldrbot);
          ldrstate1=digitalRead(ldrbot1);
          letter =myFile.read();
          switch (letter) {
 case 'A':
    digitalWrite(servobot,HIGH);
            analogWrite(pwm,255);
            while(x<3)
            {
              old_ldrstate=ldrstate;
              ldrstate=digitalRead(ldrbot);
              if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
            }
            delay(2000);
            digitalWrite(servobot,LOW);
            analogWrite(pwm,0);

            Serial.print(" Success");
            break;

and the code for SD card that i would like to insert is this.

    switch (letter) {
 case 'A':
 myFile.close();
   myFile = SD.open("Claim/Cookies.txt", FILE_WRITE);     //Write .txt file in SD, done.
            if (myFile) {
              myFile.println("1");
              myFile.close();
            }

but when i combine Gear and SD codes, the GSM turn off.

here's the combine code i've made.

int pwm=9;
int servobot=8;
int ldrbot=A4;
int ldrstate=0;
int old_ldrstate;  
void Setup()
{
}
Void loop()
{
}
void receiveEvent(int howMany)
{
while(Wire.available() > 0)
  {
    char c = Wire.read();
    numbers[i++] = c;
    numbers[i]='\0';
  }
 strcat(myBigArray, numbers);
    strcat(myBigArray, str);
    char letter;
if (SD.exists(myBigArray)) 
    {
      myFile = SD.open(myBigArray);
      if (myFile) {
        // read from the file until there's nothing else in it:
        while (myFile.available()) {
          ldrstate=digitalRead(ldrbot);
          ldrstate1=digitalRead(ldrbot1);
          letter =myFile.read();
          switch (letter) {
 case 'A':
    digitalWrite(servobot,HIGH);
            analogWrite(pwm,255);
            while(x<3)
            {
              old_ldrstate=ldrstate;
              ldrstate=digitalRead(ldrbot);
              if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
            }
            delay(2000);
            digitalWrite(servobot,LOW);
            analogWrite(pwm,0);
// i just insert here the code of SD card so that when Gear motor is turn low, it will write to SD card.
 myFile.close();
   myFile = SD.open("Claim/Cookies.txt", FILE_WRITE);     //Write .txt file in SD, done.
            if (myFile) {
              myFile.println("1");
              myFile.close();
            }
            Serial.print(" Success");
            break;

my problem is in this loop,

 while(x<3)
            {
              old_ldrstate=ldrstate;
              ldrstate=digitalRead(ldrbot);
              if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
            }

after it reach the condition.
the GSM will turn off.

Here's the code for Gear Motor to turn on. no problem here.

void Setup()  // problem is here..!! it should be setup().
{
}

i think it is not your complete code. if not kindly post your complete code.

here's my code sir, when the motor is turned on, it has no problem, only when this code reach the condition, then the problem occur.
while(x<3)
{
old_ldrstate=ldrstate;
ldrstate=digitalRead(ldrbot);
if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
}

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
File myFile;
#include<stdio.h>
#include<String.h>
char str[5]= ".txt";
long randNumber;
char numbers[20]="";
int x=0;
int y=0;
int S1;
int S2;
char i;
int a=0;
int b=0;
int d=0;
int pwm=9;
int servobot=8;
int ldrbot=A4;
int ldrstate=0;
int old_ldrstate=0;
char s;
char buf[50];
char bufs[70];
char message[180];
void setup()
{
  pinMode(pwm,OUTPUT);
  pinMode(servobot,OUTPUT);
  pinMode(ldrbot,INPUT);
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  while (!Serial) {
    ; 
  }
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}
void loop()
{

}
void receiveEvent(int howMany)
{
  while(Wire.available() > 0)
  {
    char c = Wire.read();
    numbers[i++] = c;
    numbers[i]='\0';
  }
  if (strcmp (numbers,"A")==0) {
    while(true)
    {
      randNumber = random('11111','99999');
      sprintf(buf,"%lu.txt",randNumber);
      sprintf(bufs,"code:%lu ordered: 2 ",randNumber);
      if(!SD.exists(buf))    
      {
        myFile = SD.open(buf, FILE_WRITE);     
        if (myFile) {
          myFile.println("A");
          myFile.close();
          break; 
        }
      }
    }
  }
  if (strcmp (numbers,"B")==0) {
    while(true)
    {
      randNumber = random('11111','99999');
      sprintf(buf,"%lu.txt",randNumber);
      sprintf(bufs,"code:%lu ordered: 3 ",randNumber);
      if(!SD.exists(buf))    
      {
        myFile = SD.open(buf, FILE_WRITE);    
        if (myFile) {
          myFile.println("B");
          myFile.close();
          break; 
        }
      }
    }
  }
  if(!strcmp (message,"A") ==0 && (!strcmp (message,"B")==0))
  {
    strcat(myBigArray, numbers);
    strcat(myBigArray, str);
    char letter;
    if (SD.exists(myBigArray)) 
    {
      myFile = SD.open(myBigArray);
      if (myFile) {
        // read from the file until there's nothing else in it:
        while (myFile.available()) {
          ldrstate=digitalRead(ldrbot);

          letter =myFile.read();
          switch (letter) {
          case 'A':
            myFile.close();
            myFile = SD.open("Claim/Cookies.txt", FILE_WRITE);     //Write .txt file in SD, done.
            if (myFile) {
              myFile.println("1");
              myFile.close();
            }
            digitalWrite(servobot,HIGH);
            analogWrite(pwm,255);
            while(x<3)
            {
              old_ldrstate=ldrstate;
              ldrstate=digitalRead(ldrbot);
              if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
            }
            digitalWrite(servobot,LOW);
            reset();
            Serial.println("A Success");
            break;
          default:
            letter = '?';
            break;
          } 
          Serial.print("Letter ");  
          Serial.println(letter);
          letter='\0';
        }      

        SD.remove(myBigArray);

        Serial.print("Done!");
      }
    }
    else {
      Serial.println("example.txt doesn't exist.");
    }
    //    reset;
  }
}
void requestEvent()
{
  Wire.write(bufs);
  reset();
}
void reset()
{
  memset(buf, 0, 50);
  memset(bufs, 0, 70);
  memset(myBigArray, 0, 128);
  i='\0';
  x='\0';
  y='\0';
  numbers[0]='\0';
  randNumber='\0';
}

read the code comments.

if(!strcmp (message,"A") == 0 && (!strcmp (message,"B")== 0)) // if your condition is, messgase is not equal to A and B then do like this. if(strcmp (message,"A") != 0 && (strcmp (message,"B") != 0))
{
strcat(myBigArray, numbers); // myBigArray is not defind. Error..!!!
strcat(myBigArray, str);
char letter;
if (SD.exists(myBigArray))
{
myFile = SD.open(myBigArray);
if (myFile)
{
// read from the file until there's nothing else in it:
while (myFile.available()) {
ldrstate=digitalRead(ldrbot);

letter =myFile.read();
switch (letter) {
case 'A':
myFile.close();
myFile = SD.open("Claim/Cookies.txt", FILE_WRITE); //Write .txt file in SD, done.
if (myFile) {
myFile.println("1");
myFile.close();
}
digitalWrite(servobot,HIGH);
analogWrite(pwm,255);
while(x<3)
{
old_ldrstate=ldrstate;
ldrstate=digitalRead(ldrbot);
if(ldrstate==LOW && old_ldrstate==HIGH) x=x+1;
}
digitalWrite(servobot,LOW);
reset();
Serial.println("A Success");
break;
default:
letter = '?';
break;
}
Serial.print("Letter ");
Serial.println(letter);
letter='\0';
}
SD.remove(myBigArray); //here you are removing myBigArray so after first call your else part extecutes. because SD.open() does not getting myBigArray. i guess what you want is SD.open(pathname, MODE)
Serial.print("Done!");
}
}
else {
Serial.println("example.txt doesn't exist.");
}
// reset;
}