Variable to Array

SMSNumber = (textMessage.substring(9,22));
    String message = "Unit now configured to " + SMSNumber;
    // start of EEPROM Write

    char Str4[] = "+441234567890";       <-------- I need to pass the SMSNumber to str4[] instead of the "+441234567890"
    for (int i = 0; i < sizeof(Str4); i++){
        EEPROM.write(i, Str4[i]);
        delay(50);
    
        if (i == EEPROM.length()) {
          i = 0;
        }
      }

    // end of EEPROM Write

I need help on passing an array a string. I know there is an issue with doing this.

The above works perfectly but I need to pass the string SMSNumber to the array instead.

I have tried to find out what I need to do but not getting anywhere fast.

It’s the only thing I can’t get working in all the program.

Much appreciated.

What is the data type of SMSNumber. You do say string but you seem to use String and string interchangeably. The trouble is that string and String are very different things.

If SMSNumber is a String, you can convert it to a string with the toCharArray() function then use strcpy() to put it into the string Str4. You will have to declare Str4 with enough elements to hold the converted SMSNumber.

You are better off not using Strings if possible because they can easily cause memory corruption. See the evils of Strings.

Thank you for your help. I will look into what you are saying.

I have given my code so far, as I am using this with GSM SIM900 but need to save the SMSNumber into an array.

String SMSNumber;

if(SIM900.available()>0){
    textMessage = SIM900.readString();
    delay(10);
    CMDNumber = (textMessage.substring(9,22));
    Serial.print(textMessage);
    Serial.print(" CMD Number > "); Serial.println(CMDNumber); 
    Serial.print(" SMS Number > "); Serial.println(SMSNumber);
    delay(10);
  } 
  if (CMDNumber == SMSNumber) {
    if(textMessage.indexOf("ON")>=0 || textMessage.indexOf("On")>=0){
      // Turn on relay and save current state
      digitalWrite(relay, HIGH);
      lampState = "on";
      Serial.println("Relay set to ON");  
      textMessage = "";   
    }
    if(textMessage.indexOf("OFF")>=0 || textMessage.indexOf("Off")>=0){
      // Turn off relay and save current state
      digitalWrite(relay, LOW);
      lampState = "off"; 
      Serial.println("Relay set to OFF");
      textMessage = ""; 
    }
    if(textMessage.indexOf("STATE")>=0 || textMessage.indexOf("State")>=0){
      String message = "LED is " + lampState;
      sendSMS(message);
      Serial.println("LED state resquest");
      textMessage = "";
    }
  }
    // Set telephone number
  if(textMessage.indexOf("Configure 777")>=0) {
    delay(10);
    SMSNumber = (textMessage.substring(9,22));
    String message = "Unit now configured to " + SMSNumber;
    // start of EEPROM Write

    char Str4[] = "+441234567890";                 <----- I need this to be a variable (SMSNumber)
    for (int i = 0; i < sizeof(Str4); i++){
        EEPROM.write(i, Str4[i]);
        delay(50);
    
        if (i == EEPROM.length()) {
          i = 0;
        }
      }

    // end of EEPROM Write

//    sendSMS(message);
    Serial.print("This is the textMessage variable > "); Serial.print(SMSNumber);
    textMessage = "";
  }

Something like this shouild work:

  // start of EEPROM Write
  int i;
  for (i = 0; i < SMSNumber.length(); i++)
  {
    EEPROM.write(i, SMSNumber[i]);
  }
  EEPROM.write(i, '\0'); // Add null terminator
  // end of EEPROM Write

You are a genius johnwasser....

Worked perfect.

Thank you!!!