I have a project which is reading barcode scanner result then compare with an existed string. I have used the library USB host shield in order to read data from barcode scanner then I would like to compare the code from barcode with an existed code which I will define in an array. My approach is store the arrives data into buffer then compare the buffer but I have failed for several trials. Here is my code:

#include <hidboot.h>
#include <usbhub.h>

// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>

USB Usb;
USBHub Hub1(&Usb);
USBHub Hub2(&Usb);

  uint8_t i;
  class KbdRptParser : public KeyboardReportParser

    void OnKeyDown  (uint8_t mod, uint8_t key);
    void OnKeyPressed(uint8_t key);

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

/* what to do when symbol arrives */

 void KbdRptParser::OnKeyPressed(uint8_t key)
  const char* new_line = "\n";
  uint8_t rcode;
  uint8_t keylcl;
  char buffer [12];
    keylcl = key;
    int a = (int) char (keylcl);
    a = a - 48;

    buffer[i] = a;
    Result = char(a);
KbdRptParser Prs;

void setup()
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  Serial.println("\r\nADK demo start");

  if (Usb.Init() == -1) {
    Serial.println("OSCOKIRQ failed to assert");
    while(1); //halt
  }//if (Usb.Init() == -1...

  HidKeyboard.SetReportParser(0, &Prs);

  delay( 200 );

void loop()
  • What exactly "failed"? "store the arrives data into buffer..." - I don't see any attempts to do that in your code.
    – AnT
    May 2 '19 at 13:43
  • @AnT Dear Ant, I have updated my code with buffer attempt. But it show weird characters and I don't have much experience on Arduino so that I stuck at where to use the buffer. May 3 '19 at 4:21
  • 1
    You declare the buffer in your function. It will be deleted, when the function returns. Declare the buffer in global scope. And before adding a character, check, if the index i is bigger than the buffer has elements. Where is the string, that you want to compare with? Also the weird casting of key from uint8_t to char to int does not make sense. You can just directly subtract 48 from it and safe the result in a char variable.
    – chrisl
    May 3 '19 at 11:08

Your Answer

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

Browse other questions tagged or ask your own question.