Need to add a "/" after every translated character but strcat doesn't work 100%

Okay, so basically in the function char string2ASH I am using a for loop to gradually loop through each character (for example “ash code”), calling the function char2ASH which converts a single character i.e. a into ASH code which is a made up language consisting of 1-4 length “words” made up only of the 3 characters “!”, "" and “@”.
Theoretically, every time I go through the loop, if the character at position input is a space then it will perform
* *strcat(ASHcat, " ")* *
where ASHcat is my output buffer. I am that this part of the function works pretty much as intended, however my issue is that, and I believe that the error resides within the “else” part of the if statement in question, if you enter more than one word to the serial port e.g. “ash ash ash” or “ash code”, the more words you enter the lower the amount of characters ASHcat holds, eventually reaching the point where on the third word when testing “ash ash ash” looks like this; “*///@!///!**///”
Thanks
(Code attached below, please ignore the comments I have a fair bit of house keeping yet to do)
```
*//blue led is 11 red is 5

const int oblen = 100;
char ASHcat[oblen];
char outputbuf[oblen];
char input[oblen];

char buffer1[20];
//char penis[100] = {‘a’,‘1’};
//bool wrongType;
int ASHorASCII;
char userInput1 = “LP”;
char userInput2 = “TXIR”;
char userInput3 = “–IR”;
char userInput4 = “PT”;
const int LED_BLUE = 11;
const int LED_RED = 5;
const int POTENTIOMETER = A0;
const int LED_YELLOW = 9;
const int LED_AMBER = 6;
const int LED_GREEN = 10;
const int LED_IR = A1;
int val;

char ASH_a = “";
char ASH_b[] = "!!
”;
char ASH_c = “!!@”;
char ASH_d = “!"; // for some reason char2ASH(d) is broken
char ASH_e[] = “!”;
char ASH_f[] = "!
!”;
char ASH_g = “";
char ASH_h[] = "!
”;
char ASH_i = “!!”;
char ASH_j = “!!";
char ASH_k[] = "
@”;
char ASH_l = “!@";
char ASH_m[] = "!
”;
char ASH_n = “!@”;
char ASH_o = “@";
char ASH_p[] = “!@!”;
char ASH_q[] = "
!@”;
char ASH_r = “!@";
char ASH_s[] = “@!”;
char ASH_t[] = “@”;
char ASH_u[] = “@@”;
char ASH_v[] = "!";
char ASH_w[] = "
”;
char ASH_x = “@!";
char ASH_y[] = “!@@”;
char ASH_z[] = “**@”;
char ASH_space[] = " "; //NEEDS TO BE CHANGED AT SOME POINT if issues with int data type for conversion use int variable in switch stateent
char ASH_1[] = "@!!
”;
char ASH_2 = “@!!@”;
char ASH_3 = “@!!";
char ASH_4[] = “@!**”;
char ASH_5[] = "@!
@”; //QUICK NOTE THAT THE LENGTH OF ALL NUMBERS IS 5 ASH CHARS INSTEAD OF 3 SO IN CONVERSION USE STRLEN AND PUT INTO AN INTEGER DATA TYPE INSTEAD OF CHAR OR WHATEVS
char ASH_6 = “@!@!”;
char ASH_7 = “@!@";
char ASH_8[] = “@!@@”;
char ASH_9[] = "@
!!”;
char ASH_0 = “@!!!”;
char ASH_fullStop = “@!!”;
char ASH_comma = “@!";
char ASH_apostrophe[] = "@
!”;
char ASH_questionMark = “@**”;
char ASH_exclamationMark = “@@";
char ASH_plus[] = “@@!”;
char ASH_minus[] = "@@
”;
char ASH_multiply = “@@@”;
char ASH_divide = “@!@”;

void userInput(char * ip ) {
  char tempBuffer;
  int i = 0;
  do {
    while (!Serial.available()) {};
    tempBuffer = Serial.read();
    if (tempBuffer == ‘\n’) {
      break;
    } else {
      ip[i] = tempBuffer;
      i++;
    }
    ip[i] = ‘\0’;
  } while (true);
}

char * char2ASH(char c) {

c = tolower(c);
 
  switch (c) {
    case ‘a’:
      return ASH_a;

case ‘b’:
      return ASH_b;

case ‘c’:
      return ASH_c;

case ‘d’:
      return ASH_d;

case ‘e’:
      return ASH_e;

case ‘f’:
      return ASH_f;

case’g’:
      return ASH_g;

case ‘h’:
      return ASH_h;

case ‘i’:
      return ASH_i;

case ‘j’:
      return ASH_j;

case ‘k’:
      return ASH_k;

case ‘l’:
      return ASH_l;

case ‘m’:
      return ASH_m;

case ‘n’:
      return ASH_n;

case ‘o’:
      return ASH_o;

case ‘p’:
      return ASH_p;

case ‘q’:
      return ASH_q;

case ‘r’:
      return ASH_r;

case ‘s’:
      return ASH_s;

case ‘t’:
      return ASH_t;

case ‘u’:
      return ASH_u;

case ‘v’:
      return ASH_v;

case ‘w’:
      return ASH_w;

case ‘x’:
      return ASH_x;

case ‘y’:
      return ASH_y;

case ‘z’:
      return ASH_z;

case ‘1’:
      return ASH_1;

case ‘2’:
      return ASH_2;

case ‘3’:
      return ASH_3;

case ‘4’:
      return ASH_4;

case ‘5’:
      return ASH_5;

case ‘6’:
      return ASH_6;

case ‘7’:
      return ASH_7;

case ‘8’:
      return ASH_8;

case ‘9’:
      return ASH_9;

case ‘0’:
      return ASH_0;

case ‘.’:
      return ASH_fullStop;

case ‘,’:
      return ASH_comma;

case ‘’’:
      return ASH_apostrophe;

case ‘?’:
      return ASH_questionMark;

case ‘!’:
      return ASH_exclamationMark;

case ‘+’:
      return ASH_plus;

case ‘-’:
      return ASH_minus;

case ‘*’:
      return ASH_multiply;

case ‘/’:
      return ASH_divide;

case ’ ':
      return ASH_space;
  }

}

char * detectIpType(char *input, int val, char *buffer1, char *userInput1) { //char decide which acts upon the result of isASH using the true and false returns
String str (val);
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);

char *string2ASH(char * input, char * ASHcat) {
int count = 0;
  //ASHcat[0] = ‘\0’;
  int i = 0;
  int len = 0;
  int ASHindex = 1;
  char *fSlash;
  char *buf2;
  char *buf;
  char *space;

space = " ";
      fSlash = “/”;
     
for(i=0;i<strlen(input);i++) {

// if (input[i + 1] == ’ ') {
  //  strcat(buf, space);
   
   
   
//  } else {

if (input[i] == ’ ') {
     
    strcat(ASHcat, " ");

} else {
buf = (char2ASH(input[i]));
  strcat(buf, “/”);
//Serial.println(buf);
    strcat(ASHcat, buf);
 
}

//}
}
  return ASHcat;
}

char buffer2[20];
  char buffer3[20];
  char buffer4[20];
int i = 0;
  for(i=0;i<2;i++){
  buffer1[i] = input[i];
}
// for(i=0;i<5;i++){
//  buffer2[i] = input[i];
//  }
//  for(i=0;i<5;i++){
  //  buffer3[i] = input[i];
  //}
    for(i=0;i<5;i++){
     
    buffer4[i] = input[i];
   
   
    }
   
  //  Serial.println(buffer4);
 
// NEED TO HAVE FOR LOOP FOR BUFFER FOR TO SEE IF SOMEONES ENTERED “PT”

/* do  {
    if (input[i] > 0 && input[i] <10) {
      i++;
    wrongType = false;
  } else {
  wrongType = true;
 
  }
  } while (input[i] != ‘\0’);
  */

void LPcounter(char *input){
  int i=0;
  int P1;
  int P2;
  int P3;
  int P4;
  int P5;
  int P6;
  int P7;
  int P8;
  int P9;
  int P10;
  int P11;
  int P12;
  int P13;
  int T1;
  int T2;
  int T3;
  int T4;

P1 = input[2];
    P1 = P1 * 100;
    P2 = input[3];
    P2 = P2 * 10;
    P3 = input[4];
    T1 = P1 + P2 + P3;
    P4 = input[5];
    P4 = P4 * 100;
    P5 = input[6];
    P5 = P5 * 10;
    P6 = input[7];
    T2 = P4 + P5 + P6;
    P7 = input[8];
    P7 = P7 * 100;
    P8 = input[9];
    P8 = P8 * 10;
    P9 = input[10];
    T3 = P7 + P8 + P9;
    P10 = input[11];
    P10 = P10 * 100;
    P11 = input[12];
    P11 = P11 * 10;
    P12 = input[13];
    T4 = P10 + P11 + P12;
    analogWrite(LED_RED, T1);
    analogWrite(LED_YELLOW, T2);
    analogWrite(LED_AMBER, T3);
    analogWrite(LED_GREEN, T4);
  // digitalWrite
   
   
    // buffer1[5] = P1;
    // digitalWrite(LED_RED, (buffer1[5], buffer1[6], buffer1[7]));
    // buffer1[5] = P1;
 
   
 
 
 
}

void setup() {
  Serial.begin(57600);
Serial.println(ASH_d);
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_AMBER, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);

}

void loop() {

int val = analogRead(POTENTIOMETER);

// char potvalue[5] = {val}; CANNOT SET POT VALUE TO BE EQUAL TO AN INTEGER WITHOUT IT RETURNING GARBAGE, ASK TOMORROW

userInput(input);
  detectIpType(input, val, buffer1, userInput1);
  memset(ASHcat, 0, sizeof(100));
  memset(outputbuf, 0, sizeof(100));
  //TAKES AGES TO BRING UP THE SECOND ENTRY AND RUNS REALLY SLOW

}*
```

