Serial port monitor input

Hello guys,
I’m quite new to Arduino and tried to write a simple English-to-Morse translator, but for some reasons the serial port monitor just goes the first line over and over again. Could someone please explain what’s done wrong?

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

void loop()
{  
  Serial.print("Enter yor message (max. 100 chracters), type '.' to termiante:\n");
  char * returned_text_array = read_text();
  translate(returned_text_array);

}

//FUNCTIONS

int * translate(char * returned_text_array) //1 means dot, 2 means dash
{
  int * translation = (int*)malloc(100);
  char letter_array[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  int values_array[26] = {12,2111,2121,211,1,1121,221,1111,11,1222,212,1211,22,21,222,1221,2212,121,111,2,112,1112,122,2112,2122,2211};

  for(int i=0; i<100; i++)
  {
    for(int j=0; j<26; j++)
    {
        if(returned_text_array[i] == letter_array[j])
          translation[i] = values_array[j];
    }
  }
return translation;
}

char * read_text() //reads text from the console
{
  char * text = (char*)malloc(100);
  for(int i=0; i<100; i++)
  {
    text[i] = Serial.read();
    
    if(text[i] == '.')
    break;
  }
  return text;
}

There are quite a few problems with your code:

  1. Serial.read() does not block until a character is available. Use Serial.available() to check if characters were received, before you read from it.
  2. You allocate 100 bytes of memory but never free it. This will fill your ram very quickly.
  3. You never do anything with the translated text so there is no real point in doing so
  4. If you are sure that you only receive values between ‘A’ and ‘Z’ you can use that as an index into the translation table. No need to compare the characters.
  5. You are going over the whole text_array (100 bytes). What if you only received 10 characters instead of 100?

The serial input basics thread shows how to read serial port data reliably.

@LightuC

Tried that one out. Still nothing. I would appreciate a little hint.

char * read_text() //reads text from the console
{
  char * text = (char*)malloc(100);
  for(int i=0; i<100; i++)
  {
    if(Serial.available() > 0)
    {
    {text[i] = Serial.read();
    
    if(text[i] == '.')
    break;
    }
  }
  return text;
  }
}

What happens when Serial.available returns 0? (Hint: Serial.available() gets the number of bytes already received, nothing more)
Also please don’t use malloc, instead use a pre-allocated buffer.

int readTextFromSerial(char* buffer, unsigned int bufferSize)
{
	for(unsigned int i = 0; i < bufferSize; i++)
	{
                  // ...
                  buffer[i] = receivedCharacter
        }
}