Hi all,
I've been working with a barcode scanner, trying to take the barcode numbers and translate them into prices and output that to an lcd screen. I've found how to save the input keys into a char variable using the following, where the input keys originally came in as uint8_t variable:
char Str2[13] = {(char)key};
Serial.println( Str2 );
lcd.print( Str2 );
This prints out the following to the serial monitor:
Start
0
2
8
4
0
0
0
9
0
8
5
8
And it also nicely prints out to the lcd screen. But this isn't what I want, I want to read/modify/write that input before it prints and change it.
I feel like it's pretty simple, Str2 is 12 characters long, so I've built another string the same length to compare it to:
char Str1[13] = {'0', '2','8','4','0','0','0','9','0','8','5','8'};
But I printed this string and it prints it 13 times???:
Start
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
028400090858
How can I create this second string right so I can match it to the incoming one???
When I can effectively compare them, I can create an if statement to output certain prices based off of what barcode numbers get scanned, which is the ultimate goal.
Thanks for any help you can give!
Here's the rest of my code:
/*
Portable barcode scanner. Uses USB HID barcode scanner, Arduino Board, USB Host Shield and HD44780-compatible LCD display
The circuit:
- LCD RS pin to digital pin 7
- LCD Enable pin to digital pin 6
- LCD D4 pin to digital pin 5
- LCD D5 pin to digital pin 4
- LCD D6 pin to digital pin 3
- LCD D7 pin to digital pin 2
- LCD R/W pin to ground
- 10K resistor:
- ends to +5V and ground
- wiper to LCD VO pin (pin 3)
*/
#include <hid.h>
#include <hiduniversal.h>
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
//#include <avrpins.h>
//#include <max3421e.h>
//#include <usbhost.h>
//#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
//#include <address.h>
#include <hidboot.h>
//#include <printhex.h>
//#include <message.h>
//#include <hexdump.h>
//#include <parsetools.h>
#define DISPLAY_WIDTH 16
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
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 Str2[13] = {(char)key};
char Str1[13] = {'0', '2','8','4','0','0','0','9','0','8','5','8'};
Serial.println( Str1 );
lcd.print( Str1 );
};
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("Ready");
delay( 200 );
}
void loop()
{
Usb.Task();
}