Could someone help me? I'm new

I need do a morse translator, but it doesn't do anything and I don't know why, can someone help me?
I'm not a professional in the coding, but I know some things.

struct Caracts{
  String a = ".-";
  String b = "-...";
  String c = "-.-.";
  String d = "-..";
  String e = ".";
  String f = "..-.";
  String g = "--.";
  String h = "....";
  String i = "..";
  String j = ".---";
  String k = "-.-";
  String l = ".-..";
  String m = "--";
  String n = "-.";
  String o = "---";
  String p = ".--.";
  String q = "--.-";
  String r = ".-.";
  String s = "...";
  String t = "-";
  String u = "..-";
  String v = "...-";
  String w = ".--";
  String x = "-..-";
  String y = "-.--";
  String z = "--..";
};

Caracts mor;

int i = 0;
int l = 0;
String matriz[] = {};
String letras = "";
String punto = ".";
String guion = "-";
String espacio = " ";
int tiempo_guion = 2.5;
int tiempo_term = 1;
int tiempo_espacio = 5;
int tiempo_activo = 0;
int tiempo_inactivo = 0; 

void setup() {
  pinMode(2, OUTPUT);//led
  pinMode(5, OUTPUT);//buzzer
  pinMode(8, OUTPUT);//boton
  Serial.begin(9600);
}

void carga(String mat[], int i); //esta matriz carga caracteres dentro de la matriz principal
void mostrar(String mat[], String letras, int l);
void cantidadMatriz(String mat[], int l);//esta matriz cuenta la cantidad de caracteres que tiene la matriz principal

void loop() {
  carga(matriz, i);
}

void carga(String mat[], int i){ 
  int j = 0;
  while(j < 1){
    if( digitalRead(8) == 0){
      digitalWrite(2, LOW);
      digitalWrite(5, LOW);
      while(digitalRead(8) == 0){
      	tiempo_inactivo += 1;
      }
      if(tiempo_inactivo == tiempo_term){
        mostrar(mat, letras, l);
        j++;
      }
      if(tiempo_inactivo > tiempo_espacio and tiempo_inactivo < tiempo_term){
        mat[i] = espacio;
        i++;  
      }
      if(tiempo_inactivo < tiempo_term and tiempo_inactivo >= 7500){
        while(tiempo_inactivo < tiempo_term){
          digitalWrite(2, HIGH);
          delay(50);
          digitalWrite(2, LOW);
        }
      }
    }
    if( digitalRead(8)== 1){
      while(digitalRead(8) == 1){
      	tiempo_activo += 1;
      }
      digitalWrite(2, HIGH);
      digitalWrite(5, HIGH);
      delay(tiempo_activo);
      digitalWrite(2, LOW);
      digitalWrite(5, LOW);
      if(tiempo_activo < tiempo_guion){
        mat[i] = punto;
        i++;  
      }
      if(tiempo_activo >= tiempo_guion and tiempo_activo < tiempo_espacio){
          digitalWrite(2, HIGH);
      digitalWrite(5, HIGH);
      delay(tiempo_activo);
      digitalWrite(2, LOW);
      digitalWrite(5, LOW);
      mat[i] = guion;
      i++;
      }
    }
  }
  cantidadMatriz(matriz, l);
}

void cantidadMatriz(String mat[], int l){ 
  int i = 0;
  if(mat[i] == punto or mat[i] == guion or mat[i] == espacio){
    l++;
    i++;
  }
}

void mostrar(String mat[], String letras, int l){
  for(int j = 0; j < l; j++){
    if(mat[j] != espacio){
      Serial.print(mat[j]);
      letras = mat[j];  
    }
    else{
      if(letras == mor.a){
        Serial.print("\t");
        Serial.print('a'); 
      }
      if(letras == mor.b){
        Serial.print("\t");
        Serial.print('b'); 
      }
      if(letras == mor.c){
        Serial.print("\t");
        Serial.print('c'); 
      }
      if(letras == mor.d){
        Serial.print("\t");
        Serial.print('d'); 
      }
      if(letras == mor.e){
        Serial.print("\t");
        Serial.print('e'); 
      }
      if(letras == mor.f){
        Serial.print("\t");
        Serial.print('f'); 
      }
      if(letras == mor.g){
        Serial.print("\t");
        Serial.print('g'); 
      }
      if(letras == mor.h){
        Serial.print("\t");
        Serial.print('h'); 
      }
      if(letras == mor.i){
        Serial.print("\t");
        Serial.print('i'); 
      }
      if(letras == mor.j){
        Serial.print("\t");
        Serial.print('j'); 
      }
      if(letras == mor.k){
        Serial.print("\t");
        Serial.print('k'); 
      }
      if(letras == mor.l){
        Serial.print("\t");
        Serial.print('l'); 
      }
      if(letras == mor.m){
        Serial.print("\t");
        Serial.print('m'); 
      }
      if(letras == mor.n){
        Serial.print("\t");
        Serial.print('n'); 
      }
      if(letras == mor.o){
        Serial.print("\t");
        Serial.print('o'); 
      }
      if(letras == mor.p){
        Serial.print("\t");
        Serial.print('p'); 
      }
      if(letras == mor.q){
        Serial.print("\t");
        Serial.print('q'); 
      }
      if(letras == mor.r){
        Serial.print("\t");
        Serial.print('r'); 
      }
      if(letras == mor.s){
        Serial.print("\t");
        Serial.print('s'); 
      }
      if(letras == mor.t){
        Serial.print("\t");
        Serial.print('t'); 
      }
      if(letras == mor.u){
        Serial.print("\t");
        Serial.print('u'); 
      }
      if(letras == mor.v){
        Serial.print("\t");
        Serial.print('v'); 
      }
      if(letras == mor.w){
        Serial.print("\t");
        Serial.print('w'); 
      }
      if(letras == mor.x){
        Serial.print("\t");
        Serial.print('x'); 
      }
      if(letras == mor.y){
        Serial.print("\t");
        Serial.print('y'); 
      }
      if(letras == mor.z){
        Serial.print("\t");
        Serial.print('z'); 
      }
      Serial.println(mat[j]);  
    }
  }
}

