How to flash serial port output with led and control speed with potentiometer?

Hi guys, I've created a program that translates ASH to ASCII and vice versa. I now need to flash the ASH onto the arduino shield led pins. For example, if the user types in ASCII when the ASH translation gets displayed on the serial port it should also flash onto the arduino led.

I need to flash modified ASH (when the user enters ASCII and it gets translated to ASH) onto LED pin 5 and I need to flash unmodified (When the user enters ASH onto the serial port) onto LED pin 11. I also need to control the ASH transmission speed (LOW, HIGH speed) with the potentiometer on the arduino board.

If anyone here could offer me help it would be greatly appreciated, I just don't know where to start with it, because I don't want to do more than I have to when a function could just be modified to do the task.

Previous threads I've posted on this topic -
How to create a function that detects if the user has entered ASCII or ASH?

Where Should I put my bool statement to get it to work?

How to store a buffer of single characters and print them out as a string?

NOTE: I've include an attachment which explains ASH and the timings for the ASH led flashes :)!

Here's my code so far :slight_smile: :

char ASH_a[] = "*";
char ASH_b[] = "!!*";
char ASH_c[] = "!!@";
char ASH_d[] = "*!";
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_1[] = "@!!*";
//char ASH_2[] = "@!!@";
//char ASH_3[] = "@!*!";
//char ASH_4[] = "@!**";
//char ASH_5[] = "@!*@";
//char ASH_6[] = "@!@!";
//char ASH_7[] = "@!@*";
//char ASH_8[] = "@!@@";
//char ASH_9[] = "@*!!";
//char ASH_0[] = "@!!!";
//char ASH_.[] = "@!!";
//char ASH_,[] = "@!*";
//char ASH_?[] = "@**";
//char ASH_![] = "@*@";
//char ASH_+[] = "@@!";
//char ASH_-[] = "@@*";
//char ASH_*[] = "@@@";
//char ASH_/[] = "@!@";


char t[] = "ash code";
char buffer1[100];
const int oblen = 100;
char ob [oblen];



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  // Serial.println(ASH2ASCII("!**")); //These are the ASH characters I want to convert to ASCII using the ASH2ASCII function
  //Serial.println(ASH2ASCII("!"));
  //Serial.println(ASH2ASCII("!*@"));
  //Serial.println(ASH2ASCII("!*@"));
  //Serial.println(ASH2ASCII("@*"));


  //ASCIIstring2ASHstring (t, ob, oblen);
  //Serial.println(ob);
  
   
  Serial.println("Welcome to my ASCII and ASH translator!");
  Serial.println("NOTE: if you're translating from ASH to ASCII please put a '/' after each ASH character you enter, thanks!");
  
  usinput(buffer1);
  Serial.print(buffer1);
  chardecide(buffer1);

 
}

void chardecide(char * buffer1) { //char decide which acts upon the result of isASH using the true and false returns
  if (isASH (buffer1)) {
    Serial.println(" has been recognized as ASH - ");
    ASHstring2ASCIIstring(buffer1, ob); //passes function with buffer1 and ob included
    Serial.println(ob); 
} else {
    Serial.println(" has been recognized as ASCII - ");
    ASCIIstring2ASHstring (buffer1, ob, oblen);
    Serial.println(ob);
  }
}

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


bool isASH(char * buffer1)
{

  if (buffer1[0] != '*' && buffer1[0] != '!' && buffer1[0] != '@') return false;

  return true;

}

 int ASHstring2ASCIIstring(char *buffer, char *ob)  //converts ash to ascii
{
    char str[10];
    int j = 0;
    int l = 0;

    while (*buffer) {
        if (*buffer == '/') { //hit slash
            str[j] = '\0'; //empty buffer
            ob[l++] = ASH2ASCII(str);
            j = 0;
        } else {
            if (j + 1 < sizeof(str)) {
                str[j++] = *buffer;
            }
        }

        buffer++;
    }
    if (j > 0) ob[l++] = ASH2ASCII(str);
    ob[l] = '\0';
    return l;
}

void ASCIIstring2ASHstring (char * ip, char * op, int oplen) { //converts ascii to ash

  op[0] = '\0';
  int bp = 0;
  int n;
  char m[9];
  int l = strlen(ip);
  for (int i = 0; i < l; i++) {
    m[0] = '\0';
    strcpy(m, ASCII2ASH(ip[i]));
    n = strlen(m);
    if ((bp + n + l) < oplen) {
      strcat(op , m);
      bp = bp + n;
    }
    if (ip[i] != ' ' && ip[i + l] != ' ') {
      op[bp] = '/';
      bp++;
    }
    op[bp] = '\0';
  }
}


