Custom HEX codes for ASCII characters. Do I need a personalized library?

Hello,

I am working on a simple laser-based transciever and i have seem to have succeed in creating the circuit and the code, but I need help on 2 final steps of the programming part of the project.

Basically I enter a HEX character from a keypad and it sends it to the message preview LCD. The code has start and end markers ("<" and “>”) so it knows when you have typed the entire message you want to send over serially.

So, an example of a message looks like “” (without the quote marks) and the receiver will display on the LCD “CDF312A0” (again, w/o quotes).

What I need help with is the part where I want to assign an ASCII character to a certain pair of HEX symbols. For example, “CD”=A; “F3”=B; “12”=C; “A0”=D and so on. Therefore, the message you will encode and display on your LCD and the decoded message that the receiver will get will be “ABCD”.

Here is my code so far:

#include <LiquidCrystal.h>

LiquidCrystal lcdSend(2, 3, 4, 5, 6, 7); //declare arduino pins used on sending LCD screen
LiquidCrystal lcdReceive(A1, A0, 13, 12, 11, 10);
String message;

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;
int thresholds[16] = {0, 79, 146, 204, 248, 294, 335, 371, 399, 429, 456, 481, 501, 522, 542, 560}; //voltate thresholds for each character key
char keypad[16] = {'1', '2' , '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '<', '0', '>', 'D'}; //values assigned to voltage values

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.println(receivedChars);
    lcdReceive.print(receivedChars);
    newData = false;
  }
}

void setup() {
  Serial.begin(1200); //start serial port
  lcdSend.begin(16, 2);   //setu[ sending LCD
  lcdReceive.begin(16, 2); //set receiving LCD
  lcdSend.setCursor(0, 0);
  lcdReceive.setCursor(0, 0);
  lcdReceive.print("Received");
  lcdSend.print("Write here:");
  delay(500);
  lcdSend.clear();
  lcdReceive.clear();

}

void loop() {

  while (true)
  {
    if (Serial.available() > 0)
    {
      lcdReceive.clear();
      recvWithStartEndMarkers();
      showNewData();
    }
    int value = analogRead(A4); //assign analog pin A4 to read the voltage coming from the keypad

    for (int i = 0; i < 16; i++) {
      if (abs(value - thresholds[i]) < 5) //if a voltage value is close (+/- 5) to one of the threshold, use it
      {
        while (analogRead(A4) < 561)      //recieved voltage is in expected value range
        {
          delay(550);                     //wait 550ms before pressing another button
        }
        if (keypad[i]) {                  //if a key is pressed also show it on sending LCD
          lcdSend.print(keypad[i]);
          message += String(keypad[i]);
          if (message.startsWith("<") && message.endsWith(">"))
          {
            Serial.print(message.length());
            Serial.print(message);
          }
        }
      }
    }
  }
}

As you can see, I am using a single analogue pin of the arduino receive the pressed button on the keypad and the serial receive protocol from the sticky post (very useful)

Do I need a special custom library for defining the characters I want to encode on the 2 HEX symbols? If so, can you point/show me an example, please?

//i’m not just using simple ascii codes because custom codes can be used as a form of encryption

Also, can you point out any good tutorials regarding my second issue, aka, how to get the cursor on the LCD to move to the second row of the LCD module when I enter the 17th character?

Thank you very much for reading. Any help is welcomed :smiley:

Why don't you save yourself a lot of trouble and decode the hex digits that actually correspond to the ASCII characters, then? For example, "41" for 'A'.

"//i'm not just using simple ascii codes because custom codes can be used as a form of encryption"

The simplest form of encryption that takes a few seconds and a third graders skill at coding to break. If you want encryption use real encryption. If you don't then use ascii.

If you really must do that, use a look up table. Google it.

Logiman:
Also, can you point out any good tutorials regarding my second issue, aka, how to get the cursor on the LCD to move to the second row of the LCD module when I enter the 17th character?

I never researched if there was a tutorial. This will probably work (not tested).

void printText(char *txt)
{
  // temporary buffer to hold text for first line
  char buffer[17];

  if (strlen(txt) > 16)
  {
    // copy 16 bytes
    strncpy(buffer, txt, 16);
    // add terminating nul
    buffer[16]='\0';

    // print first line
    Serial.println(buffer);

    // print remainder on second line
    Serial.println(&txt[16]);
  }
  else
  {
    Serial.println(txt);
  }
}

aarg:
If you really must do that, use a look up table. Google it.

Thank you for the tip? can you help with a link to a similar application of the lookuptable?

Logiman:
Thank you for the tip? can you help with a link to a similar application of the lookuptable?

Sure. Here you go:
ASCII table

ASCII is a form of look up table. Given a number, you look in the table and find a character.

Happy to be of assistance. :slight_smile:

Conversely, given a character, you can search the ASCII table and return a number!

Of particular interest are the 65 characters between and including #32 (' ' or space) and #96 ('.' or period), which allow you to encode any conceivable message.