Changing the message recipient's number via Arduino IDE serial monitor.

Hi guys!
I couldn’t fully grasp the commands of gsm shield and serial monitor (but I’ve read a lot about it believe me), so I have trouble with changing the recepient phone number of my messages using the arduino IDE serial monitor send thingy.
I’m new to this forum and hobby (arduino rocks btw), and I’m a newbie programmer. I’m very sorry if this is a dumb question and if my code is bad. Please help me :smiley:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String readString; 
int number;
void setup()
{
  Serial.begin(9600);
  Serial.println("Cellphone number"); 
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() > 0) {
    Serial.println(readString); 
    number = readString.toInt();  //convert readString into a number

 
  
    readString=""; //empty for next input
  } 
  SIM900.begin(19200);
  SIM900power();  
  delay(20000);  // give time to log on to network. 
}
 
void SIM900power()

{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
}
 
void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
   SIM900.println("AT + CMGS = \"+number\"");   //phone number. this is where my problem is T_T

  delay(100);
  SIM900.println("Hello, world!!!!.");        // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
  SIM900power();                                   // turn off module
}
 
void loop()
{
  sendSMS();
  do {} while (1);
}
SIM900.println("AT + CMGS = \"" + number +"\"");

Oh my. While we're at it, are my data types correct? Thanks for the quick reply! :D Gonna try this out too. BRB.

Don't convert the number to be called to a number, leave it as a String.

SIM900.println("AT + CMGS = \"" + readString +"\"");

Don't clear it before using it though!

You should really prefix it with the international dialing code too.

Oh I see.

SIM900.println("AT + CMGS = \"" +XX+ readString +"\"");

is this correct? And what do you mean by 'it'? Sorry. Just tested it and it's not sending the message. It's not probably storing the number I send in the serial monitor

Don't clear readString before you use it, which you will do with your current code.

By international dialing code I mean as in +447751123456. Adjust for your own country.

You print out the contents of readString shortly before you (did) convert it to a number. If that's correct then it should work.

If it's still not working please post your revised code.

Here is the code. I removed the conversion. Should I keep it? I can’t still send messages btw

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String readString; 
void setup()
{
  Serial.begin(9600);
  Serial.println("Cellphone number"); // so I can keep track of what is loaded
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() > 0) {
    Serial.println(readString);  //so you can see the captured string 
  

    // auto select appropriate value, copied from someone elses code.
  
    readString=""; //empty for next input
  } 
  SIM900.begin(19200);
  delay(20000);  // give time to log on to network. 
}
 

 
void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
   SIM900.println("AT + CMGS = \"" +61+readString +"\"");

  delay(100);
  SIM900.println("Hello, world. This is a text message from an Arduino!!");        // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS

}
 
void loop()
{
  sendSMS();
  do {} while (1);
}

I also noticed that the data i sent is not displayed even though it’s supposed to be captured. Is this part of the issue? thanks for your patience

Think the process though.

In setup() you have the line

while (Serial.available()) {

What happens if there is nothing waiting to be read?

So let's put a

while (!Serial.available()); before that to wait until we have something.

When printing out a variable, I Serial.print(variable); and then Serial.println("*");so I can see if there is anything - maybe a lot of spaces?

Assuming you have captured the number you wish to send a message to,

readString=""; //empty for next input

clears it.

Thanks for the help! It worked. Never expected the fix was so simple sobs

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String readString; 
void setup()
{
  Serial.begin(9600);
 
  while (!Serial.available())
  { 
      Serial.println("Cellphone number"); // i can't find a better way so here it is atm
      delay (500);
  }
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() > 0) {
    Serial.print(readString);
    readString=""; //empty for next input
  } 
  SIM900.begin(19200);
  delay(20000);  // give time to log on to network. 
}
 

 
void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \""+readString +"\"");

  delay(100);
  SIM900.println("Hello, world. This is a text message from an Arduino Uno.");        // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS

}
 
void loop()
{
  sendSMS();
  do {} while (1);
}