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());
}*/