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);
}
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);
}