Opening SD card file with variable created from barcode scanner

Hi,

Trying to open this file (34306505.TXT) on my sd card by scanning in the corresponding barcode for it.

I print out the char array right before I run SD.open and it looks perfect but it won't recognise it.

I've been trying and searching around for ages but now I'm really stuck, any help would be fantastic.

I've never really programmed before so the code might be a bit of a mess, feel free to get me to clarify any bits.

Thanks!

Tom

All of my Code -

#include <PS2Keyboard.h>
#include <SPI.h>
#include <SD.h>


PS2Keyboard keyboard;
int option;
char input[16];
File myFile;


char keyboardRead(){
  
  // Gets input from keyboard / scanner and stores it in the input array, this works perfectly I think
  
  char line[16];
  int count = 0;
  
  for(int i=0;i<17;i++){
      input[i] = 0;
  }
  
    for(int i=0;i<17;i++){
      line[i] = 0;
  }

  while(1){
     if(keyboard.available()){
        char kb = keyboard.read();
      
        if(kb != PS2_ENTER){
            line[count] = kb;
            count++;  }
        else{
          line[count++] = '\0';
            for(int i=0;i<17;i++){
              input[i] = line[i];
             
            }
              break;
              }  
            }
          }
}

void transaction(){
  
  
  int price;
  float pricePounds;
  int total = 0;
  String description;

  
  Serial.println("TRANSACTION MODE");

  /*Scan Products
		Total Price
		Deduct price from card
		Finish */

 keyboardRead();
 
 //THIS IS THE ISSUE AREA
 
  char fileName[] = {input[5],input[6],input[7],input[8],input[9],input[10],input[11],input[12],'.','T','X','T', '\0'};
  
  // THE BARCODE IS TOO LONG TO USE THE WHOLE BARCODE AS THE FILE NAME, SO I'M CUTTING OUT THE FIRST SECTION TO CONFORM TO 8.3 
  Serial.println(fileName); // PRINTS OUT THE FILE NAME PERFECTLY
  
 myFile = SD.open(fileName); // DOESNT WORK
 
  if(!myFile){
    Serial.println("Product not Found");
   transaction();
    }
  price = myFile.parseInt();
  pricePounds = (float)(price)/(100);
  description = myFile.readStringUntil('.');
  Serial.print(description); Serial.print(" - "); Serial.write(0xA3); Serial.println(pricePounds);
  total = total + price;
  while(1); 
  


}


void setup(){
  Serial.begin(9600);
  delay(2000);
  keyboard.begin(8,2);
  pinMode(4, OUTPUT);
  
    if (!SD.begin(4)){
    Serial.println("Product Database not Found");
    while(1);}

}

