SERVO doesn't reponse SMS

  • Arduino Uno R3
  • Servo MG995
  • Shield SIM900

i have a project that the goal is the servo could responses to a command that i send via SMS.
example : when i send SMS with text “SERVO”, so the servo will rotating to a degree that i set in the code.
but i dont know why my servo doesnt response to the SMS, serial monitor showing mark question (???), and the servo do nothing.

when i send SMS with a command :

  1. if i dont attach the servo to the shield (just attach LED), LED responses the SMS and turn on
  2. if i attach the servo and LED to the shield at the same time, LED doesnt response the SMS and showing mark question (???) on serial monitor
  3. if i attach only the servo, servo doesnt response and showing mark question (???) on serialmonitor

here’s my code :

/*

#include <Servo.h>
#include <SoftwareSerial.h>

SoftwareSerial sim900(7, 8);
#define led1 2

Servo myservo;
int pos = 0;

#define gsm_sw 9

bool myNumber;
int state;
String sms;

void send_sms(String text)
{
  sim900.println("AT + CMGS = \"+6285777200677\"");
  delay(500);
  sim900.println(text);
  delay(500);
  sim900.println((char)26);
  delay(500);
  sim900.println();
}

int test_gsm()
{
  int fail=0;
  
  sim900.println("ATE0\r");
  if(sim900.find("OK")) 
  {
    Serial.println("No Echo OK");
  }
  else{
      fail++;
  }  
  delay(500);

  sim900.print("AT+CMGF=1\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Text Mode OK");  
  }
  else{
      fail++;
  }
  delay(500);


  sim900.print("AT+CNMI=2,2,0,0\r");    
  if(sim900.find("OK")) 
  {
    Serial.println("Setting GSM OK");  
  }
  else{
      fail++;
  }
  delay(500);

  sim900.print("AT+CMGD=1,4\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Delete Memory OK");  
  }
  else{
      fail++;
  }
  delay(2000);

  return fail;
}

void setup() {
  sim900.begin(9600);
  Serial.begin(9600);

    pinMode(led1,OUTPUT);

  myservo.attach(9);
  pinMode(gsm_sw,OUTPUT);
  
 digitalWrite(led1,LOW);
  digitalWrite(gsm_sw,LOW);
  
  int i,temp;
  for(i=0; i<3; i++)
  {
    Serial.println("Test GSM");
    temp=test_gsm();
    if(temp<4) break;

    if(temp==0)
    {
      digitalWrite(gsm_sw,HIGH);
      delay(1500);
      digitalWrite(gsm_sw,LOW);      
    }
  }

  if(i==3) Serial.println("Tidak Dapat Komunikasi Dengan GSM");
  if(i<3) Serial.println("Connect GSM SIM900");
}

void loop() {
  // put your main code here, to run repeatedly:
  
  while(sim900.available())
  {
    char temp1 = (char)sim900.read();
    sms+=temp1;

    if(temp1=='\n') 
    {
      state++;

      if(state==1) sms="";
    }

    if(sms.indexOf("+CMT: \"+6285777200677\"")>-1)    
    {
      sms="";
      state=0;
      myNumber=true;
    }

    Serial.print(temp1);
  }

  if(myNumber && state>=2)
  {
    myNumber=false;

    if(sms.indexOf("SERVO")>-1)
    {
       for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);              
    delay(15);                      
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);             
    delay(15);                      
  }
    }
    else if(sms.indexOf("Led1on")>-1)
    {
      digitalWrite(led1,LOW);
      sms="";
      send_sms("Led 1 Turn On");
    }
    else
    {
      sms="";
      send_sms("SMS WRONG");    
    }
  }  
}

I have never had to deal with it myself, but there are many threads talking about softwareserial not being compatible with servo.

Here is a thread with a proposed solution, ServoTimer2

thank you for the answer, i'll try

my servo wont rotating after i send the SMS command to SIM900, and showing a lot of question mark on serial monitor, anyone know what the problem is? thank you.

-Arduino Uno R3
-SIM900
-SERVO MG995

here is my code :
i set SERVO as the command

[b]#include <Servo.h>[/b]
#include <SoftwareSerial.h>

SoftwareSerial sim900(7, 8);
#define led1 2

[b]Servo myservo;[/b]
int pos = 0;

#define gsm_sw 9

bool myNumber;
int state;
String sms;

void send_sms(String text)
{
  sim900.println("AT + CMGS = \"+6285777200677\"");
  delay(500);
  sim900.println(text);
  delay(500);
  sim900.println((char)26);
  delay(500);
  sim900.println();
}

int test_gsm()
{
  int fail=0;
  
  sim900.println("ATE0\r");
  if(sim900.find("OK")) 
  {
    Serial.println("No Echo OK");
  }
  else{
      fail++;
  }  
  delay(500);

  sim900.print("AT+CMGF=1\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Text Mode OK");  
  }
  else{
      fail++;
  }
  delay(500);


  sim900.print("AT+CNMI=2,2,0,0\r");    
  if(sim900.find("OK")) 
  {
    Serial.println("Setting GSM OK");  
  }
  else{
      fail++;
  }
  delay(500);

  sim900.print("AT+CMGD=1,4\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Delete Memory OK");  
  }
  else{
      fail++;
  }
  delay(2000);

  return fail;
}

void setup() {
  sim900.begin(9600);
  Serial.begin(9600);

    pinMode(led1,OUTPUT);

  [b]myservo.attach(9);[/b]
  pinMode(gsm_sw,OUTPUT);
  
 digitalWrite(led1,LOW);
  digitalWrite(gsm_sw,LOW);
  
  int i,temp;
  for(i=0; i<3; i++)
  {
    Serial.println("Test GSM");
    temp=test_gsm();
    if(temp<4) break;

    if(temp==0)
    {
      digitalWrite(gsm_sw,HIGH);
      delay(1500);
      digitalWrite(gsm_sw,LOW);      
    }
  }

  if(i==3) Serial.println("Tidak Dapat Komunikasi Dengan GSM");
  if(i<3) Serial.println("Connect GSM SIM900");
}

void loop() {
  // put your main code here, to run repeatedly:
  
  while(sim900.available())
  {
    char temp1 = (char)sim900.read();
    sms+=temp1;

    if(temp1=='\n') 
    {
      state++;

      if(state==1) sms="";
    }

    if(sms.indexOf("+CMT: \"+6285777200677\"")>-1)    
    {
      sms="";
      state=0;
      myNumber=true;
    }

    Serial.print(temp1);
  }

  if(myNumber && state>=2)
  {
    myNumber=false;

    [b]if(sms.indexOf("SERVO")>-1)
    {
       for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);              
    delay(15);[/b]

The code you posted is not complete.

...R

Robin2:
The code you posted is not complete.

…R

here’s my code

/*


Arduino                   SIM900
7 (RX Software Serial)    TX       
8 (TX Software Serial)    RX
9                         Power up (harus di jumper menggunakan resistor dibeberapa tipe sim900)
*/