Reposted it with the function string2ASH which is where the problem is.

The most frequently misused statement in C has to be the do/while statement. In 35 years of programming, I have never had to use a do/while statement. There is no reason for you to be using one, either.

A while statement is MUCH more logical.

String str (val);

Useless pissing away of resources. People have been writing string handling code in C for years before that useless class was invented.

  //ASHcat[0] = '\0';

Why would you even consider doing this? Why the hell didn't you delete it when you realized it was stupid?

Do you

suppose

you could add

some more

silly white space to

your code?

buf = (char2ASH(input[i]));

Maybe some more useless parentheses, too?

buf = (char2ASH(input[i]));
  strcat(buf, "/");

You can NOT add more data to buf. buf points to a FIXED amount of memory.

I got fed up with all the stupid white space and commented out code and poor indenting at this point, and quit reading.

Well, that sure enraged PaulS!

   memset(ASHcat, 0, sizeof(100));
   memset(outputbuf, 0, sizeof(100));

Hmm.

void setup ()
  {
  Serial.begin (115200);
  Serial.println (sizeof(100));
  }  // end of setup

void loop () { }

Output:

2

Why take the size of 100? What's wrong with 100? Or better still sizeof (ASHcat).

Your current sketch won't even format, let alone compile. Too many left curly braces.