void loop(){
  Serial.println("Welcome......");
  Serial.println("Please select one of the following options");
  Serial.println("1) Transaction Mode");
  Serial.println("2) Topup");
  Serial.println("3) Card Managment");
  Serial.println("4) Product Managment");
  
 keyboardRead();
 
//MENU OPTION, ONLY WRORKING ON TRANSACTION ATM


  switch ((int)input[0]) {
    case 49:
      transaction();
      break;
    case 50:
      Serial.println("Top");
      break;
    case 51:
      Serial.println("Card");
      break;
    case 52:
      Serial.println("Prod");
    default:
      Serial.println("ERROR"); 
  } 
  while(1);
}
char line[16];
...  
    for(int i=0;i<17;i++){
      line[i] = 0;

That's not going to go well.

AWOL:

char line[16];

... 
    for(int i=0;i<17;i++){
      line[i] = 0;



That's not going to go well.

Thats just to clear the array every time the function is called. Whats the problem with that?

I should also mention that it works when I manually type in the characters instead of using input[whatever].

Thats just to clear the array every time the function is called.

And then a little bit extra.

Don't write to memory you don't own

AWOL:

char line[16];

... 
    for(int i=0;i<17;i++){
      line[i] = 0;



That's not going to go well.

Oh thats set it to all be null hasn't it? Is that bad?

AWOL:

Thats just to clear the array every time the function is called.

And then a little bit extra.

Don't write to memory you don't own

Sorry I'm new to this I don't quite understand? Surely I'm just rewriting to the 16 elements in the array?

No, you're writing to seventeen elements of the sixteen element array.

for(int i=0; i < 17 ;i++){
line = 0;

  • }*
    'i' has to be less than 17, so it can only go to 16? I thought less than or equals to was '<='

I tried changing it to 16 and that ruined the "Serial.println(fileName);" and reset the program

So, your sketch still isn't correct.
Post it.

i' has to be less than 17, so it can only go to 16?

A sixteen element arrays has indices 0 . .. 15 inclusive.

AWOL:
So, your sketch still isn't correct.
Post it.

i' has to be less than 17, so it can only go to 16?

A sixteen element arrays has indices 0 . .. 15 inclusive.

Ok I've just changed the 17's to 16's in keyboardRead()

Code -

#include <PS2Keyboard.h>
#include <SPI.h>
#include <SD.h>


PS2Keyboard keyboard;
int option;
char input[16];
File myFile;


char keyboardRead(){
  
  // Gets input from keyboard / scanner and stores it int the input array
  
  char line[16];
  int count = 0;
  
  for(int i=0;i<16;i++){
      input[i] = 0;
  }
  
    for(int i=0;i<16;i++){
      line[i] = 0;
  }

  while(1){
     if(keyboard.available()){
        char kb = keyboard.read();
      
        if(kb != PS2_ENTER){
            line[count] = kb;
            count++;  }
        else{
          line[count++] = '\0';
            for(int i=0;i<17;i++){
              input[i] = line[i];
             
            }
              break;
              }  
            }
          }
}

void transaction(){
  
  
  int price;
  float pricePounds;
  int total = 0;
  String description;

  
  Serial.println("TRANSACTION MODE");

  /*Scan Products
		Total Price
		Deduct price from card
		Finish */

 keyboardRead();
 
 //THIS IS THE ISSUE AREA
 
  char fileName[] = {input[5],input[6],input[7],input[8],input[9],input[10],input[11],input[12],'.','T','X','T', '\0'};
  
  // THE BARCODE IS TOO LONG TO USE THE WHOLE BARCODE AS THE FILE NAME, SO I'M CUTTING OUT THE FIRST SECTION TO CONFORM TO 8.3 
  Serial.println(fileName); // PRINTS OUT THE FILE NAME PERFECTLY
  
 myFile = SD.open(fileName); // DOESNT WORK
 
  if(!myFile){
    Serial.println("Product not Found");
   transaction();
    }
  price = myFile.parseInt();
  pricePounds = (float)(price)/(100);
  description = myFile.readStringUntil('.');
  Serial.print(description); Serial.print(" - "); Serial.write(0xA3); Serial.println(pricePounds);
  total = total + price;
  while(1); 
  


}


void setup(){
  Serial.begin(9600);
  delay(2000);
  keyboard.begin(8,2);
  pinMode(4, OUTPUT);
  
    if (!SD.begin(4)){
    Serial.println("Product Database not Found");
    while(1);}

}

void loop(){
  Serial.println("Welcome......");
  Serial.println("Please select one of the following options");
  Serial.println("1) Transaction Mode");
  Serial.println("2) Topup");
  Serial.println("3) Card Managment");
  Serial.println("4) Product Managment");
  
 keyboardRead();
 
//MENU OPTION, ONLY WRORKING ON TRANSACTION ATM


  switch ((int)input[0]) {
    case 49:
      transaction();
      break;
    case 50:
      Serial.println("Top");
      break;
    case 51:
      Serial.println("Card");
      break;
    case 52:
      Serial.println("Prod");
    default:
      Serial.println("ERROR"); 
  } 
  while(1);
}

Attached screenshot of Serial monitor

Thanks for your help :slight_smile:

Screen Shot 2014-08-17 at 20.10.16.png

Read the code again

AWOL:
Read the code again

Yup I missed one, and now it works :astonished: :astonished: :astonished: 4 hours work for one simple thing.

Thank you so much, I feel like such an idiot. Keep helping people, you're great :slight_smile:

Tom

Glad you got there yourself.
Array indices are tricky to start with.
Consistency and the use of named constants is the key.

Can you please send me the updated code?

Thanks