It doesn't do anything at all? What did you expect it to do? Have you tried to debug it by using Serial.print() some of the variables you use in the program to see if they provide a clue?

Paul

I know that there are Arduino official examples that show the use of String variables but those are small examples written by PC programmers.

String is not good to use with small RAM, and you use a lot of Strings.

Instead use C language strings which are char[] arrays containing text and a 0 char (not '0') at the end.

char Caracts[ 26 ][ 5 ] = // 26 letters, space for 4 ./- plus terminating 0 each
{ 
  ".-", "-...", "-.-.",

--------- you do the rest
};

Letter 'a' is in Caracts[0]. Every letter is in Caracts[ letter - 'a' ].
Caracts[0][0] is '.' and [0][1] is '-' and [0][2] is 0 which tells you that the end is reached.

All computer text is by number. Letter 'a' == 97 whether it is in a String or a string but it is much easier to know using C char array strings that do not hide details, add overhead, or do things you likely have no idea it does.

------------------- and -----------------

You cannot digital read pin 8 (that you named boton but then coded 8 instead of boton) because you moded it OUTPUT.

void setup() {
  pinMode(2, OUTPUT);//led
  pinMode(5, OUTPUT);//buzzer
  pinMode(8, OUTPUT);//boton
  Serial.begin(9600);
}

And last, make a habit of setting serial speed high unless it must be less. Faster serial will clear the serial output buffer more quickly, if a print would overfill the buffer then only printing happens until the print is all in the buffer at whatever speed is set. 9600 baud is 960 chars/sec, 115200 baud is 11520 chars/sec, which will empty the buffer quicker?

Why nou use booleans, FALSE for a short beep, TRUE for a long beep? That ought to save data memory. Any Morse character can rest in one byte.

Railroader:
Why nou use booleans, FALSE for a short beep, TRUE for a long beep? That ought to save data memory. Any Morse character can rest in one byte.

Arduino bools take 1 byte each. Better to use bit logic which is not beginner-friendly though powerful stuff.

You could code 1 byte as 4 2-bit values to get 0 for dot, 1 for dash, 2 for end using C bitfields.

@GFS
Thanks for telling me. I didn't know how Booleans recides in memory. Extracting bits, as I thought, out of bytes costs code anyway. Nothing is for free.

Thank you for help me with the translator, now is working very good!

GoForSmoke:
I know that there are Arduino official examples that show the use of String variables but those are small examples written by PC programmers.

String is not good to use with small RAM, and you use a lot of Strings.

Instead use C language strings which are char[] arrays containing text and a 0 char (not '0') at the end.

char Caracts[ 26 ][ 5 ] = // 26 letters, space for 4 ./- plus terminating 0 each

{
 ".-", "-...", "-.-.",

--------- you do the rest
};




Letter 'a' is in Caracts[0]. Every letter is in Caracts[ letter - 'a' ]. 
Caracts[0][0] is '.' and [0][1] is '-' and [0][2] is 0 which tells you that the end is reached.

All computer text is by number. Letter 'a' == 97 whether it is in a String or a string but it is much easier to know using C char array strings that do not hide details, add overhead, or do things you likely have no idea it does.

------------------- and -----------------

You cannot digital read pin 8 (that you named boton but then coded 8 instead of boton) because you moded it OUTPUT.



void setup() {
 pinMode(2, OUTPUT);//led
 pinMode(5, OUTPUT);//buzzer
 pinMode(8, OUTPUT);//boton
 Serial.begin(9600);
}




And last, make a habit of setting serial speed high unless it must be less. Faster serial will clear the serial output buffer more quickly, if a print would overfill the buffer then *only printing happens until the print is all in the buffer at whatever speed is set. 9600 baud is 960 chars/sec, 115200 baud is 11520 chars/sec, which will empty the buffer quicker?*
*[/quote]*

