DECIMAL POINT HEADACHE

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

DECIMAL POINT HEADACHE

Post by siliconchip »

Hi all,
im having trouble with the decimal point, if the value is under 1000 ohms then all is well however when the value goes above 1000 i get the display showing for example 1013.Kohms (ohms denoted by the 4), if when the value is above 1000 how can i get the decimal point to show 1.013 k or even 1.01 k, im presuming its something to do with the floating point value but ive been unsuccessful in moving the decimal, any thoughts please

cheers bob
Attachments
DECIMAL POINT..fcfx
(21.46 KiB) Downloaded 99 times

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by Steve »

Divide your floating point value by 1000 - that should move the decimal point 3 places to the left. Then convert it to a string and append the "k".

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi steve
thanks for the reply you beat me to it i knew i needed to divide by 1000 but couldnt find the right way to do it ive added my new flowchart for comparison, ive modified the VALUE macro as well as the ADC macro seems to be doing what im after now although i will need to further modify to show Meg Ohms not sure if mines the right way was there a simpler method or does it even matter as mine is working ??

cheers bob
Attachments
DECIMAL POINT..fcfx
(23.01 KiB) Downloaded 93 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob.
I have attached the way I would handle the dividing.
With the attached flowchart,
If R = 123.456 then the result on the display should be 123.4Ω
If R = 123456.789 then the result on the display should be 123.4KΩ
If R is 123456789.01 then the result on the display should be 123.4MΩ
Attachments
DECIMAL POINT v2.fcfx
(22.98 KiB) Downloaded 98 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin,

WOW what youve done is pure art i will try your version in hardware and although my version is without doubt primitive and rough round the edges compared to yours i enjoyed the challenge i set myself but it does start to become consuming once i start, on a day off today i spent 6 hours all be it with a couple of short breaks to try and get results which i feel i got but when seeing something like yours just shows i have a lot more to learn dont get me wrong i appreciate what you have done its brill and although mine is functional yours is great, i will break it down step by step to understand your method its a great way for me to grasp other peoples methodology and hopefully improve my own as ive said in the past a big thanks i will post what i find when trying yours in hardware

cheers martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob.
You're welcome and thank you for the nice comments.
The flowchart is a demonstration of how I would divide down depending on the result.
I have not checked the rest of the flowchart, nor have I tested o hardware.
If you find it does not work as expected, then I will help you further.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin
just tried your version in hardware the kilohm function seems to be fine however when testing a value of 147,4 ohm for example the display says 14.74 ohms this is the same for any value under 1000 ohms the decimal needs moving to the right one space not tried any Meg ohm values yet, had a quick fiddle with the calc boxes but didn't find a cure problem seems to be when using the step into function the calculation box at the bottom of the ADC macro that's where I've left it for now

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Whoops, sorry.
The divider is to the power of
No divide is 10 to the power of 0 = 1.
I used 10 to the power 1 = 10
To fix that just change the 1 to 0:
No divide.png
No divide.png (21.56 KiB) Viewed 11923 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin
thats fixed it , could i ask how you inserted the png pic please

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob, that's excellent.
Thanks for letting up know.
siliconchip wrote:
Wed Feb 09, 2022 11:30 am
could i ask how you inserted the png pic please
Select the Attachments tab, then Add files button to add the image.
You can place the image anywhere within the post by selecting where you want the image to go.
Select Place inline for the image you are interested in if more than one was added.
If you don't select Place inline then the image will be shown after the last line of text.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin

thanks for the reply how do you save the image within flowcode im getting a bmp file which wont let me attach in the forum ??

bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

What I do is take a screenshot of the area I want, using a free program called Greenshot.
When you take a screenshot, after selecting the area you wish to capture, a popup appears asking how you want the screenshot saved.
Save  capture.png
Save capture.png (105.42 KiB) Viewed 11911 times
I save to paint.
I then use paint to save as png.
You could open your BMP in Pant and select File, save as PNG
If you don't want use Greenshot then if you press print screen, the capture will be saved to the clipboard.
You can open Paint, and paste (ctrl V) or use the paste clipboard icon.
Select the area you wish to use, then use crop.
Finally, save as PNG.

You can save as PNG within Flowcode.
Select File, Save Image.
Select either Flowchart, Dashboard, or system.
Select the dropdown that shows (*.BMP) then change it from (*BMP) to (*.PNG)
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

