input triggers LED

I have a program where i can turn LED on by calling GPRS shield from specific number, when i call it again from that specific number LED turns off.

It looks like this:

 #include <SoftwareSerial.h> 
SoftwareSerial mySerial(9, 10);

int led = 3;
String callid;
bool change = false;
bool thisIsTheFirstTime = true;
unsigned long ledTurnedOffAt;
unsigned long ledTurnedOnAt;

void setup()
{
  mySerial.begin(9600);  
  Serial.begin(9600);    
  delay(100);

  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  if (mySerial.available()>0){
  callid = mySerial.readString();
  }else
  {
    callid = "";
  }

 if(callid.indexOf("+381601416081") > -1){
    if(change== false && (millis() - ledTurnedOffAt >= 10000 || thisIsTheFirstTime)){
       Serial.println("LED ON");
       ledTurnedOnAt = millis();
       thisIsTheFirstTime = false;
       digitalWrite(led,HIGH);
       change = true;
     } if(change == true && millis() - ledTurnedOnAt >= 10000)  {
       Serial.println("LED OFF");
       ledTurnedOffAt = millis();
       digitalWrite(led,LOW);
       change = false;
     }
  }

  delay(50);
}

I also have program with button input where when button is pressed, that same phone number is being called by GPRS shield. It looks like this:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
int buttonpin=4;
void setup()
{
  pinMode(buttonpin,INPUT);
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);// Setting the baud rate of Serial Monitor (Arduino)
  Serial.println("press button");
}

void loop()
{
  if(digitalRead(buttonpin)==HIGH)
  {
    
    Serial.println("button pressed");
    delay(2000);
    callMe();  
  }

 if (mySerial.available()>0)
 Serial.write(mySerial.read());
}

void callMe()
{
 

 mySerial.println("ATD+ +381601416081;");

}

I want to combine them and make one program but when i do, it all messes up and button press turns the LED on when it is not supposed to.

And the combined program is where?

Edit: Also post the debug output echoed back from the GPRS. When you send a message the module presumably responds with the phone number of the phone it is sending to. This is obviously erroneously triggering the code that detects the incoming call and makes a led state change.

You need to look at the messages coming back from the GPRS. Work out how to differentiate an incoming call from and outgoing one. Then disable the led state change in an outgoing call.

As I’ve said before: We don’t have your GPRS module. Only you can provide that debug output.

pcbbc:
And the combined program is where?

Edit: Also post the debug output echoed back from the GPRS. When you send a message the module presumably responds with the phone number of the phone it is sending to. This is obviously erroneously triggering the code that detects the incoming call and makes a led state change.

You need to look at the messages coming back from the GPRS. Work out how to differentiate an incoming call from and outgoing one. Then disable the led state change in an outgoing call.

As I’ve said before: We don’t have your GPRS module. Only you can provide that debug output.

This is combined program:

 #include <SoftwareSerial.h> 
SoftwareSerial mySerial(9, 10);
int buttonpin = 4;
int led = 3;
String callid;
bool change = false;
bool thisIsTheFirstTime = true;
unsigned long ledTurnedOffAt;
unsigned long ledTurnedOnAt;

void setup()
{
  mySerial.begin(9600);  
  Serial.begin(9600);    
  delay(100);
  pinMode(buttonpin, INPUT);
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  if(digitalRead(buttonpin)==HIGH) 
  {
    
    Serial.println("button pressed");
    delay(1000);
    callMe(); 
     
  }
  if (mySerial.available()>0){
  callid = mySerial.readString();
  Serial.println(callid);
  Serial.write(mySerial.read());//added from second program
  }else
  {
    callid = "";
  }

 if(callid.indexOf("+381601416081") > -1){
    if(change== false && (millis() - ledTurnedOffAt >= 10000 || thisIsTheFirstTime)){
       Serial.println("LED ON");
       ledTurnedOnAt = millis();
       thisIsTheFirstTime = false;
       digitalWrite(led,HIGH);
       change = true;
     } if(change == true && millis() - ledTurnedOnAt >= 10000)  {
       Serial.println("LED OFF");
       ledTurnedOffAt = millis();
       digitalWrite(led,LOW);
       change = false;
     }
  }

  delay(50);
}
void callMe()
{
 

 mySerial.println("ATD+ +381601416081;");

}

This happens when i press the button:

button pressed
ATD+ +381601416081;

OK

?LED ON

And then i can't turn LED off when i call it, it displays

RING

?
RING

How would i disable led state change from outgoing call?

When you detect the button press, set a flag...

sendingMessage = true;

When you are checking for the phone number...

 if(sendingMessage == false && callid.indexOf("+381601416081") > -1){

And the when you detect OK or RING received back from the module...

sendingMessage = false;

Also I suspect your delays are tripping you up. I’d suggest you follow my code from the previous thread for only togging the led on the first phone number after RING and avoid delay all together.

I have no idea why when you call are seem to be missing the caller ID from the module. You’ll have to investigate that yourself. Perhaps it only sends the ID if the number has changed? Perhaps you only showed us part of the logs? Up to you to investigate - as I said I don’t have a module...

pcbbc:
And the when you detect OK or RING received back from the module...

sendingMessage = false;

Also I suspect your delays are tripping you up. I’d suggest you follow my code from the previous thread for only togging the led on the first phone number after RING and avoid delay all together.

Now i did as you suggested and input button doesn't trigger led anymore, only problem is when i press the button and make a call after that, i don't get the ''RING'' messages anymore so i can't make bool sendMessage = false again.

I only get inverted question makrs

⸮button pressed
⸮⸮⸮⸮⸮

So i can't change the state of ''bool sendMessage'' since i don't get the RING or OK or anything after i press the button, all i get is another ''⸮" sign for every new beep. I am so close, if i could just make it send "RING" when i call it or anything except inverted question mark it would be awesome.

New program:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
int buttonpin = 4;
int led = 3;
String callid;
bool change = false;
bool newRing = false;
bool sendMessage = false;
void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
  delay(100);
  pinMode(buttonpin, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop()
{

///////
if(digitalRead(buttonpin)==HIGH)
  {
    sendMessage = true;
    Serial.println("button pressed");
    delay(1000);
    callMe();  
  }
  
  if (mySerial.available() > 0) {
    callid = mySerial.readString();
     Serial.write(mySerial.read());
    if (callid.indexOf("RING") > -1)
    {
      newRing = true;
      sendMessage = false;
    }

    if (sendMessage == false && newRing && callid.indexOf("+381601416081") > -1) {
      if (change == false)
      {
        Serial.println("LED ON");
        digitalWrite(led, HIGH);
        change = true;
      }
      else
      {
        Serial.println("LED OFF");
        digitalWrite(led, LOW);
        change = false;
      }
      newRing = false;
    }
  }
}

void callMe()
{
 

 mySerial.println("ATD+ +381601416081;");

}