![]() |
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: AustraliaPosts: 23 |
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 KingdomPosts: 7937 |
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: AustraliaPosts: 23 |
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 KingdomPosts: 7937 |
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 KingdomPosts: 10310 |
Try CtrlVal (#31) =Str$(pin(14),2) |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7937 |
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: AustraliaPosts: 1003 |
v=CINT(100*pin(14)) CtrlVal (#31) =str$(v/100,2,2) Latest F4 Latest H7 FotS |
||||
envirotronics Newbie ![]() Joined: 18/02/2016 Location: AustraliaPosts: 23 |
Hi, I jhave tried that and error is "Expected a number" |
||||
envirotronics Newbie ![]() Joined: 18/02/2016 Location: AustraliaPosts: 23 |
Thanks Guru, I will try that tomorrow |
||||
envirotronics Newbie ![]() Joined: 18/02/2016 Location: AustraliaPosts: 23 |
I tried tat and it comes up with ERROR: "expected a number" |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
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 KingdomPosts: 7937 |
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: AustraliaPosts: 6283 |
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: AustraliaPosts: 23 |
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 KingdomPosts: 7937 |
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 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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |