Bar Code System help

Hi All,

I am working on a bar code system application. The idea is to read a bar code and store its information into a SD card along with time and date. I dont know why it is not returning the data from the Bar code. The same code used to work properly before.

#include <hidboot.h>
#include <hiduniversal.h>
#include "RTClib.h"
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> 
const int chipSelect = 4;
String DataBarcode;
bool x = false;
int counter=0;

RTC_DS3231 rtc;
#include <Arduino.h>

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

class KbdRptParser : public KeyboardReportParser
{
    void PrintKey(uint8_t mod, uint8_t key);

  protected:
    void OnControlKeysChanged(uint8_t before, uint8_t after);

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

void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
  MODIFIERKEYS mod;
  *((uint8_t*)&mod) = m;
}

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

  if (c)
    OnKeyPressed(c);
}

void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {

  MODIFIERKEYS beforeMod;
  *((uint8_t*)&beforeMod) = before;

  MODIFIERKEYS afterMod;
  *((uint8_t*)&afterMod) = after;
}

void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
  //Serial.print("UP ");
  //PrintKey(mod, key);
}

void KbdRptParser::OnKeyPressed(uint8_t key)
{
  //Serial.print((char)key);
  if (key == 0x0D){
    x = true;
  }else{
    DataBarcode += (char)key;
  }
}


USB Usb;
HIDUniversal Hid(&Usb);

KbdRptParser Prs;


void setup()
{
 
  Remove_SDcard();
  Initialize_RTC();
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  lcd.setCursor (3,0); // go to start of 2nd line
  lcd.print("Welcome"); 
  lcd.setCursor (2,1); // go to start of 2nd line
  lcd.print("Robot Welding"); 
  Serial.begin( 115200 );
  Serial.println("Serial begin");
  Initialize_SDcard();
  Serial.println("initialize end!");

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 200 );

  Hid.SetReportParser(0, &Prs);
}

void loop()
{
  //lcd.setCursor(16,1);
  //lcd.autoscroll();    // Set diplay to scroll automatically
  //lcd.print(" ");      // set characters
  //delay(700);    
  Usb.Task();
  if (x){
    Write_SDcard();
    //Serial.println(DataBarcode);
    //lcd_display();
    x = false;
    DataBarcode = "";
    delay(1000); 
    
    
  }


}


void Remove_SDcard()
{
  SD.remove("TankLog.txt");
}

void Write_SDcard()
{
    // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.

  counter= ++counter;
  File dataFile = SD.open("TankLog.txt", FILE_WRITE);
  DateTime now = rtc.now();
  
 
  
  Serial.begin(115200);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(now.day(), DEC);
    dataFile.print("/");
    dataFile.print(now.month(), DEC);
    dataFile.print("/");
    dataFile.print(now.year(), DEC); //Store date on SD card
    dataFile.print("  ");

    // Serial print
    Serial.print(now.day());
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.year(), DEC); //Store date on SD card
    Serial.print("  ");


    
    //dataFile.print(","); //Move to next column using a ","
    dataFile.print(now.hour(), DEC); //Store date on SD card
    dataFile.print(":");
    dataFile.print(now.minute(), DEC);
    dataFile.print(":");
    dataFile.print(now.second(), DEC);
    dataFile.print("  ");

    // Serial print
    Serial.print(now.hour(), DEC); //Store date on SD card
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.print(now.second(), DEC);
    Serial.print("  ");
    
    
    //dataFile.print(","); //Move to next column using a ","
    dataFile.print(DataBarcode);
    dataFile.print("  ");
    
    //Serial print
    Serial.print(DataBarcode);
    Serial.print("  ");
    Serial.println();

    //LDC print
    //
    lcd.clear ();
    //lcd.print("Registering...");
    //delay(200);
    //lcd.clear ();
    lcd.setCursor (0,0);
    lcd.print("ID: ");
    lcd.print(DataBarcode);
    delay (100);
    lcd.setCursor (0,1);
    lcd.print("Entries: ");
    lcd.print(counter);

   
  
    //lcd.print("Entered");

   // dataFile.print(DHT.temperature); //Store date on SD card
   // dataFile.print(","); //Move to next column using a ","

   // dataFile.print(DHT.humidity); //Store date on SD card
   // dataFile.print(","); //Move to next column using a ","

    dataFile.println(); //End of Row move to next row
    dataFile.close(); //Close the file
    
    //Serial.println("Delaaaaaaaaaaaay!");
    //delay(10000000);
    //Serial.println("I'm free!!!");

    // read all the available characters
 
    //delay(100);
    // clear the screen
    //lcd.clear ();
    //lcd.print("Tank Counter:");
    //lcd.setCursor (0,1); // go to start of 2nd line
    //lcd.print(counter);
  }
  else
  Serial.println("OOPS!! SD card writing failed");
}