#include <SoftwareSerial.h>
SoftwareSerial sim900(7, 8);
#include <Servo.h>

#define led1 2
#define led2 3
#define led3 4
#define led4 5

// Create a servo object 
Servo Servo1;
Servo Servo2; 

#define gsm_sw 9

bool myNumber;
int state;
String sms;

void send_sms(String text)
{
  sim900.println("AT + CMGS = \"+6285777200677\"");    //harus menggunakan kode negara, contoh +6287654321098
  delay(500);
  sim900.println(text);
  delay(500);
  sim900.println((char)26);
  delay(500);
  sim900.println();
}

int test_gsm()
{
  int fail=0;
  
  sim900.println("ATE0\r");
  if(sim900.find("OK")) 
  {
    Serial.println("No Echo OK");
  }
  else{
      fail++;
  }  
  delay(500);

  sim900.print("AT+CMGF=1\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Text Mode OK");  
  }
  else{
      fail++;
  }
  delay(500);


  sim900.print("AT+CNMI=2,2,0,0\r");    
  if(sim900.find("OK")) 
  {
    Serial.println("Setting GSM OK");  
  }
  else{
      fail++;
  }
  delay(500);

  sim900.print("AT+CMGD=1,4\r");   
  if(sim900.find("OK")) 
  {
    Serial.println("Delete Memory OK");  
  }
  else{
      fail++;
  }
  delay(2000);

  return fail;
}

void setup() {
  Servo1.attach(5); 
  Servo2.attach(6); 
  
  sim900.begin(9600);
  Serial.begin(9600);

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(gsm_sw,OUTPUT);
  
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
  digitalWrite(gsm_sw,LOW);
  
  int i,temp;
  for(i=0; i<3; i++)
  {
    Serial.println("Test GSM");
    temp=test_gsm();
    if(temp<4) break;

    if(temp==0)
    {
      digitalWrite(gsm_sw,HIGH);
      delay(1500);
      digitalWrite(gsm_sw,LOW);      
    }
  }

  if(i==3) Serial.println("Tidak Dapat Komunikasi Dengan GSM");
  if(i<3) Serial.println("Connect GSM SIM900");
}

void loop() {
  // put your main code here, to run repeatedly:
  
  while(sim900.available())
  {
    char temp1 = (char)sim900.read();
    sms+=temp1;

    if(temp1=='\n') 
    {
      state++;

      if(state==1) sms="";
    }

    if(sms.indexOf("+CMT: \"+6285777200677\"")>-1)    //harus menggunakan kode negara, contoh +6287654321098
    {
      sms="";
      state=0;
      myNumber=true;
    }

    Serial.print(temp1);
  }

  if(myNumber && state>=2)
  {
    myNumber=false;

    if(sms.indexOf("pakan")>-1)
    {
      Servo1.write(90); //atur derajat 
      delay(2000); //atur waktu
      sms="";
      send_sms("Pakan Sudah Diberikan");
    }
    //else if(sms.indexOf("Led1off")>-1)
    //{
      //digitalWrite(led1,LOW);
      //sms="";
      //send_sms("Led 1 Sudah Mati");
    //}
    else if(sms.indexOf("Minum")>-1)
    {
      Servo2.write(-90); //atur derajat 
      delay(2000); //atur waktu
      Servo2.write(90); //atur derajat 
      
      sms="";
      send_sms("Minum Sudah Diberikan");
    }
    //else if(sms.indexOf("Led2off")>-1)
    //{
      //digitalWrite(led2,LOW);
      //sms="";
      //send_sms("Led 2 Sudah Mati");
    //}
    //else if(sms.indexOf("Led3on")>-1)
    //{
      //digitalWrite(led3,HIGH);
      //sms="";
      //send_sms("Led 3 Sudah Menyala");
    //}
    //else if(sms.indexOf("Led3off")>-1)
    //{
      //digitalWrite(led3,LOW);
      //sms="";
      //send_sms("Led 3 Sudah Mati");
    //}
    //else if(sms.indexOf("Led4on")>-1)
    //{
      //digitalWrite(led4,HIGH);
      //sms="";
      //send_sms("Led 4 Sudah Menyala");
    //}
    //else if(sms.indexOf("Led4off")>-1)
    //{
      //digitalWrite(led4,LOW);
      //sms="";
      //send_sms("Led 4 Sudah Mati");
    //}
    else if(sms.indexOf("Pakan dan Minum")>-1)
    {
      Servo1.write(90); //atur derajat 
      //delay(2000); //atur waktu
      Servo2.write(90); //atur derajat 
      delay(2000); //atur waktu
      sms="";
      send_sms("Semua Pakan dan Minum Sudah Diberikan");
    }
    //else if(sms.indexOf("Ledalloff")>-1)
    //{
      //digitalWrite(led1,LOW);
      //digitalWrite(led2,LOW);
      //digitalWrite(led3,LOW);
      //digitalWrite(led4,LOW);
      //sms="";
      //send_sms("Semua Led Sudah Mati");
    //}
    else
    {
      sms="";
      send_sms("SMS WRONG");    
    }
  }  
}

when i attach the servo with a LED, and i send SMS turnon LED, the LED wont turn on
when i attach the servo only, and i send SMS to rotate the servo, the servo wont rotate (do nothin)
when i attach the LED only, and send SMS to turn on the LED, the LED turn on

when i attach the servo with a LED

What does that mean?

In the other thread you posted about your similar problems, I suggested you try a different servo library, since there are reported conflicts between servo.h and softwareserial.h

You were going to try out servotimer2.h

How that that attempt go?

@ramadhaniricky1, if you have been double posting without even including a link to your other Thread how can I tell what has been going on. Seems like a complete waste of my time.

...R