Shop OBEX P1 Docs P2 Docs Learn Events
How to convert a number to binary and then seperate into digits in pbasic? — Parallax Forums

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

LittleBLittleB Posts: 2
edited 2009-08-28 22:47 in BASIC Stamp
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?

Comments

  • UnsoundcodeUnsoundcode Posts: 1,530
    edited 2009-08-28 21:15
    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
    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.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-08-28 21:42
    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
    ·
  • LittleBLittleB Posts: 2
    edited 2009-08-28 22:23
    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.
  • UnsoundcodeUnsoundcode Posts: 1,530
    edited 2009-08-28 22:44
    @ Chris ,

    ·http://www.savagecircuits.com/

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

    Jeff T.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-08-28 22:47
    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
    ·
Sign In or Register to comment.