Shop OBEX P1 Docs P2 Docs Learn Events
Writing to SD Card — Parallax Forums

Writing to SD Card

frankwillisfrankwillis Posts: 6
edited 2012-04-05 04:22 in Propeller 1
I have been using FSRW26 to try to write to an SD card, but I am unable for some reason to assign a variable and write it to the SD card. I know I must be missing something stupid. I am Gadget Gangster's tutorial to write a predetermined quotation to the card just like he does, but I cannot figure out how to write the value of a variable to the card.

As an engineer (non electronics related), with 30 years experience, I find it absolutely incredible that a simple method to apply an object to write data to an SD card is so illusive. Parallax has done a pretty good job with the basic stamp, but for the entry level people, too much time is being wasted on the Propeller. This should be simple and direct, and I am sure the final coding is, but why make it so darn complex to get a grasp of this? I know I am not alone.

Can anyone point me to a simple code that writes a variable to the SD card? I know how to use objects.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-04 19:20
    Edit: I realized after I wrote this post, that it's a bad example. It too complicated. I'm sure there are simplier ways to do this. I'll leave this post here in case it can be useful and because I don't like to delete posts.

    Fank,

    One thing you need to decide is how you want your variables stored.

    Do want them stored as computer native numbers or do you want them stored as ASCII characters.

    If you want to be able to read the file from Excel for some other text reader, you probably want to store the number in ASCII characters.

    In my PropBOE-Bot project, the servo pulse length are written to the SD card as ASCII characters.

    Here's the section of code to store the variables.
    if driveMode == _RcRecord
          LongToDecStr(wheelSpeed[_PortWheel], 4, @sdBuffer)
          Sd.writeString(@sdBuffer)
          Sd.writeByte(",")
          LongToDecStr(wheelSpeed[_StarboardWheel], 4, @sdBuffer)
          Sd.writeString(@sdBuffer)
          Sd.writeByte(13)
          Sd.writeByte(10)
    

    I convert the variables wheelSpeed[_PortWheel] and wheelSpeed[_StarboardWheel] to ASCII characters and store them in the buffer "sdBuffer" and then write the buffer as string to the card.

    The SD card objects may have a method that does this conversion for you. I wanted to minimize the number of writes to the card so I built up a buffer to write all at once (now I wonder why I didn't add the "13" and "10" end of line characters to the buffer first).

    Here's the method "LongToDecStr"
    PUB LongToDecStr(value, digits, localPtr) | temp
    '' Write an ASCII decimal number to localPtr
      temp := 1
      repeat digits
        temp *= 10
      value //= temp                ' tuncate value that wont fit in digits
      repeat digits
        temp /= 10
        byte[localPtr++] := (value / temp) + "0"
        value //= temp
     
    

    I'm using Kye's SD card object but the principles apply to FSRW26 as well.

    If you don't want the data stored as ASCII data then you'd use a different method to write the values.
  • frankwillisfrankwillis Posts: 6
    edited 2012-04-04 19:33
    Thanks, Duane.

    I am unable to track where some of your variables are coming from.

    To declare a variable so that it is a string, I can't use long?

    Then to write the variable to the sD takes all of this? I don't follow some of the code you have listed. It must be part of your larger program.

    For example, say I want to write the values of A and B to a card in ASCII format.

    What would the declaration be?
    What part of the could could run on its own to write to an SD card. I am using new Propeller BOE that just came out.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-04 19:34
    As I mentioned, I use Kye's "SD-MMC_FATEngine.spin" object for writing to a SD card.

    It doesn't have a method to automatically write a variable as ASCII characters so you'd need to use a method such as "LongToDecStr" to write a variable as ASCII characters.

    The SD object does have a writeLong method. So if your variable name is "variableX" and you wanted to write to a SD card you could use this code.
    Sd.writeLong(variableX)
    

    There are write methods for other variables sizes as well.

    The PropBOE-BOT code I mentioned earlier is posted in the (currently) last post of the thread I linked to in my earlier post.
  • frankwillisfrankwillis Posts: 6
    edited 2012-04-04 19:50
    Duane,
    Thanks. Where is Kyles object? I don't see it in the propeller object in obex.
  • AribaAriba Posts: 2,690
    edited 2012-04-04 20:10
    You can use the Simple_numbers object to generate strings from Variables, and then write them with the pputs methode to the SD card. Between the strings you can write the separation characters you need.
    OBJ
      sd  : "FSRW"
      num : Simple_Numbers"
    
    VAR
      long  x,y
    PUB
      'sd mount
      'sd open
      ...
      sd.pputs(num.dec(x))
      sd.pputc(",")
      sd.pputs(num.dec(y))
      sd.pputc(13)
      ...
      sd.pclose
    

    Andy
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-04 20:13
    Duane,
    Thanks. Where is Kyles object? I don't see it in the propeller object in obex.

    It's way to hard to find things in the OBEX IMO.

    But I did find it.
    http://obex.parallax.com/objects/619/

    Kye's object is very sensitive to card errors. I've had to reformat cards before they would work. "Quick" formats don't fix the problems (if there are any) either.
  • frankwillisfrankwillis Posts: 6
    edited 2012-04-05 04:22
    Thanks, Andy and Duane!

    Andy, it seems that your suggestion best fits where I am now. I was apparently having problems with the FSRW and the string issue thing. I will try it today. I bet it resolves my issue.

    THANKS!
Sign In or Register to comment.