String value not passed on

I try to program a mobile phone, using a Siemens TC35 module and Arduno Uno.

When passing a phone number manually to “Telefonnummer” it works properly.

Now I want to use a 3x4 keypad. As well, this works, at least I see the number in the serial monitor. However, when I pass the value to ATD_Telefonnummer it does not work, and the variable only contains “ATD”. Any idea why? Thanks, from rainy Munich.

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3

SoftwareSerial gsmSerial(rxPin, txPin);
char recu[150];    // Array for message
String Telefonnummer;
String ATD_Telefonnummer;
int Checkend;
int i;
int Checkend_True;

//Ab hier Keypad **************************************
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//*****************************************************


void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  Serial.begin(9600);
  while(!Serial) {}
  Telefonnummer = "";
  ATD_Telefonnummer = "";

  //--- turn on TC35 ---
  // wire pin 8 Arduino to IGT pin on TC35
  // ground pin for 100 ms - this is the same as pressing the button on the TC35 to start it up
  pinMode(8, INPUT);
  digitalWrite(8, LOW);
  pinMode(8, OUTPUT);
  delay(100);
  pinMode(8, INPUT); 

  gsmSerial.begin(9600);
  delay(5000);
  Serial.println("Ready");
  Serial.println();
  Serial.println("Telefonnummer eingeben, und mit * abschliessen");
  Serial.println();
  Serial.println("Auflegen mit #");
  Serial.println();
}

void loop() {
  char key = kpd.getKey();
  if (key != 42)
  {
    Telefonnummer += char(key);
  }
  Serial.print("Telefonnummer: ");
  Serial.println(Telefonnummer);
 
  // 42 is ASCII code for "*"
  if (key == 42) {
    digitalWrite(13, HIGH);
    Serial.println("Anruf starten...");
    Serial.print("Telefonnummer: ");
    Serial.println(Telefonnummer);
    ATD_Telefonnummer = String("ATD" + Telefonnummer + ";");
    Serial.print("ATD_Telefonnummer: ");
    Serial.println(ATD_Telefonnummer);
  
    gsmSerial.println(ATD_Telefonnummer);
    Serial.println("Klingelingeling ... ");
    Checkend = "";

    while (Checkend != 35){
      key = kpd.getKey();
      Checkend = key;
      if (Checkend == 35) {
        Serial.println("Auflegen ... ");
        Checkend_True = 1;
        break;
      }
    }
      
    if (Checkend_True == 1) { 
      gsmSerial.println("ATH"); //auflegen
      Serial.println("Aufgelegt ... ");
      delay(5000);
      digitalWrite(13, LOW);
      Checkend_True = 0;
    }
    delay(1000);
    Telefonnummer = "";
    ATD_Telefonnummer = "";
    i = 0;
    Serial.println();
    Serial.println();
    Serial.println("Ready");
    Serial.println();
    Serial.println("Telefonnummer eingeben, und mit * abschliessen");
    Serial.println();
    Serial.println("Auflegen mit #");
    Serial.println();
  }
}
  char key = kpd.getKey();
  if (key != 42)
  {
    Telefonnummer += char(key);
  }

When no key is pressed, key contains 0. 0 is NOT 42, so you just stuffed a NULL in the String. When you print a String, or a string, the NULL says "Hey, this is the end of the data. We are done".

Is that what you REALLY want?

D’oh. That was it. Problem is fixed and all works now. Thanks Paul !!!

Here’s the code that works, next is to connect an OLED display for the phone:

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3

SoftwareSerial gsmSerial(rxPin, txPin);
char recu[150];    // Array for message
String Telefonnummer;
String ATD_Telefonnummer;
int Checkend;
int i;
int Checkend_True;

//Ab hier Keypad **************************************
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//*****************************************************


void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  Serial.begin(9600);
  while(!Serial) {}
  Telefonnummer = "ATD";
  ATD_Telefonnummer = "";

  //--- turn on TC35 ---
  // wire pin 8 Arduino to IGT pin on TC35
  // ground pin for 100 ms - this is the same as pressing the button on the TC35 to start it up
  pinMode(8, INPUT);
  digitalWrite(8, LOW);
  pinMode(8, OUTPUT);
  delay(100);
  pinMode(8, INPUT); 

  gsmSerial.begin(9600);
  delay(5000);
  Serial.println("Ready");
  Serial.println();
  Serial.println("Telefonnummer eingeben, und mit * abschliessen");
  Serial.println();
  Serial.println("Auflegen mit #");
  Serial.println();
}

void loop() {
  char key = kpd.getKey();
  if (key){
    if (key != 42)
    {
      Telefonnummer += char(key);
    }
  }
  Serial.print("Telefonnummer: ");
  Serial.println(Telefonnummer);
  
  // 42 is ASCII code for "*"
  if (key == 42) {
    digitalWrite(13, HIGH);
    Serial.println("Anruf starten...");
    ATD_Telefonnummer = Telefonnummer + ";";
    Serial.print("ATD_Telefonnummer: ");
    Serial.println(ATD_Telefonnummer);
  
    gsmSerial.println(ATD_Telefonnummer);
    Serial.println("Klingelingeling ... ");
    Checkend = "";

    while (Checkend != 35){
      key = kpd.getKey();
      Checkend = key;
      if (Checkend == 35) {
        Serial.println("Auflegen ... ");
        Checkend_True = 1;
        break;
      }
    }
      
    if (Checkend_True == 1) { 
      gsmSerial.println("ATH"); //auflegen
      Serial.println("Aufgelegt ... ");
      delay(5000);
      digitalWrite(13, LOW);
      Checkend_True = 0;
    }
    delay(1000);
    Telefonnummer = "ATD";
    ATD_Telefonnummer = "";
    i = 0;
    Serial.println();
    Serial.println();
    Serial.println("Ready");
    Serial.println();
    Serial.println("Telefonnummer eingeben, und mit * abschliessen");
    Serial.println();
    Serial.println("Auflegen mit #");
    Serial.println();
  }
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R