You have 'string2ASH' declared in the middle of 'detectIpType'.

Get your sketch to compile, format it (Tools->Auto Format), and THEN post it again.

Sorry but this is probably the messiest sketch I've ever seen. I'm not so fussy as others about indenting and formatting of code etc..., but this is simply diabolical. It looks like you've randomly highlighted chunks of code and hit delete.

The last time I saw anything this bad was after my cat had been playing with the animated cursor on my laptop while sitting on the keyboard.

The last time I saw anything this bad was after my cat had been playing with the animated cursor on my laptop while sitting on the keyboard.

I love the visual… 8)

Your sketch would be lot shorter if you use more arrays (properly)

All those P-variables with an index number “shout” for an array just like the ASH_a …ASH_z thingies.

You can have arrays of (char) arrays .

This is, as best as I can determine, what the code is going. Since “detectIpType()” doesn’t actually do anything it appears that most of the code will never be used.

What is “detectIpType()” supposed to do?!?

//blue led is 11 red is 5

const int oblen = 100;
char ASHcat[oblen];
//char outputbuf[oblen];
char input[oblen];

char buffer1[20];

// int ASHorASCII;
char userInput1[] = "LP";
//char userInput2[] = "TXIR";
//char userInput3[] = "--IR";
//char userInput4[] = "PT";

