0

My code returns uint8_t format when is cast to char and Display on LCD it works fine but I want to get that scanned code for further processing. not only to display on LCD 16x2 , but it returns some strange symbols. Any Idea?

#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <hid.h>
#include <hiduniversal.h> 
#include <usbhub.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <SPI.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
USB     Usb;
USBHub     Hub(&Usb);
HIDUniversal Hid(&Usb); 
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);

class KbdRptParser : public KeyboardReportParser
{
  void PrintKey(uint8_t mod, uint8_t key);
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)  
{
    uint8_t c = OemToAscii(mod, key);

    if (c)
        OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)  
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  

    if( millis() > next_time ) {
      lcd.clear();
      current_cursor = 0;
      delay( 5 );  //LCD-specific 
      lcd.setCursor( 0,0 );
    }//if( millis() > next_time ...

    next_time = millis() + 200;  //reset watchdog

    if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
      lcd.setCursor( 0,1 );
    }
  char keys=(char) key;
    Serial.println(keys);
// char add =keys.indexOf(5)+keys.indexOf(6)+keys.indexOf(7)+keys.indexOf(8);
    Serial.println(keys);
    lcd.print(keys);
};

KbdRptParser Prs;

void setup()
{
    Serial.begin( 115200 );
    Serial.println("Start");

    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }

    delay( 200 );

    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    lcd.clear();
    lcd.noAutoscroll();
    lcd.print("Scan Your Code");
    delay( 200 );
}

void loop()
{
  Usb.Task();
}
  • I have some difficulties in following your code, and you include the same file twice. Maybe you can review the code and highlight where the problem lies? – Vladimir Cravero Jul 18 '15 at 20:20
  • Where in your code does it return 'strange symbols'? – Bruce Abbott Jul 18 '15 at 20:26
  • @Bruce I have used this with scanning barcodes with my barcode reader, and it displays the barcode with numbers fine on LCD, but when I see the output of serial monitor, it show some stange symbols instead of numbers that display on LCD – TP Ap Jul 19 '15 at 2:20
0

A unit8_t is an 8 bit unsigned integer - it can have any value between 0 and 255, and can be compared to any numeric value in that range.

You can do "further processing" on the received uint8_t value whether it is a printable ASCII value or not.

  • Bennet I have used this with scanning barcodes with my barcode reader, and it displays the barcode with numbers fine on LCD, but when I see the output of serial monitor, it show some stange symbols instead of numbers that display on LCD – TP Ap Jul 19 '15 at 2:19
0

If the symbols look like Japanese, then probably bit 7 of your characters is set to 1. The upper 128 characters are reserved for Katakana in many LCDs. So, if you are sure the characters are correct, you may have to be AND them with 0x7F to assure they are in the right half of the character encoder.

Do check the requirements of your LCD, and the code in the library to control the display. In one occasion, you added a 5 ms delay, and in other clear(), you didn't. One of the annoyances of LCD displays is that you have to be careful with those delay, else you can land the controller in a strange mode,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.