Shop OBEX P1 Docs P2 Docs Learn Events
Need help with fractions in Basic!!!! — Parallax Forums

Need help with fractions in Basic!!!!

NoviceNovice Posts: 4
edited 2007-08-07 02:52 in Learn with BlocklyProp
Hi to all,
I'm doing a project to relay tank levels wirelessly for school using the Basic Stamp 2. I have to do a calculation of multiplying a range of 0 To 5 Volts i.e 0.1, 0.2, 0.25, all the way up to 5 volts to a constant that is derived by Maximum Filling Level divided by the Maximum voltage (5 V). When multiplied together it give the level in the storage tank. However the Basic Stamp can only multiply integers and not fractions and this give back an offset reading not the accurate level reading. Is there a way to perform the calculation (eg. 1.25 X (43/5)) to display the correct level. Below is a section of the code that i am using to get the level reading. The adcbits is generated using a potentiometer to reflect the tank level when in operation. From the code below i get the correct volts being displayed but i cannot configure it to multiply by the constant (43/5). Can anyone assist me with this problem?? Thank You in advance.

Calc_Volts:
v= 5 * adcBits / 255
r= 5 * adcBits // 255
v2= 100 * r/255
DEBUG CR, "DVM Reading: " '.. new line
DEBUG DEC1 v, ".", DEC2 v2, " Volts",CR '.. new line
Calc_Level:
A= 43 'Maximum Filling Level
B= 5 'Maximum Voltage Required
C= 43/5=8.6=(860/100)
X= (860/100) * adcBits / 255
y1= (860/100) * adcBits // 255
y2= (100) * r/255
DEBUG " Level:", DEC x*5, ".", DEC2 y2*5,CR


PAUSE 5000 ' Wait a 1.0 Seconds

GOTO Main
RETURN

Post Edited (Novice) : 8/6/2007 4:54:36 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-06 04:20
    There are some good discussions of the use of fixed-point arithmetic here: www.emesystems.com.

    Generally, you use integers to hold fixed point numbers (with precision of your choosing).

    In your case, you could convert your values to hundreths of a volt, so 1.25 becomes the integer 125. To multiply by 43/5, you just multiply by 43, then divide by 5. In other words, 125 * 43 / 5 = 5375 / 5 = 1075. To display this with a decimal point, use DEBUG DEC x/100,".",DEC2 x//100,CR

    Note that you have to be careful to arrange your multiplications and divisions to maximum the remaining precision given that the largest integer is 65535. You also have to align the decimal points for addition and subtraction and adjust by appropriate factors of 10 for multiplications and divisions by non-integer values.

    By the way, don't use forum subjects like "Urgent help needed!". It's a waste of time. Always give some useful information about your problem in the subject line. You should change yours to something like "Need help with fractions in Basic".

    Post Edited (Mike Green) : 8/6/2007 4:25:36 AM GMT
  • NoviceNovice Posts: 4
    edited 2007-08-06 05:03
    Thanks Mike.
    For a value of 1.25 volts The value obtained is from V and V2 above. How can i get them in one form??
    The adcbits is the varying binary input signal to indicate the level, can i use this to get the result:

    A= adcbits
    B= 43/5
    C=adcbits * B
    How do i debug to get the output form??

    Another question, How do one increase the signal and also distance of a Parallax RF Transmitter??? Thanks to all.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-06 05:13
    Your voltage in hundreths of a volt = v * 100 + v2

    To use the correction factor you get: z = (43 * (v * 100 + v2)) / 5

    You display the result with: DEBUG DEC z / 100,".",DEC2 z // 100,CR

    Because you're using integer division, putting just 43 / 5 gives you 8 ... that's all
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-06 05:22
    The only way to increase the signal and distance of a Parallax RF Transmitter would be to use a better antenna. The ARRL (www.arrl.org) publishes some antenna guides that describe different kinds of antennas and how they're made (and sized for the frequencies involved). You would also gain some distance by using a better antenna on the receiver as well.

    These transmitters are deliberately limited in the amount of power they can use to transmit and there's no good way to increase that. Other options might be to buy a different transmitter and receiver, either designed for more power or for more range, probably using different frequencies. More range generally involves more expense, frequently greater physical size, and greater power requirements. Sometimes, switching to a different frequency band will improve things.
  • NoviceNovice Posts: 4
    edited 2007-08-06 23:36
    Hi Mike,
    I need to multiply the voltage generated by 43/5 = 8.6. The value i am getting is wrong since it gives a result of 8 feet until the voltage goes to 2 volts. Then 16ft until 3 volts etc if u get my point. I need the fraction to be included in the calculation. Any suggestions??
  • FranklinFranklin Posts: 4,747
    edited 2007-08-07 00:21
    Have you tried (value * 43)/5 ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-07 02:52
    Novice,
    I've given you suggestions already for multiplying by a scale factor. Have you tried them yet? Are there parts that you don't understand?
Sign In or Register to comment.