# Society of Robots - Robot Forum

## Software => Software => Topic started by: 555 timer chip guy on March 31, 2007, 09:32:15 AM

Title: expressing a decimal in PBASIC
Post by: 555 timer chip guy on March 31, 2007, 09:32:15 AM
I am trying to control a servo with a BS1 microcontroller but I can't figure out how to express the value 1.5 or any other decimals for that mater dose any one know how to express a decimal in PBASIC.
Title: Re: expressing a decimal in PBASIC
Post by: Admin on April 01, 2007, 03:40:20 PM
whats your code look like so far?

look into 'float', which is a variable type for storing decimal numbers. its been almost a decade since i programmed in PBASIC, but i dont remember ever having a problem with decimals when using them . . . in theory it should just work . . .
Title: Re: expressing a decimal in PBASIC
Post by: Somchaya on April 01, 2007, 04:47:40 PM
I've never worked with PBASIC, but one way you could consider would be to use fixed-point math.

For example, you can store numbers like 1.5 and 2.4 as 15 and 24 respectively. This way, you can do
regular math on these "integers", and to get the digits and decimal parts, you can divide by 10 and mod by 10 respectively. Of course, if you needed more precision, you could use larger numbers like 100 or 1000, but you get the idea.

Hope this helps!
Title: Re: expressing a decimal in PBASIC
Post by: nanob0t on April 01, 2007, 05:17:24 PM
A 'float' character stores decimals.  I don't know what it looks like in PBASIC though.

If anything, think of it a different way.  1.5  is also 3/2    Sometimes it can be a matter of modifying what you learned.  If you do division, make sure you store it in an appropriate integer.

Hope this helps
Title: Re: expressing a decimal in PBASIC
Post by: 555 timer chip guy on April 02, 2007, 08:29:33 AM
thanks very much for the help that  helped a lot . :D
Title: Re: expressing a decimal in PBASIC
Post by: 555 timer chip guy on April 02, 2007, 10:22:18 AM
so tell me if I have this right:

if I wont to pause for 1.5 ms I could use this command  PAUSE 2/3 ???
Title: Re: expressing a decimal in PBASIC
Post by: nanob0t on April 02, 2007, 08:01:50 PM
I have no clue.   Try looking for information on the web first.

If not, give the program a try like that and see if you get any errors, or if the program doesn't like to your wishes.

Maybe, if the programming language supports it, try () to encase the number.

Or perhaps, if you use this number once, and if the programming language supports it, try defining a variable at the beginning to be 3/2 or 1.5.  Then you can just say  PAUSE (Insert Variable Here)

A few ideas that may work.
Title: Re: expressing a decimal in PBASIC
Post by: Somchaya on April 02, 2007, 10:27:06 PM
Actually, from what I know, most languages will treat 3 / 2 as an integer division, and return an integer, so 3/2 = 1 in this case.

If you need to do a pause for fractions of a second, but the function like pause only accepts seconds, then it is unlikely that it'll work. Instead, there might be another function that will take in milliseconds or some smaller division.

Again, I'm not familiar with PBASIC, but I'm guessing through my experience with other languages, and playing with QBasic a long time ago (no idea if it's related to PBASIC though).

Another suggestion would be to try google for "pause fraction of second" or something like that.
Title: Re: expressing a decimal in PBASIC
Post by: JonHylands on April 03, 2007, 05:48:53 AM
so tell me if I have this right:

if I wont to pause for 1.5 ms I could use this command  PAUSE 2/3 ???

Since the units are milliseconds, there is no way to do anything other than whole milliseconds with PAUSE.

If you're trying to control an I/O pin for a specific length of time, you could use PULSOUT. Otherwise, you need to use a better microcontroller that has higher timing resolution.

- Jon
Title: Re: expressing a decimal in PBASIC
Post by: 555 timer chip guy on April 05, 2007, 08:13:57 AM
thank you for you  help
Title: Re: expressing a decimal in PBASIC
Post by: Admin on April 16, 2007, 11:48:40 AM
When I do my servos, I dont actually pause in milliseconds. I actually pause in units of computational cycles.

For example, if I know my microcontroller takes 1.5ms to run a while loop 300 times, then I just do a while loop 300 times for my servo  :P

300 is a non-decimal number, too 8)
Title: Re: expressing a decimal in PBASIC
Post by: Somchaya on April 16, 2007, 12:12:40 PM
For example, if I know my microcontroller takes 1.5ms to run a while loop 300 times, then I just do a while loop 300 times for my servo  :P

Hmm.. how do you know the microcontroller takes 1.5ms to run the loop 300 times? By calculating the number of clock cycles in the loop and comparing it to the processor speed?
Title: Re: expressing a decimal in PBASIC
Post by: Admin on April 16, 2007, 12:47:39 PM
the easiest method is just to use an oscope - send out a high-low pulse from a DO after the while loop is done, and measure the time between pulses.

i dont bother calculating or counting anything cause its too dependent on hardware (the timing crystal, voltages, etc.)
Title: Re: expressing a decimal in PBASIC
Post by: Somchaya on April 16, 2007, 12:58:51 PM
the easiest method is just to use an oscope - send out a high-low pulse from a DO after the while loop is done, and measure the time between pulses.

i dont bother calculating or counting anything cause its too dependent on hardware (the timing crystal, voltages, etc.)

That's really smart, thanks! ;D There's so much more about electronics and stuff about robotics that I need to learn :P
Title: Re: expressing a decimal in PBASIC
Post by: 555 timer chip guy on April 16, 2007, 01:51:59 PM
thanks  :D
Title: Re: expressing a decimal in PBASIC
Post by: nanob0t on April 17, 2007, 09:35:16 AM
I never thought of doing it that way.  I used to do BASIC and everything was in milliseconds and very limited.

Now I just need an oscilliscope  :(
Title: Re: expressing a decimal in PBASIC
Post by: aaqilkhan on May 24, 2007, 12:19:51 PM
I started off with a basic stamp II microcontroller and programmed it in PBASIC. From my experience, PBASIC doesnt like decimal numbers. All numbers must be in integer format. If u really want to work with decimal numbers, try PICmicros. If you still want to play with the stamp, then try the parallax forums. They provide excellent support for their products there.