0

After trying for a good month.. looking like a Newbie I have not managed to log the result of barcode scanner , using rtc SD module and a waveshare scanner connected via UART to ports 2 and 3 from the arduino.

Main idea was being able to log into the SD card under the header Barcode and time, the barcode scanned and time of scan.

I know how to create files with current date + int. CSV , consecutively if one was already created, deleted them...etc

However as by not reaching the desired result I reduced and further reduce the code so at the end I am happy that the barcode I scan is logged into the SD card.

It follows mess--code..not messy enough I have some more ...honestly..anyway

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <Wire.h>
#include "RTClib.h"
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial = SoftwareSerial (rxPin, txPin);

RTC_DS1307 RTC;
File myFile; // file to be created and where data is to be to stored

char c = ' ';         // stores the latest character from the serial input  buffer
int length = 30;      // Max. lenght of te buffer
char buffer [31];     // Char Array to store theincoming data
char termChar = 10;   // terminating character
byte index = 0;       // index position of the bufffer, so where the next character t come shall be copied to
boolean haveNewData = false; // Flag to tell the sketch we have data
const int chipSelect = 10; //CS pin For SD card.


void setup()  {

  Serial.begin(9600);

  mySerial.begin(9600);

  Wire.begin();       

  if (!RTC.begin()) {
    return;
  }

  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  myFile = SD.open("test.CSV", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to file...");
    myFile.println("Hello BarWorld");
    myFile.close();
  }
}


// right now I scann a barcode



void loop()
{    
  readmySerial();
  if ( haveNewData ) {  processNewData();  }
}




void readmySerial()
{
     if (mySerial.available())
    {
      c = mySerial.read();
      if (c != termChar)
      {
        buffer[index] = c;
        index = index + 1;
      }
      else
      {
        buffer[index] = '\0';
        index = 0;
        haveNewData = true;
      }
  }
}

 
 
void processNewData()
{
  myFile = SD.open("test.CSV", FILE_WRITE);
  if (myFile) {
  myFile.println(buffer);
  myFile.close();}
  }
   
  
  • Why did you repost your question? – chrisl Jul 17 at 20:52
  • because the code it was wrong, I forgot to add the right voidloop - it appeared as the function "readmySerial" in the other post...and somehow I couldn't remake it all in one just one ode again...sorry... my bad.. – Maximilian1988 Jul 17 at 20:55
  • 1
    You didn't ask a question nor did you describe the problem you are facing – Sim Son Jul 17 at 21:56
  • 1
    whit part of scanning barcode into a file does not work? – jsotola Jul 18 at 0:17
  • 1
    @Maximilian1988 You know, that you can always edit your question to correct or add information to it? My comment from before is now lost, since you deleted the question. Have you tried inserting some Serial prints for debug? – chrisl Jul 18 at 14:00
0

I found the way:

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <Wire.h>
#include "RTClib.h"
#define rxPin 2
#define txPin 3
RTC_DS1307 RTC;
SoftwareSerial mySerial = SoftwareSerial (rxPin, txPin);
File main_folder; // initialize folder for saving
File dataFile; // initialize sd file
const int chipSelect = 10; // CS pin on sd card module
int prev_file_indx = 0; // used for file naming
String fileName = "000";
char timestamp[30];


// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;


void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now = RTC.now();
  sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year() - 2000);
  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}


void setup ()
{
  Serial.begin (115200);
  mySerial.begin (115200);
  Wire.begin();       
  if (!RTC.begin()) {
    return;
  }

  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
      return;
    }
    
    main_folder = SD.open("/");
    fileName = sd_saver(main_folder);
 // end of setup
    delay(1500);
    dataFile = SD.open("DATA"+fileName+".csv",FILE_WRITE);
  if (dataFile) {
    Serial.println(dataFile);
    Serial.println("Writing Headers...");
    dataFile.println("FA,  Zeit");
    dataFile.close(); // close the file
    Serial.println("File closed, Headers written");
  }
 }

void process_data (const char * data)
// here to process incoming serial data after a terminator received
{ delay(500);
    SdFile::dateTimeCallback(dateTime);  
  DateTime now = RTC.now();
  sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year() - 2000);
     dataFile = SD.open("DATA"+fileName+".csv",FILE_WRITE);
  if (dataFile) {
    Serial.println("Writing to file...");
    dataFile.print (data);
    dataFile.print(",");
    dataFile.println (timestamp);
    Serial.println (data);
    Serial.println (timestamp);
    Serial.println("Done.");
    dataFile.close();
  } else {
    Serial.println("error opening test.txt");
    delay(1000);
  }

}



void processIncomingByte (const byte inByte)
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
  {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0;
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

  }  // end of switch

} // end of processIncomingByte

void loop() {
  delay(2000);


  // if serial data available, process it
  while (mySerial.available () > 0)
  processIncomingByte (mySerial.read ());
  mySerial.write(processIncomingByte);
  delay(1000);

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop



String sd_saver(File dir){
  while(true){
    // iterate all files to ensure no overwrites
    File entry = dir.openNextFile();
    if (!entry){
      break;
    }
    // naming routine
    String entry_name = entry.name();
    if ((entry_name.substring(4,7)).toInt()>=prev_file_indx){
      prev_file_indx = (entry_name.substring(4,7)).toInt()+1;
      if (prev_file_indx>=100){
        fileName = String(prev_file_indx);
      } else if (prev_file_indx>=10){
        fileName = "0"+String(prev_file_indx);
      } else{
        fileName = "00"+String(prev_file_indx);
      }
    }
    entry.close();
  }
  return fileName;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.