Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:19 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Reading AIN and display in Number Box 2 decimal places

Author Message
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 07:13am 01 Jun 2021
Copy link to clipboard 
Print this post

Hi,
I need to read and Analog port and display the reading in a number box limiting the reading to 2 decimal places. Cant seem to do it. Can anyone assist? Current reading displays at least 6 decimal places.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 07:28am 01 Jun 2021
Copy link to clipboard 
Print this post

A good starting point is STR$(). It's a pretty flexible function. :)

STR$(1.2345678,,2) will display 2 characters after the decimal place i.e. 1.23. Anything after that is truncated - it won't round up 1.239 to 1.24.

Also look at FORMAT$(nbr [,fmt$]). This can use a fmt$ to format nbr into a particular format. Maybe not exactly what you are looking for, but it might come in useful for a display.

Here's an old function from an old TRS-80 book. This rounds to 2 decimal places correctly. :)  Never throw old computer books out.

Function round(n)
 round=Fix((Fix(n*1000)+Sgn(n)*5)/10)/100
End Function
Edited 2021-06-01 18:01 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 08:41am 01 Jun 2021
Copy link to clipboard 
Print this post

Thanks Mick
I was doing something like that but no go.
See code below
Any clues"
'tests AIN straight to Gauge and number ox'

cls
Const frm_big2=82
Const c_hi2=34, c_lo2=35,c_bot2=37
nbr_hi2=31
GUI Caption c_hi2, "SET PRESSURE PSI",285,70, "LT",RGB(red)
Font 4: GUI Frame frm_big2,"",0,0,790,470,RGB (green)
GUI gauge #32,100,380,90,RGB(green),0,0,3.3,2,"LEAK PSI",RGB(yellow),1.5 ,RGB(blue),2.5,RGB(red)
GUI Numberbox nbr_hi2,310,100,200,MM.FontHeight+40,RGB(yellow),RGB(64,64,64)
setpin 59,dout
setpin 14, ain
READVOLTS:
CtrlVal (#32) =(pin (14))
CtrlVal (#31) =Str$(pin(14),,2)
pause 1000


goto READVOLTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 09:20am 01 Jun 2021
Copy link to clipboard 
Print this post

I've nothing to run the GUI commands on yet. I've not got round to wiring a LCD display onto my F4. It's a "tomorrow" job ...

Your prog looks ok to my unpracticed eye. You could probably use CtrVal(#31)=round(Pin(14)) if you wanted to try the function I mentioned.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 10:08am 01 Jun 2021
Copy link to clipboard 
Print this post

Try
CtrlVal (#31) =Str$(pin(14),2)
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 10:29am 01 Jun 2021
Copy link to clipboard 
Print this post

That'll work, Peter, but it won't round to 2 decimal places, it's a truncation.
So 1.234 and 1.235 would both give 1.23.
The function I showed will automatically round up, so 1.234 gives 1.23 and 1.235 gives 1.24.

Of course, it may not matter. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 11:15am 01 Jun 2021
Copy link to clipboard 
Print this post

v=CINT(100*pin(14))
CtrlVal (#31) =str$(v/100,2,2)

Latest F4 Latest H7 FotS
 
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 12:25pm 01 Jun 2021
Copy link to clipboard 
Print this post

  Mixtel90 said  That'll work, Peter, but it won't round to 2 decimal places, it's a truncation.
So 1.234 and 1.235 would both give 1.23.
The function I showed will automatically round up, so 1.234 gives 1.23 and 1.235 gives 1.24.

Of course, it may not matter. :)


Hi,
I jhave tried that and error is "Expected a number"
 
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 12:27pm 01 Jun 2021
Copy link to clipboard 
Print this post

  disco4now said  
v=CINT(100*pin(14))
CtrlVal (#31) =str$(v/100,2,2)


Thanks Guru, I will try that tomorrow
 
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 12:30pm 01 Jun 2021
Copy link to clipboard 
Print this post

  envirotronics said  
  disco4now said  
v=CINT(100*pin(14))
CtrlVal (#31) =str$(v/100,2,2)


Thanks Guru, I will try that tomorrow


I tried tat and it comes up with ERROR: "expected a number"
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 01:48pm 01 Jun 2021
Copy link to clipboard 
Print this post

It is not properly specified in the manual (which I will fix) but all controls expect to be assigned a number (float or integer) except Frame, Caption, Display Box, Text Box and Format Box which expect a string.

So you should use:
v=Cint(100*Pin(14))
CtrlVal(#31) = Val(Str$(v/100,2,2))


Although this is simpler:
CtrlVal(#31) = Cint(100*Pin(14))/100


Geoff
Geoff Graham - http://geoffg.net
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 01:56pm 01 Jun 2021
Copy link to clipboard 
Print this post

Yep - didn't spot that. It needs changing to:

CtrVal = Val(Str$(Pin(14),2))

Str$(Pin(14),2) is returning a formatted string, of course, so Val() will convert it back into a number.

It's the same in the other suggestions.

CtrVal = round(Pin(14))
with
Function round()

would get the analogue value from Pin14 and round it without any string conversions.

There's always more than one way to skin a cat. :)
Edited 2021-06-01 23:58 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:37pm 01 Jun 2021
Copy link to clipboard 
Print this post

  Mixtel90 said  That'll work, Peter, but it won't round to 2 decimal places, it's a truncation.
So 1.234 and 1.235 would both give 1.23.
The function I showed will automatically round up, so 1.234 gives 1.23 and 1.235 gives 1.24.

Of course, it may not matter. :)


Just for the record,
STR$() does round. What happens with 0.5 is less certain but anything else will round.

print str$(1.23456,2,2)
print str$(1.235,2,2)
print str$(1.2356,2,2)
print str$(1.245,2,2)


Jim
VK7JH
MMedit
 
envirotronics
Newbie

Joined: 18/02/2016
Location: Australia
Posts: 23
Posted: 10:01pm 01 Jun 2021
Copy link to clipboard 
Print this post

  Geoffg said  It is not properly specified in the manual (which I will fix) but all controls expect to be assigned a number (float or integer) except Frame, Caption, Display Box, Text Box and Format Box which expect a string.

So you should use:
v=Cint(100*Pin(14))
CtrlVal(#31) = Val(Str$(v/100,2,2))


Although this is simpler:
CtrlVal(#31) = Cint(100*Pin(14))/100


Geoff


Thanks Geoff.
Works well
Graham
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 07:46am 02 Jun 2021
Copy link to clipboard 
Print this post

  TassyJim said  
  Mixtel90 said  That'll work, Peter, but it won't round to 2 decimal places, it's a truncation.
So 1.234 and 1.235 would both give 1.23.
The function I showed will automatically round up, so 1.234 gives 1.23 and 1.235 gives 1.24.

Of course, it may not matter. :)


Just for the record,
STR$() does round. What happens with 0.5 is less certain but anything else will round.

print str$(1.23456,2,2)
print str$(1.235,2,2)
print str$(1.2356,2,2)
print str$(1.245,2,2)


Jim
I wonder where I screwed up when I tried it? Weird....
Thanks, Jim, I stand corrected and I promise to be more diligent in my testing in future (but then, we all say that!). :)

1.235 doesn't round up, 1.236 does.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025