+ Reply to Thread
Results 1 to 6 of 6

Thread: How to convert a number to binary and then seperate into digits in pbasic?

  1. #1

    Default How to convert a number to binary and then seperate into digits in pbasic?

    I want to use the potentiometer to provide input to a neural network, so I want to convert the rctime number to a binary number, and then separate it into 1 digit lengths.

    For example, say the potmeter gives the rctime value of 323. In binary it would be 0101000011. Then in a format understandable by the neural net, it would be split, literally, into 10 separate digits, i.e. 0, 1, 0, 1, 0, 0, 0, 0, 1, and 1. Each of these would be stored as a variable (or a constant?).
    Another example would be the number 700 (around the max of the potmeter). In binary it would be 1010111100. So again, split into 10 digits this would be: 1, 0, 1, 0, 1, 1, 1, 1, 0, and 0.

    So the steps pbasic would have to perform would be as follows:
    1. Take the number of the current rctime and convert it to binary.
    2. Take that binary number and split it into 10 separate digits.
    3. Store each of those digits as a variable.

    I've been trying to look through the documentation, trying to figure out how to do this. Can anybody help me?
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

  2. #2

    Default

    Hi , converting to binary is not a problem as the Stamp stores all numbers as binary anyway, decimal format is just a way of displaying the value so that it's easier for you and I to understand.
    You could create an array for each bit of the number and read the bits into the array something like the following
    Code:
    x VAR Bit(10)    ' ten bit array
    idx VAR Nib       ' variable use for the count loop
     
    value VAR Word  ' value that contains the ten bits of interest
     
    FOR idx =0 TO 9   ' ten iterations of the loop
     
    x(idx)=value.BIT0       ' store bit
    value=value>>1              ' shift value right one bit
     
    NEXT


    Jeff T.
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

  3. #3
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,329
    Blog Entries
    8

    Default

    If you're sending the data serially to a device this could be dine simply with the BIN formatter of the SEROUT command. It's not clear to me where the data is originating and where it is being stored though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    50 72 6F 6A 65 63 74 20 53 69 74 65
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

  4. #4

    Default

    I'm writing a neural net in pbasic to control the servo, and on the input side of the neural net, I need 10 1-bit values, which I want to get from a potentiometer, also hooked up to the stamp module.

    Thanks Unsoundcode, I'm not sure what the heck you explained to me, but I may be able to figure it out =)

    Apologies, I am still new at programming.
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

  5. #5

    Default

    @ Chris ,

    http://www.savagecircuits.com/

    is that new or have I been to blind to see it , neat link.

    Jeff T.
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

  6. #6
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,329
    Blog Entries
    8

    Default

    Jeff, not new...just hasn't been updated in forever. I've been too busy, but it seems recently there has been a need to get information up fast and so I am using the tool again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    50 72 6F 6A 65 63 74 20 53 69 74 65
    Last edited by ForumTools; 10-02-2010 at 11:05 PM. Reason: Forum Migration

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts