Converting mV into "xx.x volts" serout

cloudy

Member
Does anyone have any good ideas for converting a word value storing millivolts, into to a xx.x decimal serial output?

I know the PICAXE doesn't support FP - but hoping I could send (mV/100)&"."& (mV-(mV/100)*100 ' Get remainder)

ie: 1240mV - 12.4V

1240/100 = 12 (x)

12*100 = 1200

1240-1200 = 40 (y)

x&.&y = 12.40

This fails for 1260 however due to the rounding...

Any ideas?
 

Haku

Senior Member
BINTOASCII wordvariable, tenthousands, thousands, hundreds, tens, units

In your case it'll be something like:

BINTOASCII w0,b2,b3,b4,b5,b6
SERTXD(b3,b4,".",b5,b6,"V")
 
Last edited:

Jeremy Harris

Senior Member
As above, but it might be worth tidying up the leading zero, just to make things neater.

Just add a check to see if the first ASCII character is "0" and if it is replace it with " " (a space character). This then keeps the display looking neater as when you have, say, and output that would give "09.9V" on display it will be corrected to " 9.9V", with the leading zero replaced.

In applications where you can have several leading zeros you can cascade leading zero checks easily, just start with the left-most zero and work through replacing zeroes with spaces until you reach the first significant digit. I have a couple of data loggers where I store data as ASCII text files (.csv files so they can be easily read into a spreadsheet) and have found that adding the leading zero removal for all but a zero immediately preceding a decimal point character makes it a lot easier to process the data later, as all the fields are the same length.
 

AllyCat

Senior Member
Hi,

hoping I could send (mV/100)&"."& (mV-(mV/100)*100 ' Get remainder)
It would be better if you (try to) write "real" PICaxe code and not Pseudocode. What you have overlooked is the "//" operator which gives the remainder of a division directly. Thus for one decimal place (as in the title) you need only:

Code:
w1 = tensofmillivolts / 10
b1 = w1 // 10      ; remainder (=tenths of a volt)
w1 = w1 / 10       ; integer volts
sertxd(#w1,".",#b1," Volts")
However, you do need to be careful if using two (or more) places of decimals (e.g. if my first division by 10 above were omitted, and divisors by 100 used instead) because ".02" will print as ".2" unless you use the BINTOASCII method (or add code to insert the suppressed leading zero).

Therefore, at least for a "quick and dirty" output up to 10 volts, I also tend to just print out a "mV" value, although it may give a false impression of the real accuracy of the result. ;)

Cheers, Alan.
 

hippy

Ex-Staff (retired)
This fails for 1260 however due to the rounding...
I'm not sure why or how. PICAXE maths doesn't do any rounding itself.

1260mV => 12.6V

1260/100 = 12 (x)

12*100 = 1200

1260 - 1200 = 60 (y)

x&.&y = 12.60

If you were testing your maths in some other programming language where 1260/100 becomes 13, you probably need to look for a non-rounding integer or 'floor division' operator for that language.
 

grim_reaper

Senior Member
Finding that kind of error usually works better on a breadboard... while you're scribbling maths down on a bit of paper, wondering if you've got 12.4 volts or 12.6 volts pumping through your PICAXE, the kind little device lets you know with some smoke signals...
 

hippy

Ex-Staff (retired)
It's a good example of how bugs creep in and then get perpetuated. It can often take a keen eye like premelec's to spot the issue. Such things should get caught in debugging and testing but probably will not be when the result looks exactly like what was erroneously expected.
 
Top