Need Some Serious Help (SD CARD)
Posted: Wed Dec 05, 2012 9:05 am
Hey Guys,
So I am a college student taking an introductory to robotics class. The beginning of the year we all had to purchase Netburner 5270. The end of the year has now come and we have a final project due and I am having some issues with my code (If this is not the right forum to post in my direct me towards the right one)
My code should be pretty simple (again this is my first time coding). All I want it to do is check the status of the pushbutton wired to J2{43} if it is pushed check the status of dipswitch 1. If it is on turn Led1 on, open the servo arm (OPEN DOOR), and create a text file on my sd write to the file and close the file.
My only problem is with the sd card. Randomly, say every 10th time I hit the pushbutton I will get an error closing my text file. This is my code with should create, write, and close the SD.
void f_open_PrintError(char* pFileName);
void f_close_PrintError(char* pFileName);
void Enter_Door(char * FileName) {
iprintf("\r\nCreating file: %s\r\n", FileName);
int i = 0;
F_FILE* fp;
do {
fp = f_open( FileName, "a+" ); // Creating and Opening File
i++;
if (fp == 0) { /// Does NOT open
OSTimeDly(TICKS_PER_SECOND / 2);
}
} while (fp == 0 && i < 5);
if (fp == 0) {
iprintf("\r\n Error in Opening:%s\r\n", FileName);
} else {
const unsigned int write_buffer_size = 100;
char write_buf[write_buffer_size];
sniprintf(write_buf, write_buffer_size, "Person Entered %d\r\n", Secs);
int n = f_write( write_buf, 1, strlen( write_buf ), fp );
iprintf("Wrote %d bytes: %s", n, write_buf);
iprintf("Closing file %s\r\n\r\n", FileName);
int rv = f_close( fp ); // Close a previously opened file of type F_FILE
do {
i++;
if (rv != 0) { // Does NOT close
OSTimeDly(TICKS_PER_SECOND / 2);
}
} while (rv != 0 && i > 10);
if (rv != 0) {
f_close_PrintError(FileName);
DisplayEffsErrorCode(rv);
}
}
The parts in red are what I most recently added. I did those parts because I thought if for some reason the file didnt close the first time through it would close the second time or something to that effect.
There are 3 errors that will sporadically appear on MTTY.
***Error in f_close(Enter.txt) during task(Main)
F_ERR_NOTOPEN
***Error in f_close(Enter.txt) during task(Main)
F_ERR_NOTFORMATED
***Error in f_close(Enter.txt) during task(Main)
F_ERR_WRITE
What really confuses me is I can push the pushbutton 15 times and it will write to the SD every time, but then the 16th time I will get an error, but then the 17th I wont have an error. Sometimes I will get two errors in a row too but then it will work again. I really have no clue how to fix this.
I am sorry that this is such a long post. If anyone has any ideas I would really appricaite it I am very stressed out about this problem. I didnt include my entire code this is just the function which is supposed to do all of this, but I can send the rest of my code if anyone thinks it would help them.
Also as I said I am completely new to coding so if there is another forum anyone would suggest to help me with my issue I would love to hear about it.
(This is being written on NBeclipse)
Thanks
Ben
So I am a college student taking an introductory to robotics class. The beginning of the year we all had to purchase Netburner 5270. The end of the year has now come and we have a final project due and I am having some issues with my code (If this is not the right forum to post in my direct me towards the right one)
My code should be pretty simple (again this is my first time coding). All I want it to do is check the status of the pushbutton wired to J2{43} if it is pushed check the status of dipswitch 1. If it is on turn Led1 on, open the servo arm (OPEN DOOR), and create a text file on my sd write to the file and close the file.
My only problem is with the sd card. Randomly, say every 10th time I hit the pushbutton I will get an error closing my text file. This is my code with should create, write, and close the SD.
void f_open_PrintError(char* pFileName);
void f_close_PrintError(char* pFileName);
void Enter_Door(char * FileName) {
iprintf("\r\nCreating file: %s\r\n", FileName);
int i = 0;
F_FILE* fp;
do {
fp = f_open( FileName, "a+" ); // Creating and Opening File
i++;
if (fp == 0) { /// Does NOT open
OSTimeDly(TICKS_PER_SECOND / 2);
}
} while (fp == 0 && i < 5);
if (fp == 0) {
iprintf("\r\n Error in Opening:%s\r\n", FileName);
} else {
const unsigned int write_buffer_size = 100;
char write_buf[write_buffer_size];
sniprintf(write_buf, write_buffer_size, "Person Entered %d\r\n", Secs);
int n = f_write( write_buf, 1, strlen( write_buf ), fp );
iprintf("Wrote %d bytes: %s", n, write_buf);
iprintf("Closing file %s\r\n\r\n", FileName);
int rv = f_close( fp ); // Close a previously opened file of type F_FILE
do {
i++;
if (rv != 0) { // Does NOT close
OSTimeDly(TICKS_PER_SECOND / 2);
}
} while (rv != 0 && i > 10);
if (rv != 0) {
f_close_PrintError(FileName);
DisplayEffsErrorCode(rv);
}
}
The parts in red are what I most recently added. I did those parts because I thought if for some reason the file didnt close the first time through it would close the second time or something to that effect.
There are 3 errors that will sporadically appear on MTTY.
***Error in f_close(Enter.txt) during task(Main)
F_ERR_NOTOPEN
***Error in f_close(Enter.txt) during task(Main)
F_ERR_NOTFORMATED
***Error in f_close(Enter.txt) during task(Main)
F_ERR_WRITE
What really confuses me is I can push the pushbutton 15 times and it will write to the SD every time, but then the 16th time I will get an error, but then the 17th I wont have an error. Sometimes I will get two errors in a row too but then it will work again. I really have no clue how to fix this.
I am sorry that this is such a long post. If anyone has any ideas I would really appricaite it I am very stressed out about this problem. I didnt include my entire code this is just the function which is supposed to do all of this, but I can send the rest of my code if anyone thinks it would help them.
Also as I said I am completely new to coding so if there is another forum anyone would suggest to help me with my issue I would love to hear about it.
(This is being written on NBeclipse)
Thanks
Ben