SD Card

The USBDroid provides USB Host functionality for use as an Android peripheral as an Open Android Development Kit board. [Product page]
Post Reply
marks
Posts: 31
Joined: Wed Jan 04, 2012 8:13 pm

SD Card

Post by marks » Sat Jan 07, 2012 11:34 pm

Hello All,

Another beginners question. How do I get the SD card working when the USB is enabled? The SD card works fine if I have not enabled USB but if I have the board "hangs" or restarts depending on what Pin has been set (I believe it should be 4).

So this works:

#include <SD.h>

void setup() {
pinMode(4, OUTPUT);
Serial.begin( 115200 );
if (!SD.begin(4)) {
Serial.println("SD Card Initialization failed!");
while(1);
}
else {
Serial.println("SD Card Mounted");
}
Serial.println("SD Open");
measureFile = SD.open("test.csv", FILE_WRITE);
Serial.println("SD Open Past");
if ( measureFile ) {
blah, blah
}
}

This doesn't work with hangup/restart at SD.open. Have tried any number of pins.

#include <SD.h>

void setup() {
pinMode(4, OUTPUT);
Serial.begin( 115200 );
if (!SD.begin(4)) {
Serial.println("SD Card Initialization failed!");
while(1);
}
else {
Serial.println("SD Card Mounted");
}
startUsb();
Max.Task();
Usb.Task();
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ){
rcode = Usb.getDevDescr( ADDR, 0x00, 0x32 , buf );
rcode = Usb.setConf( ADDR, 0x00, 0x01 );
rcode = Usb.outTransfer(ADDR, 0x01, 0x03, GET_SERIAL_NO);
rcode = Usb.inTransfer(ADDR, 0x01, 0x0C, retbuf );
}
Serial.println("SD Open");
measureFile = SD.open("test.csv", FILE_WRITE); //Hangs or Restarts Here
Serial.println("SD Open Past");
if ( measureFile ) {
blah, blah
}
}

Thank you in advance,

Mark.

marks
Posts: 31
Joined: Wed Jan 04, 2012 8:13 pm

Re: SD Card

Post by marks » Mon Jan 09, 2012 10:26 pm

Hi There,

Something to do with the "newbie" thing again. Changing this:

Serial.println("SD Open");
measureFile = SD.open("test.csv", FILE_WRITE); //Hangs or Restarts Here
Serial.println("SD Open Past");

To This:

Serial.println("SD Open");
appendFile("test.csv", currentMeasure); //Works Fine
Serial.println("SD Open Past");

void appendFile(char* workFile, char* appendData) {
SDFile = SD.open("workFile", FILE_WRITE);
blah, blah
}
}

Works fine?

I don't know, perhaps something the compiler is doing . Many years ago a Borland compiler I had did similar things and re-organising code slightly fixed it.

Cheers,

Mark S.

andrewduncan
Posts: 24
Joined: Tue Dec 06, 2011 1:42 am
Location: Melbourne, AU

Re: SD Card

Post by andrewduncan » Wed Jan 11, 2012 9:32 pm

Hi Mark,

Often if reorganizing code fixes a problem, the original problem is due to memory corruption (e.g. writing off the end of an array). Your solution probably hasn't fixed the underlying problem and you may find other issues down the track. However, I am not an experienced arduino developer (all my experience is writing code for Linux/UNIX/Windows) so there may be other issues that I am not aware of.

Andrew

marks
Posts: 31
Joined: Wed Jan 04, 2012 8:13 pm

Re: SD Card

Post by marks » Wed Jan 11, 2012 11:04 pm

Thanks Andrew,

Yes you are correct I have been very careful, but found an array that I was writing past the end of which has been fixed.

Unfortunately I still have issues, basically in a loop which reads multiple data records from the USB Host, writing each record to the SD card and updating the the freetronics LCD shield (there is some user input required for each set of records).

I can get either the USB and LCD to work OR the USB and SD card but not all three. It seems to me that the pin D4 is the problem as it's the slave select SS for the SD card and is also used by the LCD shield.

Is this even possible? Do I need to move pin D4 on the LCD shield to some other pin, I think so but am not sure.

It's all a bit of "head doer in" for a newbie to Arduino as I still need to put a small thermal printer into the mix (this works with my testing but I can see it all running out of pins fairly shortly as the 'wish list" has a few other serial devices).

Cheers,

Mark S.

andrewduncan
Posts: 24
Joined: Tue Dec 06, 2011 1:42 am
Location: Melbourne, AU

Re: SD Card

Post by andrewduncan » Thu Jan 12, 2012 12:49 am

Hi Mark,

I hadn't realize that pin D4 was used for both the microSD card slot and the LCD shield. I was intending to use the microSD slot for in field debugging for my project, but it isn't a show stopper for me. The problem is that I am not sure that there are any digital pins left to use. There doesn't appear to be any convenient cut-jumpers to deal with this clash. One solution would be to use a serial LCD Display instead of the 16x2 LCD Shield. I don't think that the serial displays have their own buttons, so you would need to make you own.

Andrew

marks
Posts: 31
Joined: Wed Jan 04, 2012 8:13 pm

Re: SD Card

Post by marks » Thu Jan 12, 2012 1:04 am

For anybody else trying to do this i.e.

Use the USB, LCD Shield and SD card at the same time, it seems that something must be done with pin D4.

I have previously done the mod as described on the LCD shield page which is pin 6 on the LCD to pin Arduino A1 but also Pin 11 on the the LCD to pin Arduino A2.

To do this I removed the D4 leg on the LCD shield connector and ran a wire from D4 to A2 on the LCD shield.

Hurray USB, LCD and SD all at the same time :) It may not be the "correct" thing to do but it works for me. Now onto adding some more devices to the Arduino, love it what a "tinkerers paradise"

Regards,

Mark S.

ninja2
Posts: 38
Joined: Sun Dec 25, 2011 3:25 am

Re: SD Card

Post by ninja2 » Fri Feb 03, 2012 9:14 pm

This is exactly what I want to do USBdroid+SD+LCD so thanks you've answered my question about the conflict on D4. Guess I'll have to start butchering the board ... :|

So http://www.freetronics.com/pages/combin ... e-usbdroid explains how resolve D9 conflict, but it states "what alternative digital pin can you use? All the dedicated digital pins other than the serial comms pins (D0 and D1) are used by either the LCD & Keypad Shield or the USBDroid. You could use D0 or D1, but that would interfere with USB communications. The other option is to use one of the unused analog input pins A1 to A5".

But as far as I can tell from the Eagle files pins D2 and D3 are available as well and not used by SD or LCD OOTB. Do you agree?

I'm sure Freetronics will improve on there already great design in later versions, to support this obvious combination, e.g. by providing solder jumper pads for D9 and D4

afridizaddi
Posts: 1
Joined: Fri Mar 30, 2012 7:17 am

Re: SD Card

Post by afridizaddi » Fri Mar 30, 2012 7:24 am

Thanks for the post

Post Reply