Greeting everyone!
First of all I'm french so sorry if I do some mistakes
There is many days I'm on a problem who seems to be basic but make my crazy ^^'
I got a barcode scanner(who act as a usb keyboard) connected to an USB sheild managed with th USB Host Sheild 2.0. It send data to an Arduino nano with a SD card plugged on.
My program act like that:
-The barcode scanner read a 6 character lenth barcode(only numbers ex: "123456")and send it ton the arduino one by one
-The arduino make a char array with the numbers and print it on the serial
-Then the arduino should creat a file named with this char array but it's actally not work...
There is my code :
(I know the code is strange but this is how the library work)
the active code part is void KbdRptParser::OnKeyPressed(uint8_t key) line 35
#include <hidboot.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <SPI.h>
#include <SD.h>
File membre;
uint8_t curseur = 0;
char code[]="000000.txt";
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
class KbdRptParser : public KeyboardReportParser
{
void PrintKey(uint8_t mod, uint8_t key); // Add this line to print character in ASCII
protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
uint8_t c = OemToAscii(mod, key);
if (c)
OnKeyPressed(c);
}
/* when a character is coming: */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
if(curseur <= 5){
code[curseur]=(char)key;//here,(char)key is the recieved character
curseur++;
}
else if(curseur == 6){
curseur=0;
Serial.println(code);
membre = SD.open(code, FILE_WRITE);
membre.close();
if(SD.exists(code)) {
Serial.println(F("file created"));
}
else {
Serial.println(F("file can't be creat"));
}
}
};
KbdRptParser Prs;
void setup()
{
Serial.begin( 115200 );
pinMode(9, OUTPUT);
Serial.println(F("Start"));
SD.begin(9);
if (Usb.Init() == -1) {
Serial.println(F("OSC did not start."));
}
delay( 200 );
Hid.SetReportParser(0, (HIDReportParser*)&Prs);
delay( 200 );
}
void loop()
{
Usb.Task();
}
The think wich make me crazy is the fact that when I display the char array on the serial, everithing is OK, it's print like a normal ASCII string but it's not able to crate a file named with...
example of the serial output after some scans :
Start
OK
000000.txt
file can't be creat
123456.txt
file can't be creat
011896.txt
file can't be creat
011679.txt
file can't be creat
so I tried with the exemple "Files" of the library SD
#include <SPI.h>
#include <SD.h>
File membre;
char code[]="000000.txt";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(9)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (SD.exists(code)) {
Serial.println("000000.txt exists.");
} else {
Serial.println("000000.txt doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating 000000.txt...");
membre = SD.open(code, FILE_WRITE);
membre.close();
// Check to see if the file exists:
if (SD.exists(code)) {
Serial.println("000000.txt exists.");
} else {
Serial.println("000000.txt doesn't exist.");
}
}
void loop() {
// nothing happens after setup finishes.
}
and everything worked perfectly, it was able to create a file with the char:
Initializing SD card...initialization done.
000000.txt doesn't exist.
Creating 000000.txt...
000000.txt exists.
Once I plugged the SD card in my PC I can clearly read the file, I'm lost, I don't know why it's not working, maybe the returned character of the sheild are different ASCII ?? I don't think so but at this point everything is possible^^'
Even if I put a pre-created file with the "Files" program with the same name of the code I want to scan ex: "123456.txt" will not be recognize by my program...
I aslo tried by closing the char array with
code[11]= '\0';
but nothing
I hope I was understandable and hope you could help me !
Thanks