'Trying To Get A Barcode Scanner Working With Arduino
I'm trying to make my barcode scanner work with my Arduino Mega (I'm using the Honeywell Eclipse 5145) Despite the help I've received from my other two questions I still can't seem to get it working fully.
I have the Barcode Scanner sending the barcode number into the Serial Monitor properly but I've tried to add an If Statement to the variable so that
If ([Placeholder]==(char)ascii {
digitalWrite(22, HIGH);
delay(1000);
digitalWrite(22, LOW);
}
But this doesn't work. I've been told that the function ascii only stores one character at a time so it would output the characters one by one and not in one string. I've been trying to solve this for a while now but can't seem to get it to work. Also I don't know whereabouts i should put the code in my project.
Here's my current code,
#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
char scan;
//Barcodes
char Barcode1[] = {'7','6','2','2','2','1','0','4','1','1','0','0','6','\0'};
char Barcode2[] = {"8717644012208 "};
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 (buf[2] == 1 || buf[2] == 0) return;
for (uint8_t i = 7; i >= 2; i--) {
if (buf[i] == 0) continue;
if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
OnScanFinished();
}
else {
OnKeyScanned(i > 2, buf, buf[i]);
}
return;
}
}
uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
if (VALUE_WITHIN(key, 0x04, 0x1d)) {
if (upper) return (key - 4 + 'A');
else return (key - 4 + 'a');
}
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) {
uint8_t ascii = KeyToAscii(upper, mod, key);
Serial.print((char)ascii);
scan = (char)ascii;
}
void MyParser::OnScanFinished() {
Serial.println(" - Finished");
}
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser Parser;
void setup() {
Serial.begin( 115200 );
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
delay( 200 );
Hid.SetReportParser(0, &Parser);
}
void loop() {
Usb.Task();
}
I think I somehow need to have a variable that stores the data when all the characters are received from the function ascii and then use that variable in the If Statement instead of (char)ascii. If you have any questions please comment below and I'll try my best to answer (I'm not that familiar with ascii)
Please provide example if possible
Thanks,
Solution 1:[1]
class MyParser : public HIDReportParser {
public:
MyParser();
void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
protected:
char KeyToAscii(bool upper, uint8_t mod, uint8_t key);
virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
virtual void OnScanFinished();
};
char MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
if (VALUE_WITHIN(key, 0x04, 0x1d)) {
if (upper) return (key - 4 + 'A');
else return (key - 4 + 'a');
}
else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
}
return 0;
}
This allows
void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {
scan = KeyToAscii(upper, mod, key);
Serial.print(scan);
}
which gives a sample to Scheff's and my comments. Your question resp. your problem with comparing characters and setting Arduino outputs is not yet clear to me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
