Manipulating data from a USB Barcode Scanner does not work

Dear All,

I have some trouble with my (barcode scanner - USB host shield - Arduino Uno) composition.
I have done everything according this topic: http://forum.arduino.cc/index.php?topic=449171.15
and I could read the string the same way as you written there.

My project is to use this string as a variable to control leds. More clearly the process should be like this:

When i scan a barcode i have to attach this string some LEDs (outputs)
When i scan an othe code i have to attach this string some other LEDs.
and so on until 7 barcodes.

And when a read back one of these barcodes, the attached LEDs should be light up.

I have used some adressable led strips and serial monitor to try the mechanism like this code:

#include <FastLED.h>
#include "colorutils.h"
#include "colorpalettes.h"
#include <LiquidCrystal.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define NUM_LEDS    24      // adjust this to the number of LEDs you have: 16 or more
#define LED_TYPE    WS2812B // adjust this to the type of LEDS. This is for Neopixels
#define DATA_PIN    14      // adjust this to the pin you've connected your LEDs to   
#define BRIGHTNESS  64      // 255 is full brightness, 128 is half
#define SATURATION  255     // 0-255, 0 is pure white, 255 is fully saturated color
#define COLOUR      5       // 0-255 HUE chart
#define COLOR_ORDER GRB
#define NUM_MODES 2
#define UPDATES_PER_SECOND 100
CRGB leds [NUM_LEDS];
int ledMode = 0;
CRGBPalette16 currentPalette;



String barcodeOne;
String barcodeTwo;
String barcodeThree;



void setup() {
  // put your setup code here, to run once:
  delay( 2000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness( BRIGHTNESS );
  allDark ();

  lcd.begin(16, 2);     // set up the LCD's number of columns and rows:
  Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  writeOne:
  Serial.println("Enter barcode1! ");
  lcd.print ("Enter barcode1! ");
  while (Serial.available()==0);                  //waiting for input
  barcodeOne = Serial.readString();               // read srting
  sectorOne();

  Serial.print("the barcode1 is: ");
  Serial.print(barcodeOne);
  lcd.clear();
  lcd.print ("the barcode1 is: ");
  lcd.setCursor (0, 1);
  lcd.print (barcodeOne );

  Serial.println("Enter barcode2! ");
  lcd.clear();
  lcd.print ("Enter barcode2! ");
  ceck:

  while (Serial.available()==0);                 //waiting for input
  barcodeTwo = Serial.readString();             // read srting
  if (barcodeTwo == barcodeOne){
    Serial.print ("Enter new code");
    lcd.print ("Enter new code");
    goto ceck;
  }
  sectorTwo();

Serial.print("the barcode2 is: ");
Serial.print(barcodeTwo);
lcd.clear();
lcd.print ("the barcode2 is: ");
lcd.setCursor (0, 1);
lcd.print (barcodeTwo ); 


 
 Serial.println("Enter barcode3! ");
 lcd.clear();
  lcd.print ("Enter barcode3! ");

 ceckTwo:

while (Serial.available()==0) {                 //waiting for input
  }
  barcodeThree = Serial.readString();           // read srting
  if (barcodeThree == barcodeTwo||
      barcodeThree == barcodeOne)
  {Serial.print ("Enter new code" );
  lcd.print ("Enter new code");
 goto ceckTwo;
  }
  sectorThree();
 
Serial.print("the barcode3 is: ");
Serial.print(barcodeThree);
lcd.clear();
lcd.print ("the barcode2 is: ");
lcd.setCursor (0, 1);
lcd.print (barcodeThree );


Serial.println("Enter a barcode! ");
lcd.clear();
lcd.print ("Enter a barcode! ");

while (Serial.available()==0) {                 //waiting for input
  }
  String readBarcodeOne = Serial.readString();
  lcd.clear();
if (readBarcodeOne == barcodeOne)
  {sectorOne();
  goto readOne;}
if (readBarcodeOne == barcodeTwo)
  {sectorTwo();
  goto readOne;}
if (readBarcodeOne == barcodeThree);
  {sectorThree();
  goto readOne;}
readOne:
Serial.println("Enter a barcode! ");
lcd.print ("Enter a barcode! ");

while (Serial.available()==0) {                 //waiting for input
  }

String readBarcodeTwo = Serial.readString();
lcd.clear();

if (readBarcodeTwo == readBarcodeOne)
{Serial.println ("SZLAMB, EZ MÁR VOLT! Please enter a barcode! ");
lcd.print ("SZLAMB!");
lcd.setCursor (0, 1);
lcd.print ("Enter a barcode!");
    goto readOne;}
   
if (readBarcodeTwo == barcodeOne)
   {sectorOne();
  goto readTwo;}
if (readBarcodeTwo == barcodeTwo)
   {sectorTwo();
  goto readTwo;}
if (readBarcodeTwo == barcodeThree);
  {sectorThree();
  goto readTwo;}

readTwo:
Serial.println("Enter a barcode! ");
lcd.print ("Enter a barcode! ");


while (Serial.available()==0) {                 //waiting for input
  }

String readBarcodeThree = Serial.readString();
lcd.clear();

if (readBarcodeThree == readBarcodeOne ||
    readBarcodeThree == readBarcodeTwo)
{Serial.println ("SZLAMB, EZ MÁR VOLT! Please enter a barcode! ");
lcd.print ("SZLAMB!");
lcd.setCursor (0, 1);
lcd.print ("Enter a barcode!");
    goto readTwo;}
   
if (readBarcodeThree == barcodeOne)
   {sectorOne();
  goto writeOne;}
if (readBarcodeThree == barcodeTwo)
   {sectorTwo();
  goto writeOne;}
if (readBarcodeThree == barcodeThree);
  {sectorThree();
  goto writeOne;}


 
}



void sectorOne () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[0]= CHSV (COLOUR,SATURATION,BRIGHTNESS);  //HUE chart
  leds[1]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[10]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[11]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[12]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[13]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[22]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[23]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void sectorTwo () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[2]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[3]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[8]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[9]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[14]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[15]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[20]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[21]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void sectorThree () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[4]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[5]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[6]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[7]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[16]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[17]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[18]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[19]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void allDark () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  FastLED.show();
  delay (100);
}

