Email
 Microcontroller and PC projects Forum Index : Microcontroller and PC projects         Section -- Select Forum -- Off grid living and renewables   - Windmills   - Solar   - EV's Electronics and Microcontrollers   - Electronics   - Microcontroller and PC projects   - PCB Manufacturing Miscellaneous   - Other Stuff   - Site News   - Off topic archive. Classifieds   - Price Watch   - Wanted   - For Sale Guest Book   - Guest Book
 Topic: Newie got a few questions
 << Prev Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 of 34 Next >>
Author
 Message << Prev Topic | Next Topic >>
lew247
Guru

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 925
 Posted: 05 March 2016 at 5:22pm | IP Logged
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)

JohnS
Guru

Joined: 18 November 2011
Location: United Kingdom
Online Status: Offline
Posts: 1660
 Posted: 05 March 2016 at 5:29pm | IP Logged
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

WhiteWizzard
Guru

Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2588
 Posted: 05 March 2016 at 5:49pm | IP Logged
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.

Is this what you're after??

WW

__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org

lew247
Guru

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 925
 Posted: 05 March 2016 at 8:21pm | IP Logged
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

WhiteWizzard
Guru

Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2588
 Posted: 05 March 2016 at 8:51pm | IP Logged
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

HankR
Senior Member

Joined: 02 January 2015
Location: United States
Online Status: Offline
Posts: 170
 Posted: 05 March 2016 at 9:21pm | IP Logged
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.

lew247
Guru

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 925
 Posted: 06 March 2016 at 10:10am | IP Logged
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?

lew247
Guru

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 925
 Posted: 06 March 2016 at 10:14am | IP Logged
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

lew247
Guru

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 925
 Posted: 06 March 2016 at 10:22am | IP Logged
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

WhiteWizzard
Guru

Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2588
 Posted: 06 March 2016 at 11:16am | IP Logged
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

WhiteWizzard
Guru

Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2588
 Posted: 06 March 2016 at 11:20am | IP Logged
@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

matherp
Guru

Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2164
 Posted: 06 March 2016 at 11:36am | IP Logged
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