Information regarding interfacing USB Host Shield with Arduino

hi sir am working project on usb hub...

here two devices are present 1. usb pendrive 2. usb keyboard

but without pendrive keyboard not working how to solve this problem..

my code:

/---------------------------------------------------------------------------------------------------/
#include <usbhub.h>
#include <string.h>
#include "pgmstrings.h"
#include <SPI.h>
#include <UsbFat.h>
#include <masstorage.h>
#include <hidboot.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
/---------------------------------------------------------------------------------------------------/
String receivedChars;
/---------------------------------------------------------------------------------------------------/
USB Usb;
USBHub Hub(&Usb);
/---------------------------------------------------------------------------------------------------/
BulkOnly bulk(&Usb);
UsbFat key1(&bulk);
FatFile file;
void(* resetFunc) (void) = 0;
/---------------------------------------------------------------------------------------------------/
void fDrive()
{
if (!key1.begin()) {}

while(Serial.available()>0)
{
receivedChars = Serial.readString();
Serial.println(receivedChars);
if(receivedChars == "cowfat")
{
cowfatFunc();
}
else
{
Serial.println("wrongfilename");
delay(200);
resetFunc();
}
}
}
/---------------------------------------------------------------------------------------------------/
void cowfatFunc()
{
if (!file.open("cowfat.csv", O_RDONLY)) { Serial.println(F("open failed"));}
else{ Serial.println("Filefound");}
delay(200);
resetFunc();
}
/---------------------------------------------------------------------------------------------------/
class KbdRptParser : public KeyboardReportParser
{
virtual void OnKeyDown (uint8_t mod, uint8_t key);
};
/---------------------------------------------------------------------------------------------------/
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
if(key <= 82 && key >= 40) Serial.println(key);
}
/---------------------------------------------------------------------------------------------------/
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard(&Usb); KbdRptParser Prs;
/---------------------------------------------------------------------------------------------------/
void setup()
{
Serial.begin( 115200 );
Serial.println("------------------------");
Serial.println("Start");
if (!initUSB(&Usb))
{
Serial.println(F("initUSB failed"));
}
Keyboard.SetReportParser(0, (HIDReportParser*)&Prs);
}
/---------------------------------------------------------------------------------------------------/
void loop()
{
fDrive();
Usb.Task();
}
/--------------------------------------------------------------------------/

sample output:


Start
69
69
69
69
cowfat
Filefound

Start
68
67
67
67
67
67
cow
wrongfilename

Start

You enter a filename on the keyboard, and look up a file on the pen drive. That's why both items must be connected for successful operation.

hi sir but some times i work with keyboard only.. at that instants keyboard not worked without pen-

drive..so kindly help me sir....

Specify "it works"?

hai sir i send two files

in file 1 without this '4' lines keyboard is easily working without connecting pen-drive with hub

BulkOnly bulk(&Usb);
UsbFat key1(&bulk);
FatFile file;
void(* resetFunc) (void) = 0;

when am put this '4' lines in the code that is present in file-2 then keyboard is not working without

connecting pen-drive with hub

keyboard is easily working without connecting pen-drive with hub

file2.ino (1.88 KB)

pgmstrings.h (2.79 KB)

pgmstrings.h (2.79 KB)

file1.ino (1.89 KB)

Then you know the solution: omit those 4 lines that require the pen drive.

hai sir but some times i put pen-drive at that instant pen drive works and remaining times keyboard works without pen-drive... so kindly help me in coding part sir.....

As long as you don't know or can't tell what you want to make, I cannot help you.

hi sir am working a project on usb hub...

  1. when am connecting keyboard to arduino through usb-hub then it works for file-1

  2. when am connecting pen-drive to arduino through usb-hub then it works for file-2

  3. when am connecting both pen-drive and keyboard to arduino through usb-hub then both are works for file-3

  4. when am removing pen-drive and put keyboard to arduino through usb-hub then keyboard not works for file-3

  5. when am removing keyboard and put pen-drive to arduino through usb-hub then pen-drive works for file-3

file1.ino (1.89 KB)

file2.ino (2.32 KB)

file3.ino (3.66 KB)

pgmstrings.h (2.79 KB)

hi sir am working a project on usb hub...

  1. when am connecting keyboard to arduino through usb-hub then it works for file-1

  2. when am connecting pen-drive to arduino through usb-hub then it works for file-2

  3. when am connecting both pen-drive and keyboard to arduino through usb-hub then both are works for file-3

  4. when am removing pen-drive and put keyboard to arduino through usb-hub then keyboard not works for file-3

  5. when am removing keyboard and put pen-drive to arduino through usb-hub then pen-drive works for file-3

You only describe what does and does not work with your current code. I have no real idea of what you want to improve.

If the pen drive is not present, don't try to open files on it.

i don't try the pendrive files... even thought keyboard is not working properly