char ASH2ASCII(char * m) { //Using the char variables impmented globally, ASH2ASCII searches through specific variables until it finds a match for the conversion, at which point it will capture and reuturn the ASCII string

  if (strcmp(ASH_a, m) == 0) { //if string captured return a
    return 'a';
  }
  else if (strcmp(ASH_b, m) == 0) { //else if b string is captured return
    return 'b';
  }
  else if (strcmp(ASH_c, m) == 0) {
    return 'c';
  }
  else if (strcmp(ASH_d, m) == 0) {
    return 'd';
  }
  else if (strcmp(ASH_e, m) == 0) {
    return 'e';
  }
  else if (strcmp(ASH_f, m) == 0) {
    return 'f';
  }
  else if (strcmp(ASH_g, m) == 0) {
    return 'g';

  }
  else if (strcmp(ASH_h, m) == 0) {
    return 'h';

  }
  else if (strcmp(ASH_i, m) == 0) {
    return 'i';

  }
  else if (strcmp(ASH_j, m) == 0) {
    return 'j';

  }
  else if (strcmp(ASH_k, m) == 0) {
    return 'k';

  }
  else if (strcmp(ASH_l, m) == 0) {
    return 'l';

  }
  else if (strcmp(ASH_m, m) == 0) {
    return 'm';

  }
  else if (strcmp(ASH_n, m) == 0) {
    return 'n';

  }
  else if (strcmp(ASH_o, m) == 0) {
    return 'o';

  }
  else if (strcmp(ASH_p, m) == 0) {
    return 'p';

  }
  else if (strcmp(ASH_q, m) == 0) {
    return 'q';

  }
  else if (strcmp(ASH_r, m) == 0) {
    return 'r';

  }
  else if (strcmp(ASH_s, m) == 0) {
    return 's';

  }
  else if (strcmp(ASH_t, m) == 0) {
    return 't';

  }
  else if (strcmp(ASH_u, m) == 0) {
    return 'u';

  }
  else if (strcmp(ASH_v, m) == 0) {
    return 'v';

  }
  else if (strcmp(ASH_w, m) == 0) {
    return 'w';

  }
  else if (strcmp(ASH_x, m) == 0) {
    return 'x';

  }
  else if (strcmp(ASH_y, m) == 0) {
    return 'y';

  }
  else if (strcmp(ASH_z, m) == 0) {
    return 'z';

  }

}

void ASCIIstring2ASH (char * buf) {

  Serial.println(ASCII2ASH(*t));

}


char * ASCII2ASH (char c) { //This is the opposire of ASH2ASCII, it uses the globally defined variables to search through ASCII characters, and returns the ASH version of that character

  switch (c) {

    case 'a':
      return ASH_a;//returns ASH version of a if matched

    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 ' ':
      return " ";

    default:
      Serial.println("switching went wrong!");
      break;

  }
}



void loop() {


}

You might have to start by explaining what ASH is (link). That might also explain how you would flash a "!" onto an LED.

I would change your code and store the translation table in one two-dimensional array.

I also guess that the lower part of your setup() routine should be moved to the loop() routine for continued operation.

What do you want to achieve with that sketch?

pylon:
You might have to start by explaining what ASH is (link). That might also explain how you would flash a "!" onto an LED.

I would change your code and store the translation table in one two-dimensional array.

I also guess that the lower part of your setup() routine should be moved to the loop() routine for continued operation.

What do you want to achieve with that sketch?

Sorry! I thought I included the ash specification sheet, I've updated my post to include the sheet so hopefully now my post is alot more clear in what I'm trying to achieve here :)!

This code could form the base of a ASH transmission method:

uint32_t tunit = 400; // could be any value or set by the poti
void sendASH(char *code) {
  while (*code) {
    byte l = 6;
    byte v = LOW;
    if (*code == '!') {
      l = 1;
      v = HIGH;
    } else if (*code == '*') {
      l = 2;
      v = HIGH;
    } else if (*code == '@') {
      l = 4;
      v = HIGH;
    } else if (*code == '/') {
      l = 2;
      v = LOW;
    } // everything else is treated as a space character
    digitalWrite(LEDpin, LOW);
    delay(tunit);
    digitalWrite(LEDpin, v);
    delay(tunit * l);
    code++;
  }
}

Edit: Fixed an error in the while loop.

lewrossi:
Sorry! I thought I included the ash specification sheet,

If you had just asked this question in your original Thread about ASH everyone would have ready access to all the info.

...R

Robin2:
If you had just asked this question in your original Thread about ASH everyone would have ready access to all the info.

...R

Yeah, sorry about that, this past week has been the first time I've actually used coding forums properly so will keep in mind from now on so users don't have to go searching through my profile for information!

lewrossi:
so users don't have to go searching through my profile for information!

Put a link in this Thread to your other Thread.

...R

Robin2:
Put a link in this Thread to your other Thread.

...R

Updated to include all links now!

bump! Looking for responses :slight_smile:

lewrossi:
bump! Looking for responses :slight_smile:

Have you read my response? You didn't react on it.

pylon:
Have you read my response? You didn't react on it.

Yeah, sorry I didn't respond. I've got it working now, I used your code as a template and it works well, the idea of the else if for each character was good, I just had to add in a few variables and delays! :slight_smile:
Thanks!