The next code is in the next post…

and I am trying to do the same with this, but I can’t do:

// Include Usb/HID libs
//------------------------------//

#include <hid.h>
#include <hiduniversal.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#define MAXQRCODE  50 // NEEDS TO BE LONG ENOUGH TO FIT YOUR LONGEST BARCODE SCAN
//----------------------------//
#include <LiquidCrystal.h>
//-------------------------------//
#include <FastLED.h>
#include "colorutils.h"
#include "colorpalettes.h"
#define NUM_LEDS    24      // adjust this to the number of LEDs you have: 16 or more
#define LED_TYPE    WS2812B // adjust this to the type of LEDS. This is for Neopixels
#define DATA_PIN    14      // adjust this to the pin you've connected your LEDs to   
#define BRIGHTNESS  64      // 255 is full brightness, 128 is half
#define SATURATION  255     // 0-255, 0 is pure white, 255 is fully saturated color
#define COLOUR      5       // 0-255 HUE chart
#define COLOR_ORDER GRB
#define NUM_MODES 2
#define UPDATES_PER_SECOND 100
CRGB leds [NUM_LEDS];
CRGBPalette16 currentPalette;

// initialize the LCD library with the numbers of the interface pins
#define DISPLAY_WIDTH 16
#define DISPLAY_HEIGHT 2
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
//---------------------------------------//


USB     Usb;
USBHub     Hub(&Usb);
HIDUniversal    Hid(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
const unsigned long timeOutDuration = 200ul;
 
//----------------------------------------------------//
class KbdRptParser : public KeyboardReportParser
{
  public:
    KbdRptParser();
    void resetString();
    bool newCar;
    bool newString;
    uint8_t currentCursor;
    char QRCodeBuffer[MAXQRCODE + 1];
  protected:
    virtual void OnKeyDown (uint8_t mod, uint8_t key);
    virtual void OnKeyPressed(uint8_t key);
};

KbdRptParser::KbdRptParser(): KeyboardReportParser()
{
  resetString();
}

void KbdRptParser::resetString()
{
  newCar = false;
  newString = false;
  currentCursor = 0;
  QRCodeBuffer[0] = '\0';
}

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)
{
  QRCodeBuffer[currentCursor++] = key;
  QRCodeBuffer[currentCursor] = '\0';
  if (currentCursor >= MAXQRCODE) currentCursor = MAXQRCODE - 1; // not enough space, next character will overwrite last one
  newCar = true;
  newString = true;
}
//-----------------------------------------------------------------------//


