Notifications
Clear all

Reading SD card data

8 Posts
4 Users
0 Likes
1,827 Views
(@winddance)
Member
Joined: 4 years ago
Posts: 3
Topic starter  
My question is to do with the SD card tutorial. 
In particular, the servo recorder and playback.
In my project, I’m using a rotary encoder. I’m measuring the angle that it is moved to. That works fine.
But I’m also recording the time between movements.
So I end up with two numbers separated by “,” on the SD card.
I don’t know if Parsing is the right word but how do I separate the two numbers so that one tells the servo what angle to move to and the second number, the time interval to wait before it moves.
 
My thought was to just have a delay with a variable that is updated by the delay recorded on the SD card. I just don’t know how to get the timing off the SD card.
Hope I’ve explained this well.
Cheers
 

   
Quote
triform
(@triform)
Member
Joined: 5 years ago
Posts: 324
 

@winddance

This is a function I wrote a very long time ago to do just that. 

You would call it like:

   // char char_var_to_store_result_in should be an array large enough for the value.

    getstrfld (your_line_to_parse_from, 0, 0, ",", char_var_to_store_result_in);

    // Do something with the data...

    getstrfld (your_line_to_parse_from, 1, 0, ",", char_var_to_store_result_in);

    // Do something with the data...

 You will have to do an atoi on the char_var_to_store_result_in to get the real number value.

 The field number is zero-based, so the first field would be 0 the next 1 and so on.

 

 

char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr)
{
   char *offset, *strptr;
   int curfld;

   if (!*strbuf) {
      *retstr = 0;
      return (retstr);
   }

   offset = strptr = (char *) NULL;
   curfld = 0;

   strbuf += ofset;

   while (*strbuf) {
      strptr = !offset ? strbuf : offset;
      offset = strpbrk ((!offset ? strbuf : offset), sep);

      if (offset)
         offset++;
      else if (curfld != fldno) {
         *retstr = (char) 0;
         break;
      }

      if (curfld == fldno) {
         strncpy (retstr, strptr,
         (int)(!offset ? strlen (strptr) + 1 : (int) (offset - strptr)));
         if (offset)
            retstr[offset - strptr - 1] = 0;

         break;
      }
      curfld++;
   }   
   return retstr;
}

   
ReplyQuote
(@winddance)
Member
Joined: 4 years ago
Posts: 3
Topic starter  

Hi Triform,

Thanks for the reply. However my programing level is low enough that the code you posted looks like a foreign language to me.

Right from the first line I have no idea What all these things mean?? (char *strbuf, int fldno, int ofset, char *sep, char *retstr) { char *offset, *strptr; int curfld; if (!*strbuf) { *retstr = 0; return (retstr); }

Is there a Dumbed down version.

Thanks 

Cheers

L

 

 


   
ReplyQuote
triform
(@triform)
Member
Joined: 5 years ago
Posts: 324
 

   
ReplyQuote
(@starnovice)
Member
Joined: 5 years ago
Posts: 110
 

@triform

This is the way your code came out in my email.  I thought before I read you actual msg that you were making a joke.  My response was going to be " Ok that is just cruel" 🙂

After all this is how c programmers would write their code in some competitions.

char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr); void setup() { char val_buffer[10]; int val_one = 0, val_two = 0; Serial.begin (9600); getstrfld ("123,456", 0, 0, ",", val_buffer); val_one = atoi (val_buffer); Serial.println (val_one); getstrfld ("123,456", 1, 0, ",", val_buffer); val_two = atoi (val_buffer); Serial.println (val_two); } void loop() { // put your main code here, to run repeatedly: } char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr) { char *offset, *strptr; int curfld; if (!*strbuf) { *retstr = 0; return (retstr); } offset = strptr = (char *) NULL; curfld = 0; strbuf += ofset; while (*strbuf) { strptr = !offset ? strbuf : offset; offset = strpbrk ((!offset ? strbuf : offset), sep); if (offset) offset++; else if (curfld != fldno) { *retstr = (char) 0; break; } if (curfld == fldno) { strncpy (retstr, strptr, (int)(!offset ? strlen (strptr) + 1 : (int) (offset - strptr))); if (offset) retstr[offset - strptr - 1] = 0; break; } curfld++; } return retstr; }

Pat Wicker (Portland, OR, USA)


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@winddance

Here is an example that uses C++, the language of choice, that the Arduino designers and implementer's wanted to, and intended for you to use:

void setup() {
  Serial.begin(9600);

  String line  = "  Left  ,  Right   ";
  int delimPos = line.indexOf(',');
  
  // Start from position 0, and read up to, not including "delimPos" characters
  String left = line.substring(0, delimPos);
  
  // Start from next character following "delimPos", and read remaining characters
  String right = line.substring(delimPos + 1);

  // Remove leading and trailing spaces
  left.trim();
  right.trim();
  
  Serial.print(left);
  Serial.print(right); 
 }

Although Arduino supports code written in both "Assembly" and "C" as shown, I suggest you stick to the "C++" language unless you have a need to do otherwise!

By the way, you can easily incorporate the above example I provided into a loop too, if you need to read and work with multiple lines from a an SD file in real time.

Hope this help's!


   
ReplyQuote
(@winddance)
Member
Joined: 4 years ago
Posts: 3
Topic starter  

Hi Frogandtoad,

Great handle by the way.

Thanks for the code. It gives me a good starting point. I'll play around with it and let you know how it goes.

Cheers

L


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@winddance

No problem!

Here is a good reference to bookmark, for all Arduino programmers who wish to understand most parts of the language:

Arduino Language Reference

Cheers!


   
ReplyQuote