Hi martin
big thanks ive one more issue since changing the calc box to divider =0 when no resistance is connected i dont get the please connect come up on the display this did work before i made the change, i tried adding a decision box in the ADC macro with if R=0 this would then bypass the divider section but no joy
download/file.php?mode=view&id=33940
download/file.php?mode=view&id=33941
cheers bob
Attachments
oop.png
oop.png (79.19 KiB) Viewed 11907 times
R=0.png
R=0.png (29.32 KiB) Viewed 11907 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob.
If the display does not show not connected, what does the display show as the value being?
When you get the reading, you can then change the

Code: Select all

R = 0
in main to if R < (ValueShown + 0.1)
Perhaps the maths is making the value slightly larger, e.g. 0.0001, which then means R is not 0.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

Hi martin
on start up with no value being measured the display shows 0.000MOhms

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

In that case, within main.
can you change:
If R = 0 to If R < 0.1
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin
ive used a decision box in ADC macro above the divider calcs and put if R =0.000 so if no resistance it bypasses the diver blocks, ive then in the main put R=0.000 and it now seems to work, just seen your post so i will try your suggestion as well still need to try the Meg ohm function but will keep you posted and thanks for your input its much appreciated

cheers bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin

been playing a little again with this and when checking the Meg ohm function nothing happens i connected a 1Meg resistor and it comes up as 0.000 plus the please connect wont work i thought this may be an issue with the 267.3 ohm ref resistor so i placed a 1Meg resistor here and tried to measure another 1 Meg but the display showed 16,88k i tried to loook at the maths but after a night shift im tying myself in knots all seems good now except the meg reading, im thinking using another anologue port to measure higher resistances due to the small ref resistor have you any thoughts

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Do you have a ref resistor going from the ADC pin to VDD and a test resistor going from ADC pin to GND?
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

Hi martin

Yes exactly that

Cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob.
I have got an update for you to try.
The hardware should work in the region of MΩ and the open-circuit should work better.
It will take ADC input impedance/leakage into account for higher accuracy if you wish change the .PullUpResistorUsed = 1 to .PullUpResistorUsed = 0 within ADC user macro.
If you do it means the reference resistor has to be pull-down instead of pull-up.
I have added some notes to the flowchart and changed some variables to locals for better programming practice and to save memory.

A suggestion.
You could add auto-ranging if you would like to experiment?
Autoranging can be done based on the ADC value.
Attachments
DECIMAL POINT v3.fcfx
(27.07 KiB) Downloaded 58 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin
a big thanks am i correct in thinking now that the ref resistor is 1.5Meg ?? as theres no reference to the original 267.3 ohm one, plus looking at the ADC macro has confused me a bit. as for local variables is it best practice to use these anyway or just when space is getting short, also there is a calculation box referencing Resistance 2 but this is not seen any where else ??
download/file.php?mode=view&id=33947

cheers bob
Attachments
kl.png
kl.png (22.3 KiB) Viewed 11810 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob, the value of the reference resistor can be any value you like.
It's only 1.5M because that's what I believe is best for testing resistors in the M range.
The lower the resistance will be away from the reference resistor, in theory, the less accurate the results will be.
You can change it back to 267.3R of course, But you won't be able to measure anything above say 2K with any accuracy.
If it was me I would go no higher or lower than a factor of 7 of the reference resistor at a push.
The lower the factor the better.
1.5M is a factor of 1.5E6/267.3 = 5612!

It's best practice to use floats even if you have loads of space.
Resistance2 I used for testing.
It's no longer required so can be deleted.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by siliconchip »

hi martin

a quick question, as you have stated the reference resistor at 1.5Meg im guessing i need to fit a 1.5 Meg to test this function also if i were to go back to the original 267.3 ohm resistor likewise again change the ref value from 1.5M to 267.3

regards bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: DECIMAL POINT HEADACHE

Post by medelec35 »

Hi Bob.
yes, you are spot on.
If you want to test resistors in the meg range, then the reference resistor needs to be in the meg range and the .ReferenceResistor has to be set for the same value.
I would suggest you need three ranges of ref resistances.
Mid 100's ohm
Mid 100k's ohm
and perhaps mid megohm?
Use the ADC value to auto-select the required range via either relays, reed switches or analogue switches
starting off with the lowest ref value.
Loop: If ADC >900 then switch in a higher value resistor: Exit loop
Martin

Post Reply