KbdRptParser Prs;

//-------------------//
const char * barcodeOne;
const char * barcodeTwo;
const char * barcodeThree;
boolean kapcsOne = true;
boolean kapcsTwo = true;
boolean kapcsThree = true;
boolean kulcsOne = true;
boolean kulcsTwo = true;
boolean kulcsThree = true;

//-------------------//
void setup()
{   
    delay( 2000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
    FastLED.setBrightness( BRIGHTNESS );
    allDark ();
    lcd.begin(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    Serial.begin(115200);
    Serial.println("Start");
    lcd.print ("Enter barcode");
    if (Usb.Init() == -1) {
    Serial.println("OSC did not start. Stop here.");
    while (1); // No need to continue.
  }
    delay( 200 );
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
}



//----------------------------------------------//
void loop() {
  static uint32_t lastKeyTime = 0;      //watchdog
  static bool timedOut = false;


    Usb.Task();

 
if (Prs.newCar) { // we just got a new char; you'll see the string building up
    Serial.print("["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
    Prs.newCar = false; // new char has been handled
    lastKeyTime = millis(); // memorize the last char entry time
  }

  // here we check if there has been no activity from the barcode reader for a while
  if (Prs.newString && kapcsOne && (millis() - lastKeyTime >= timeOutDuration)) { //timeout, we just finsihed entering a new string
    barcodeOne=Prs.QRCodeBuffer;
    Serial.print("Final string1 = ["); Serial.print(barcodeOne); Serial.println("]");
    sectorOne();
    kapcsOne = false;
    Prs.resetString(); // tell the Prs instance that the new string has been handled.
  }
  if (Prs.newString && !kapcsOne && kapcsTwo && (millis() - lastKeyTime >= timeOutDuration)) { //timeout, we just finsihed entering a new string
    barcodeTwo=Prs.QRCodeBuffer;
    Serial.print("Final string2 = ["); Serial.print(barcodeTwo); Serial.println("]");
    sectorTwo ();
    kapcsTwo = false;
    Prs.resetString(); // tell the Prs instance that the new string has been handled.
  }
  if (Prs.newString && !kapcsOne && !kapcsTwo && kapcsThree && (millis() - lastKeyTime >= timeOutDuration)) { //timeout, we just finsihed entering a new string
    barcodeThree=Prs.QRCodeBuffer;
    Serial.print("Final string3 = ["); Serial.print(barcodeThree); Serial.println("]");
    sectorThree ();
    kapcsThree = false;
    Prs.resetString(); // tell the Prs instance that the new string has been handled.
  }

 
}

//----------------------------------//
void sectorOne () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[0]= CHSV (COLOUR,SATURATION,BRIGHTNESS);  //HUE chart
  leds[1]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[10]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[11]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[12]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[13]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[22]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[23]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void sectorTwo () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[2]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[3]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[8]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[9]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[14]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[15]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[20]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[21]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void sectorThree () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  leds[4]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[5]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[6]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[7]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[16]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[17]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[18]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  leds[19]= CHSV (COLOUR,SATURATION,BRIGHTNESS);
  FastLED.show();
  delay (100);
}
void allDark () {
  fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
  FastLED.show();
  delay (100);
}

//-----------------------------------------------//

Can you help me how can I do the same mechanism like in the first code?

Thank you in advance,

Attila Tamás

Please don't send PMs. they are unwanted / not welcome.

you need to rethink the way you architected your program. It would probably be better with a state machine. Also don't use the String class, that will play tricks on you...

Some links that would be useful for you to explore:

Several Things at a Time

Serial Input Basics and use those libraries functions to play with what you got from the Serial line: stdlib.h and string.h

also this would probably help get rid of goats
Planning and Implementing a Program