converting string to char*

Hi,

i'm making a morse code translator and there are several problems with it.
The traslator is not working at all

I checked that there's nothing wrong with making whole String dashORdot, but the String dashORdot needs to be converted into char* type and I think there are some errors with this code for it.

or is it because of other errors?

#define SW 8
#define RELEASED 0
#define SHORT_PRESS 1
#define LONG_PRESS  2
#define SW_THRESHOLD  500


#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,2,3,4,5);

const int convert_button=9;
boolean buttonStatus;
int oneTimeFlag;

char* letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", ".." , // A-I 
                                    ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 
                                    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; // S-Z

char* alphabet[]={'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'};

String dashORdot;


void setup() {  
  pinMode(SW,INPUT);
  pinMode(convert_button,INPUT);
  digitalWrite(convert_button,LOW);
  Serial.begin(9600);
  lcd.begin(16,2);
}

void loop() {
  static unsigned long last;
  int swState = readSw();
  
  if (SHORT_PRESS == swState) {
    Serial.print(".");
    dashORdot.concat(".");
    
    
    
  } 
  else if (LONG_PRESS == swState) {
    Serial.println("-");
    dashORdot.concat("-");
    
    
  }

  if (digitalRead(convert_button)==HIGH) {
    if (oneTimeFlag==0) {
      oneTimeFlag=1;
      buttonStatus=!buttonStatus;
    }
    else {
      oneTimeFlag=0;
    }
    if (buttonStatus==1) {
      convert();
      
      
    }
    else {
      lcd.clear();
    }
    
  }

  
}

int readSw()
{
  static boolean prev = HIGH;
  static unsigned long last, pressed;
  boolean curr;
  //Serial.println("last"+last);
  if ((millis()-last) >= 10) {
    curr = digitalRead(SW);
    if ((HIGH==prev)&&(LOW==curr)) {
      pressed = millis();
      prev = LOW;
    } 
    else if ((LOW==prev)&&(HIGH==curr)) {
      prev = HIGH;
      if ((millis()-pressed) > SW_THRESHOLD) {
        return LONG_PRESS;
      } 
      else {
        return SHORT_PRESS;
      }
    }
    last = millis();
  }
  return RELEASED;
}



char* string2char(String command){
  if(command.length()!=0){
    char *p = const_cast<char*>(command.c_str());
    return p;
  }
}
void convert()
{
  
  int len=(sizeof(letters)/sizeof(letters[0]));
  int x;
  for (x=0; x<len; x++) {
    if (strcmp(string2char(dashORdot),letters[x])==0) {
      lcd.print(alphabet[x]);
      delay(500);
      
      
      
      
      
    }
  }
}

but the String dashORdot needs to be converted into char* type

Why is dashORdot a String in the first place ?

if i make dashORdot a char* in first place, i get compile error like this :sob: :

cannot convert 'char**' for argument '1' to 'charstrcat(char,const char*)

#define SW 8
#define RELEASED 0
#define SHORT_PRESS 1
#define LONG_PRESS  2
#define SW_THRESHOLD  500


#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,2,3,4,5);

const int convert_button=9;
boolean buttonStatus;
int oneTimeFlag;

char* letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", ".." , // A-I 
                                    ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 
                                    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; // S-Z

char* alphabet[]={'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'};

char* dashORdot[32];


void setup() {  
  pinMode(SW,INPUT);
  pinMode(convert_button,INPUT);
  digitalWrite(convert_button,LOW);
  Serial.begin(9600);
  lcd.begin(16,2);
}

void loop() {
  static unsigned long last;
  int swState = readSw();
  
  if (SHORT_PRESS == swState) {
    Serial.print(".");
    strcat(dashORdot, ".");
    
    
    
  } 
  else if (LONG_PRESS == swState) {
    Serial.println("-");
    strcat(dashORdot, "-");
    
    
  }

  if (digitalRead(convert_button)==HIGH) {
    if (oneTimeFlag==0) {
      oneTimeFlag=1;
      buttonStatus=!buttonStatus;
    }
    else {
      oneTimeFlag=0;
    }
    if (buttonStatus==1) {
      //convert();
      
      
    }
    else {
      lcd.clear();
    }
    
  }

  
}

int readSw()
{
  static boolean prev = HIGH;
  static unsigned long last, pressed;
  boolean curr;
  //Serial.println("last"+last);
  if ((millis()-last) >= 10) {
    curr = digitalRead(SW);
    if ((HIGH==prev)&&(LOW==curr)) {
      pressed = millis();
      prev = LOW;
    } 
    else if ((LOW==prev)&&(HIGH==curr)) {
      prev = HIGH;
      if ((millis()-pressed) > SW_THRESHOLD) {
        return LONG_PRESS;
      } 
      else {
        return SHORT_PRESS;
      }
    }
    last = millis();
  }
  return RELEASED;
}



void convert()
{
  
  int len=(sizeof(letters)/sizeof(letters[0]));
  int x;
  for (x=0; x<len; x++) {
    if (strcmp(dashORdot,letters[x])==0) {
      lcd.print(alphabet[x]);
      delay(500);
      
      
      
      
      
    }
  }
}

strcat is expecting a char*
Try
char* dashORdot;
Instead of
char* dashORdot[32];

strcat() expects an already-allocated pointer.

Try:

char dashORdot[30];

Instead of using the String class you can define a buffer like this:

const int BUF_LEN = 5;
char letterBuf[BUF_LEN];
int letterIdx = 0;

When you read a button press the character is stored in the buffer.

  // if button pressed
  if ( swState != RELEASED )
  {
    // set dot or dash
    char newChar = (SHORT_PRESS == swState) ? '.' : '-';

    Serial.print(newChar);

    // store character if room in buffer
    if ( letterIdx < BUF_LEN )
    {
      letterBuf[letterIdx++] = newChar;

    }

  }

After converting a character the index would have to be reset to zero and the buffer cleared.

void clearBuf()
{
  memset(letterBuf, 0, BUF_LEN);
}

Your convert() function looks at every letter in the alphabet even if it already found a match. I would rearrange things by stopping when a match is found and returning the index into the alphabet of the letter found.

// return index or -1
int convert()
{
  int retVal = (-1);
  int len = (sizeof(letters) / sizeof(letters[0]));

  // compare each letter
  for (int idx = 0; idx < len; idx++) {

    // if it's a match
    if (strcmp(letterBuf, (char *)letters[idx]) == 0) {
      retVal = idx;     // set index
      break;            // stop looking

    } // if

  } // for

  return (retVal);
}

I can't test the whole setup with the buttons but I did test some of the code by putting the following in setup().

  clearBuf();

  // debug - print alphabet
  for (int i = 0; i < 26; i++)
  {
    int letterPos;

    // copy letter
    strcpy(letterBuf, letters[i]);

    // find letter
    letterPos = convert();
    if ( letterPos  >= 0 )
    {
      Serial.print((char)alphabet[letterPos]);
      Serial.print(" ");
    }

    // clear buffer
    clearBuf();

  }
  Serial.println();

It prints all the letters in the alphabet.

There are some better ways of encoding the alphabet. See this link. Post #8 has code to try.