How to run the motor using QR Reader

I want to implement a code that runs the motor when the QR code number I read using the QR reader is 210305***, but I don't know how to do it, I need some help
I'm using this Barcode Module.

#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
#include <Servo.h>
#define SERVO_PIN  8

#define CW   1900
#define STOP 1450
#define CCW  1000

Servo myServo;

char c;
uint8_t barcode;
String b_code;

class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
  protected:
    uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
    virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
    virtual void OnScanFinished();
};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  // If error or empty, return
  if (buf[2] == 1 || buf[2] == 0) return;

  for (uint8_t i = 7; i >= 2; i--) {
    // If empty, skip
    if (buf[i] == 0) continue;

    // If enter signal emitted, scan finished
    if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
      OnScanFinished();
    }

    // If not, continue normally
    else {
      // If bit position not in 2, it's uppercase words
      OnKeyScanned(i > 2, buf, buf[i]);
    }

    return;
  }
}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
  // Letters
  if (VALUE_WITHIN(key, 0x04, 0x1d)) {
    if (upper) return (key - 4 + 'A');
    else return (key - 4 + 'a');
  }

  // Numbers
  else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
    return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
  }

  return 0;
}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key){
  barcode = KeyToAscii(upper, mod, key);
  b_code=(String((char)barcode));
  Serial.print(b_code);

}

void MyParser::OnScanFinished() {
  Serial.println(" - Finished");
}

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

void setup() {
  Serial.begin( 115200 );
  Serial.println("Start");
  myServo.attach(SERVO_PIN);
  myServo.writeMicroseconds(STOP);
 // MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
 // barcode = KeyToAscii(upper, mod, key);
  //Serial.print(String((char)barcode));
 
  if (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
  }

  delay( 200 );

  Hid.SetReportParser(0, &Parser);
}

void loop() {
  /*Usb.Task();
  while (Serial.available() > 0) {
    if(b_code == 2103051699626601){
      Serial.print("completed");
      myServo.writeMicroseconds(CW);
      delay(1000);
      myServo.writeMicroseconds(STOP);
      break;
    }
    
 }*/
} 

providing a picture of the barcode-scanner with chinese symbols is too less information to really help you.
This barcode-reader has a datasheet. Attach the english datasheet to a posting.

If there is no english written datasheet you have to do the work of translating it using google-translate. This works very well. Mark. Copy & paste the chinese symbols into google-translate

You seem to have copy & pasted code you found somewhere.
Well: - ...---- if you don't have to the exact same hardware wired in the exact same way as in the project the code comes from you have to make checkings and adaptions.

So first step is to use a very simple testprogram to see f the scanner works at all.
And that is the reason why a datasheet is required.

Without a datasheet you are facing the downside of buying the cheapest device from a chinese online-shop. The "price" is investing time to obtain the nescessary informations to make it work.

If you have a datasheet attach it
best regards Stefan

On an Arduino I would ignore the USB interface and use the TTL interface. Connect the "TXD" pin to a data pin and use SoftwareSerial() to read data from that pin. They don't mention baud rate so you will have to experiment. Once you can display data from the reader on Serial Monitor you can show us the data format and we can help with code to recognize a particular input.

https://www.dyscan.com/sale-11029623-flatbed-embedded-2d-scan-engine-usb-communication-with-wide-angle-scanning-dp8402.html

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.