I see you already have your code working but i thought i would just post a Morse code converter i wrote last year, which uses far less memory than your current version. It works via Serial and you can just enter a character or sentence to convert to Morse code, or you can enter a Morse code in the format or dots and underscores to convert to characters. Maybe your find something useful from it or not but none the less here is the code.

EDIT: Sorry i posted the wrong version, all sorted...

/*
  Created by edwin Martin 16/08/2018.
  Arduino Serial to Morse code example V1.0.3
  Enter either a ascii word or sentance to convert to Morse code,
  or enter a Morse code in the format of dots and underscores to convert to ascii characters,
  each Morse code should be seperated with a space.
  The dot length is 200ms dash is 600ms, however the length between letters and words
  needs to be addressed as it uses the same lengths, rather than 3 units and 7 units.
*/

const byte morseCodes[36] = {
  0b10100000,  // a - A
  0b00011000,  // b - B
  0b01011000,  // c - C
  0b00110000,  // d - D
  0b01000000,  // e - E
  0b01001000,  // f - F
  0b01110000,  // g - G
  0b00001000,  // h - H
  0b00100000,  // i - I
  0b11101000,  // j - J
  0b10110000,  // k - K
  0b00101000,  // l - L
  0b11100000,  // m - M
  0b01100000,  // n - N
  0b11110000,  // o - O
  0b01101000,  // p - P
  0b10111000,  // q - Q
  0b01010000,  // r - R
  0b00010000,  // s - S
  0b11000000,  // t - T
  0b10010000,  // u - U
  0b10001000,  // v - V
  0b11010000,  // w - W
  0b10011000,  // x - X
  0b11011000,  // y - Y
  0b00111000,  // z - Z
  0b11111100,  // 0
  0b11110100,  // 1
  0b11100100,  // 2
  0b11000100,  // 3
  0b10000100,  // 4
  0b00000100,  // 5
  0b00001100,  // 6
  0b00011100,  // 7
  0b00111100,  // 8
  0b01111100,  // 9
};

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);  // LED_BUILTIN is pin 13
  Serial.println(F("Serial to Morse code example."));
}

void loop() {
  if (Serial.available()) {
    const char input = Serial.read();
    if (input >= 'a' && input <= 'z')
      getMorseCode(input, true, false);
    else if (input >= 'A' && input <= 'Z')
      getMorseCode(input, true, true);
    else if (input >= '0' && input <= '9')
      getMorseCode(input, false, false);
    else if (input == '.' || input == '-' || input == '\n'  || input == ' ')
      convertMorseCode(input);
  }
}

void getMorseCode(const char& a_input, const bool& a_isLetter, const bool& a_isUpper) {
  byte arrayPosition = a_input;
  Serial.print(a_input); Serial.print(F(" = "));
  if (a_isLetter && !a_isUpper)
    arrayPosition -= 'a';
  else if (a_isLetter && a_isUpper)
    arrayPosition -= 'A';
  else
    arrayPosition -= 22;
  readMorseCode(arrayPosition, true);
  Serial.println(F(""));
}

void readMorseCode(const byte& a_address, const bool& a_beep) {
  bool flagBit = false;
  for (byte i = 0; i < 8; i++) {
    if ((bitRead(morseCodes[a_address], i) == 0) && flagBit) { // short pulse - dot
      Serial.print(F(".")); // 0
      if (a_beep)
        flashLED(200);
    }
    else if ((bitRead(morseCodes[a_address], i) == 1) && flagBit) { // long pulse - dash
      Serial.print(F("-")); // 1
      if (a_beep)
        flashLED(600);
    }
    else if ((bitRead(morseCodes[a_address], i) == 1) && !flagBit) { // flag bit
      flagBit = true;
    }
  }
}

void convertMorseCode(const char& a_input) {
  static byte morseCode = 1, position = 1;
  if (a_input == '.')
    bitWrite(morseCode, position, 0);
  else if (a_input == '-')
    bitWrite(morseCode, position, 1);
  if (position == 7 || (position != 1 && (a_input == '\n' || a_input == ' '))) {
    char character = 0;
    if (position < 7)
      morseCode = morseCode << 8 - position;
    for (byte i = 0; i < 36; i++) {
      if (morseCodes[i] == morseCode) {
        if (i <= 25)
          character = 'A' + i; // will only print chars in upper case
        else
          character = '0' + (i - 26); // numbers
        readMorseCode(i, false);
        break;
      }
    }
    Serial.print(F(" = "));
    character != 0 ? Serial.println(character) : Serial.println(F("no match!"));
    position = 1;
    morseCode = 1;
  }
  else if (a_input == '.' || a_input == '-')
    position++;
}

void flashLED(const int& a_time) {  // You could add a buzzer to pin 13 too.
  digitalWrite(LED_BUILTIN, HIGH);
  delay(a_time);
  digitalWrite(LED_BUILTIN, LOW);
  delay(a_time);
}