Serial LCD, sending special commands

I have build this:

https://github.com/jimblom/Serial-LCD-Kit/blob/master/Serial_LCD_Kit.pde Everything works appart from the "special commands"

I tried a bunch of different things and the LCD always prints them as text, so I am guessing instead of sending the ASCI characters 0xFF I should actually send those in hexadecimal as raw data.

 Characters received in a serial stream are displayed on a 16x2,
20x4, etc. LCD. Special commands are also available to clear, scroll
display cursors and other functions.

From the code

/*----------------------------------------------------------
  In loop(), we wait for a serial character to be 
  received. Once received, the character is checked against 
  all the special commands if it's not a special command the 
  character (or tab, line feed, etc.) is displayed. If it is 
  a special command another loop will be entered and we'll 
  again wait for any further characters that are needed to 
  finish the command.
  ----------------------------------------------------------*/
void loop()
{
    while (Serial.available() > 0) {
      inKey = Serial.read();
      // Check for special LCD command
      if ((inKey&0xFF) == SPECIAL_COMMAND)
        SpecialCommands();
      // Backlight control
      else if ((inKey&0xFF) == BACKLIGHT_COMMAND)
      {
        // Wait for the next character
        while(Serial.available() == 0)
          ;
        setBacklight(Serial.read());
      }
      // baud rate control
      else if ((inKey&0xFF) == BAUD_COMMAND)
      {
        // Wait for the next character
        while(Serial.available() == 0)
          ;
        setBaudRate(Serial.read());
      }
      // backspace
      else if (inKey == 8)
      {
        Cursor--;
        LCDDisplay(0x20);
        Cursor--;
      }
      // horizontal tab
      else if (inKey == 9)
        Cursor += 5;
      // line feed
      else if (inKey == 10)
        Cursor += columns - Cursor%columns;
      // carriage return
      else if (inKey == 13)
        Cursor += columns;
      // finally (since no special commad received), just display the received character
      else
        LCDDisplay(inKey);
    }
}

/* ----------------------------------------------------------
  SpecialCommands() is reached if SPECIAL_COMMAND is received.
  This function will wait for another character from the serial
  input and then perform the desired command. If a command is
  not recognized, nothing further happens and we jump back into
  loop().
  ----------------------------------------------------------*/
void SpecialCommands()
{
  // Wait for the next character
  while(Serial.available() == 0)
    ;
  inKey = Serial.read();
  // Clear Display
  if (inKey == 1)
  {
    Cursor = 0;
    lcd.clear();

I only need to clear the display, or return to position 0 (home) for refrerence.

Any ideas?

I don’t know exactly how your sensors work, but reading the code is not as clear as it could be. My guess is that the following code would NOT work, but it may be easier to read. I’m offering it as an alternative to getting your code to do what youo are trying to do.

void loop()
{
  unsigned int keyCommand;

  while (Serial.available() > 0) {
    inKey = Serial.read();
    keyCommand = inKey & 0xFF;
  }
  // Check for special LCD command
  if (keyCommand == SPECIAL_COMMAND) {
    SpecialCommands();
    // Backlight control
  } else {                            // Other commands...
    while (Serial.available() == 0)
      ;
    inKey = Serial.read();
    keyCommand = inKey & 0xFF;
    switch (keyCommand) {
      case BACKLIGHT_COMMAND:
        setBacklight(Serial.read());
        break;

      case BAUD_COMMAND:
        setBaudRate(Serial.read());
        break;

      default:
        Serial.println("Should be here");
        break;
    }

    switch (inKey) {
      case 8:          // Backspace
        Cursor--;
        LCDDisplay(0x20);
        Cursor--;
        break;

      case 9:          // Horizontal tab
        Cursor += 5;
        break;

      case 10:         // Linefeed
        Cursor += columns - Cursor % columns;
        break;

      case 13:         // Carriage return
        Cursor += columns;
        break;

      default:         // Regular character
        LCDDisplay(inKey);
        break;
    }
  }
}