const int LED_RED = 5;
const int LED_AMBER = 6;
const int LED_YELLOW = 9;
const int LED_GREEN = 10;
const int LED_BLUE = 11;
const int POTENTIOMETER = A0;
const int LED_IR = A1;

int val;

char *ASH_a_z[] = {
  /*a-g*/  "*", "!!*", "!!@", "*!", "!", "!*!", "**", 
  /*h-n*/  "!**", "!!", "*!!", "*@", "!*@", "!*", "!@", 
  /*o-u*/  "@*", "!@!", "*!@", "!@*", "@!", "@", "@@",
  /*v-z*/  "**!",  "***", "*@!", "!@@", "**@"};

char ASH_space[] = " "; //NEEDS TO BE CHANGED AT SOME POINT if issues with int data type for conversion use int variable in switch stateent
char *ASH_0_9[] = {
  "@!!!", "@!!*", "@!!@", "@!*!", "@!**", "@!*@", "@!@!",  "@!@*", "@!@@", "@*!!"};

char ASH_fullStop[] = "@!!";
char ASH_comma[] = "@!*";
char ASH_apostrophe[] = "@*!";
char ASH_questionMark[] = "@**";
char ASH_exclamationMark[] = "@*@";
char ASH_plus[] = "@@!";
char ASH_minus[] = "@@*";
char ASH_multiply[] = "@@@";
char ASH_divide[] = "@!@";


void userInput(char * ip ) {
  char tempBuffer;
  int i = 0;
  while (true) {
    while (!Serial.available()) {}
    tempBuffer = Serial.read();
    if (tempBuffer == '\n') {
      break;
    } 
    else {
      ip[i] = tempBuffer;
      i++;
    }
    ip[i] = '\0';
  } 
}


const char * char2ASH(char c) {
  c = tolower(c);

  if (c >= 'a' && c <= 'z')
    return ASH_a_z[c-'a'];

  if (c >= '0' && c <= '9')
    return ASH_0_9[c-'0'];

  switch (c) {

  case '.':
    return ASH_fullStop;

  case ',':
    return ASH_comma;

  case '\'':
    return ASH_apostrophe;

  case '?':
    return ASH_questionMark;

  case '!':
    return ASH_exclamationMark;

  case '+':
    return ASH_plus;

  case '-':
    return ASH_minus;

  case '*':
    return ASH_multiply;

  case '/':
    return ASH_divide;

  case ' ':
    return ASH_space;
  }
  return "";
}



char * detectIpType(char *input, int val, char *buffer1, char *userInput1) { 
  //char decide which acts upon the result of isASH using the true and false returns
}


char *string2ASH(const char *input, char *ASHcat) {
  for(int i=0;i<strlen(input);i++) {
    if (input[i] == ' ') {
      strcat(ASHcat, " ");
    } 
    else {
      strcat(ASHcat, char2ASH(input[i]));
      strcat(ASHcat, "/");
    }
  }
  return ASHcat;
}

void LPcounter(char *input) {
  int T1 = input[2] * 100 + input[3] * 10 + input[4];
  int T2 = input[5] * 100 + input[6] * 10 + input[7];
  int T3 = input[8] * 100 + input[9] * 10 + input[10];
  int T4 = input[11] * 100 + input[12] * 10 + input[13];

  analogWrite(LED_RED, T1);
  analogWrite(LED_YELLOW, T2);
  analogWrite(LED_AMBER, T3);
  analogWrite(LED_GREEN, T4);
}


void setup() {
  Serial.begin(57600);
  Serial.println(ASH_a_z[3]);  // code for 'd'?
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_AMBER, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
}


void loop() {
  int val = analogRead(POTENTIOMETER);
  // char potvalue[5] = {val}; CANNOT SET POT VALUE TO BE EQUAL TO AN INTEGER WITHOUT IT RETURNING GARBAGE, ASK TOMORROW

  userInput(input);
  detectIpType(input, val, buffer1, userInput1);
  memset(ASHcat, 0, sizeof ASHcat);
  // memset(outputbuf, 0, sizeof outputbuf);
}