Home  |  Contents 
Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: Newie got a few questions Post ReplyPost New Topic
<< Prev Page of 33 Next >>
Author
Message << Prev Topic | Next Topic >>
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 05 March 2016 at 5:22pm | IP Logged Quote lew247

Another one I've spent all day trying to work out for myself but cannot.
How do I limit a number to say 2 decimal places?
for instance using a gps in the location routine I only want 2 digits after the decimal place
or say using a pressure sensor and it gives the pressure as say for instance 991.587 mB
How can I round that down to one digit or 2? (obviously only if I don't need such a precise reading)


Back to Top View lew247's Profile Search for other posts by lew247
 
JohnS
Guru
Guru


Joined: 18 November 2011
Location: United Kingdom
Online Status: Offline
Posts: 1562
Posted: 05 March 2016 at 5:29pm | IP Logged Quote JohnS

You can arrange to display or print them that way but floating point numbers are NOT stored that way (i.e. they're not stored with a fixed number of decimals) and do not behave that way.

Try the FORMAT$ function for now.

John

Edited by JohnS on 05 March 2016 at 5:30pm
Back to Top View JohnS's Profile Search for other posts by JohnS
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2436
Posted: 05 March 2016 at 5:49pm | IP Logged Quote WhiteWizzard

Look at the STR$(a$,a,b) command where a = number of digits before decimal point, and b = number of digits after decimal point (for the string a$)

This is for printing purposes only (for example when wishing to show data on the console screen (with PRINT) OR on data on a TFT screen (with TEXT)).

So PRINT STR$(a$,3,2) will give you two decimal places (and three before the decimal point) on the console screen.

More info on Page 77 of MicroMite User Manual v5.1

Is this what you're after??

WW

__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 05 March 2016 at 8:21pm | IP Logged Quote lew247

Thanks, thats perfect for now

Has anyone used a GPS set the time on an RTC and periodically update it?

would it be easy to sort it out (the code part)

I know it's probably overkill, but I will have have a gps unit outside anyway so might as well make use of it
and my reasoning for this (apart from I like being extremely precise when it comes to time)
is the outside unit already will be streaming the time date and day of week, inside with a HC-12 (it's already done the calculations to get the day, DST and I won't have to worry about leap years or anything if it updates the RTC say once a day

Back to Top View lew247's Profile Search for other posts by lew247
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2436
Posted: 05 March 2016 at 8:51pm | IP Logged Quote WhiteWizzard

lew247 wrote:
Has anyone used a GPS set the time on an RTC and periodically update it?


Oh yes - many a time.

Several ways to do it but the one I use most is this:

Each time you get a 'valid' GPS record (and therefore have the GPS time & date info), simply set the TIME$ and DATE$ built into MMBASIC.

Then every time you need to refresh the displayed time, simply use MMBasic's Time$/Date$ as required.

This way, if you get an invalid GPS signal, the time and date will keep ticking along in MMBasic and hence your displayed time will be shown with only a slight 'drift' IF GPS signal remains invalid for extended time periods. As soon as next valid GPS signal received, then MMBasic will be set accurately once again.

If you have an RTC too, then this could be set instead of MMBasic's Date$ & Time$. However, I have not included RTC's in my circuits with GPS Date/Time info as not needed mission critical time info in the instance that no GPS signal is present (for whatever reason) AND MMBasic has drifted too much.

I have a uBlox 7 acting as a 'Time Server' in my house and it has run flawlessly for many months now. On the couple of power failures at my house, the GPS is lost, but the MMBasic still ticks happily along. As soon as power kicked back in then GPS re-syncs within a couple of minutes and I am none the wiser that the clock's time was wrong!

Out of interest for others, another source of time I use is from a RPi/MicroMite combination; using the RPi's 'Date' command to then extract the data from the internet.

Finally, an ESP8266 module can get the time/date info from an NTP server - I remember seeing code on TBS once but have not used this method myself.

WW

Edited by WhiteWizzard on 05 March 2016 at 8:53pm


__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
HankR
Senior Member
Senior Member


Joined: 02 January 2015
Location: United States
Online Status: Offline
Posts: 170
Posted: 05 March 2016 at 9:21pm | IP Logged Quote HankR

lew247 wrote:

How do I limit a number to say 2 decimal places?
for instance using a gps in the location routine I only want 2 digits after the decimal place

Without having to generate strings, the classic way to do this in BASIC is to use the INT function.

Here is a function called "ROUND" that will properly round a float value to two decimal places:

FUNCTION ROUND(x)
  ROUND = INT(x * 100 + 0.5)
  ROUND = ROUND/100
END FUNCTION 

Substitute 1000 for three decimal places, etc.

The .5 term is to cause correct rounding up and not just a truncation of excess decimal places.

There is a slightly more complex version of this algorithm that handles negative numbers just a little differently. This will get you started, though.
Back to Top View HankR's Profile Search for other posts by HankR
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 06 March 2016 at 10:10am | IP Logged Quote lew247

More confusion
I've moved onto trying to set up the anemometer but I'm getting a problem straight away with this bit of code at the start


 'Constants 
  Const wSpeedPin=6 
  Const wDirPin=8 

 'Define I/O 

  'Setpin redLed,Dout 
  Setpin wSpeedPin, Intl,anemometerClick,Pullup 


When I try and run it comes up with this error
Quote:

[31] SetPin wSpeedPin, Intl,anemometerClick,Pullup
Error: Pin 6 is reserved on startup


What am I doing wrong?
Back to Top View lew247's Profile Search for other posts by lew247
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 06 March 2016 at 10:14am | IP Logged Quote lew247

Sorry IGNORE the previous - my stupidity
I'm using an uM+ to test the code and hardware and didn't check the manual
Pin 6 is one of the console pins
Back to Top View lew247's Profile Search for other posts by lew247
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 06 March 2016 at 10:22am | IP Logged Quote lew247

I do have a problem I cannot fix now

Quote:

[40] anem_count=0
Error: Variable or function not declared: ANEM_COUNT


My code has this near the start
' Global variables 
  anem_count=0 
  anem_last=0 
  anem_min=1000 
  HGust=0 
  rain_count=0 
  rain_last=0 
  wCount=1 
  wdCount=1


I can't figure this one out myself
Back to Top View lew247's Profile Search for other posts by lew247
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2436
Posted: 06 March 2016 at 11:16am | IP Logged Quote WhiteWizzard

I'm not 100% sure about this, but maybe you can't have an underscore in the variable name.

For now, rename all variables without the underscores and see what happens!

WW

__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2436
Posted: 06 March 2016 at 11:20am | IP Logged Quote WhiteWizzard

@geoffg

I can't see anything in the User Manual regarding naming 'rules' for variables.

If there are any length restrictions and/or character restrictions then it may be worth highlighting them on Page 33

__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 1851
Posted: 06 March 2016 at 11:36am | IP Logged Quote matherp

Quote:
but maybe you can't have an underscore in the variable name.


Yes you can I use them all the time.

Have you got a statement "OPTION EXPLICIT" in your code? Probably yes if you copied anything off me

That requires you to declare all variables before use

e.g. DIM anem_count=0

This may seem like a pain but is much the best way of avoiding obscure bugs in your code

Edited by matherp on 06 March 2016 at 11:36am
Back to Top View matherp's Profile Search for other posts by matherp
 


<< Prev Page of 33 Next >>
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1250 seconds.
Privacy Policy     Process times : 0.01, 0, 0, 0.11