void Initialize_SDcard()
{
  DateTime now = rtc.now();
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("TankLog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print("Tanks Produced "); //Write the first row of the excel file
    dataFile.print(now.day(), DEC);
    dataFile.print("/");
    dataFile.print(now.month(), DEC);
    dataFile.print("/");
    dataFile.print(now.year(), DEC); //Store date on SD card
    dataFile.print("  ");

    dataFile.print(now.hour(), DEC); //Store date on SD card
    dataFile.print(":");
    dataFile.print(now.minute(), DEC);
    dataFile.print(":");
    dataFile.print(now.second(), DEC);
    dataFile.println();
    dataFile.close();
  }
}
void Initialize_RTC()
{
   if (rtc.lostPower()) {
    Serial.println(F("RTC lost power, lets set the time!"));
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
   }
   

   // Initialize the rtc object
  rtc.begin();

  // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
   // rtc.adjust(DateTime(2020, 9, 24, 12, 27, 20));

//#### The following lines can be uncommented to set the date and time for the first time###  
/*
rtc.setDOW(FRIDAY);     // Set Day-of-Week to SUNDAY
rtc.setTime(18, 46, 45);     // Set the time to 12:00:00 (24hr format)
rtc.setDate(6, 30, 2017);   // Set the date to January 1st, 2014 
*/
}



/*void Read_DateTime()
{  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
}*/

It would help us to help you if you told us:

  • which Arduino board you are using
  • which SD module you are using
  • which barcode reader you are using
  • how you've connected the various parts together

How have you formatted your SD card? Have a read of the sticky at the top of the Storage section of the board.

Please post the same code which must, of course be different to what you have in front of you now, that worked before.

a7

It looks like you’ve stitched several bits of code together, without being careful.

It might be worth going back to the beginning and planning your development.

Hi Mark,

I am using the XC4386 SD card module, USBHost shield MAX3421, RCT module, Arduino UNO. Bar code reader: Honeywell S/N: 18156b42bf.
Yes. The SD card has been formatted. This is the latest version of the code. It was working before.


OK, so if I understand, the code has not changed, and the project doesn’t work.

What’s left to consider?

I think that jumble of wires and oddly arranged modules should be rectified. You have a loose, missing or incorrect connection more probably than a broken module.

Can you test the modules independently with code you didn’t write, as you might have done before wiring it all up? One at a time?

Do you have multiple copies of the modules you can swap in and out?

We’ve seen less pretty wiring, but srsly it taxes my mind to even look at that, and it is making your work harder.

TBC my stuff looks like that, so I know of what I speak when I rant about the costs you are incurring… needlessly.

Bolt it all down, dress the wires and verify it against the schematic you are working from and see if things change.

We’d all like to see that schematic, so post a celly of it. Even if you sure it isn’t the problem.

Also, did you ever go beyond describe the current behaviour other than to say it doesn’t work? Some dets about that would not hurt.

Many times in preparing to seek help here I find the dumb thing that was the prob.

a7

What is being shown on Serial Monitor? The progress messages in your sketch are there to help figure out where the sketch is going wrong. If they don't provide enough information, add more messages.

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