Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 14:18 11 May 2024 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 : Micromite Floating point HELP

Author Message
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 06:51am 04 Feb 2015
Copy link to clipboard 
Print this post

Micromite Floating point HELP.

I read voltage on analog pin
setpin 23, ain
print pin (23)
3.29871

I don't need so many digits since the last few change all the time. I need display just like simple 4-digit voltmeter 3.29V

Can that be done? Is there some way to convert Floating point number?

 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1138
Posted: 06:54am 04 Feb 2015
Copy link to clipboard 
Print this post

Hi and welcome to the forum.

just try the Str$ function please.

Michael

edit:
MM Manual (p. 63) ? STR$(pin (23),0,2)&"V"

Should work.Edited by twofingers 2015-02-05
 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 07:08am 04 Feb 2015
Copy link to clipboard 
Print this post

Can you provide example?

The Str$(number) works when you provide the number. I have no clue how to make the string capture reading from analog pin.

I did get all kinds of errors when I experimented the last few days trying to trunkate the reading.

You can assign a=pin(23) but I could not assign it to string b$, etc.

 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1138
Posted: 07:11am 04 Feb 2015
Copy link to clipboard 
Print this post

I changed my post. Please read again!
 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 07:23am 04 Feb 2015
Copy link to clipboard 
Print this post

I tried it and did get the correct reading.

THANK YOU!!!

 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1805
Posted: 01:21pm 04 Feb 2015
Copy link to clipboard 
Print this post

If you want the voltage to stay as a variable instead of a string, you can multiply by 100 to get 329.871 then use the Int command so you only have the integer 329 then divide by 100 to get 3.29
Paul
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 844
Posted: 02:05pm 04 Feb 2015
Copy link to clipboard 
Print this post

Also see the CINT() function which returns the nearest integer.
Gerry
Latest F4 Latest H7
 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 02:25pm 04 Feb 2015
Copy link to clipboard 
Print this post

Thanks Guys. I'll experiment see what works best.

I was pretty good with GWBasic, BAT files, DOS, etc. I forgot all of it.

 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 09:15pm 04 Feb 2015
Copy link to clipboard 
Print this post

With rounding the value solved, here is a good technique to use when reading analog values.

There are very complicated and simple ones. :)
I like the simple ones like a running average.

Define an array and some variables
[code]
DIM Samples(10)
Dim NumberOfSamples, Total, Average

NumberOfSamples = 10
Total = 0
Average = 0
Index = 1
[/code]

Fill the array with initial samples
[code]
for i = 1 to 10
samples(i) = pin(23)
Total = Total + Samples(i) 'not use pin(23) because that would be a new sample!
pause 10
next
[/code]

Then in your main code loop update the Voltage by calling a function:
[code]

Voltage = ReadVoltage()
[/code]

The function that will take a new sample and calculates the running average
[code]
FUNCTION ReadVoltage()
Total = Total - Samples(index)
Samples(Index) = pin(23)
Total = Total + Samples(Index)

'Your new running average
Average = Total / NumberOfSamples

Index = Index + 1
If Index > NumberOfSamples then Index = 1

ReadVoltage = Average
END FUNCTION
[/code]
Check this for syntax errors because i wrote this here without testing. :)

This just keeps ten samples in an array and replaces the oldest one before calculating a new average. This smooths out readings so that spikes to don have a big effect.
Good for when you are testing values on thresholds. A single weird reading will not set it off.
The larger the array, the more samples, the more smoothing. All depends on what you need.

Edited by TZAdvantage 2015-02-06
Microblocks. Build with logic.
 
helltek
Newbie

Joined: 04/02/2015
Location: United States
Posts: 29
Posted: 10:13pm 04 Feb 2015
Copy link to clipboard 
Print this post

Thanks. I'll play some more. I'll try to average the readings and I will also need some calibration subroutine to run either at assembly time or maybe more often, but hopefully not.

I need voltage divider to read voltage higher than the 3.3v on ADC pins. At high resistance let say 9k and 1k (divide by 10), even 1% resistors or better introduce large error.

I may need one ADC pin fed by 3.3 volt and feed the voltage divider by the same 3.3 volt and write some subroutine to correct the error (make the reading match 3.3v as percentage of error and save the percentage to add or subtract from actual reading).

Maybe there are easier or better ways. This is all that comes to mind right now.

 
Print this page


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

© JAQ Software 2024