Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 17:34 08 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 : get week day function memory issue?

     Page 1 of 2    
Author Message
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5031
Posted: 09:35pm 15 Jan 2017
Copy link to clipboard 
Print this post

Hi

This has probably been covered before, sorry if so, please excuse my ignorance, I haven't touched a Micromite for over a year and now need to finish off a project.

I'm using the function


Function GetWeekDay(Dt$)
DOW_Day=Val(Left$(Dt$,2))
DOW_Month=Val(Mid$(Dt$,4,2))
DOW_Year=Val(Right$(Dt$,4))
a=(14-DOW_Month)/12
a=Fix(a)
y=DOW_Year-a
y=Fix(y)
m=DOW_Month+(12*a)-2
m=Fix(m)
d=((DOW_Day+y)+Fix(y/4)-Fix(y/100)+Fix(y/400)+Fix((31*m)/12))
DOW=d Mod 7
GetWeekDay=DOW+1
End Function


where Dt$ is the date. It occasionally throws a Not enough memory error. Its a Micromite ver 5.01.

Has anyone else encountered this? Is there a command to see how much memory is used? I dont think the program is that big or using that many variables.

Glenn


The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5915
Posted: 09:53pm 15 Jan 2017
Copy link to clipboard 
Print this post

The command for finding out memory usage is:
MEMORY

Jim
VK7JH
MMedit   MMBasic Help
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3671
Posted: 11:01pm 15 Jan 2017
Copy link to clipboard 
Print this post

You may be short of memory, but you could shorten the code anyway!

I think each fix(blah/blah)
can be changed
to blah \ blah

(integer division)

John
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 11:30pm 15 Jan 2017
Copy link to clipboard 
Print this post

Hi Glenn,

Few things:

1> Which PIC / MM is it you are using.

2> IF not an MX150, then I recommend first to upgrade to v5.3 that has just been released last week

3> As Jim states; MEMORY is the command to type at the Command Prompt - Post the result if possible,

4> IF you are using the excellent MMEDIT, then try uploading a 'crunched' version of your program. I once hit a limit about 80% of the way through finishing my program and thought I would have to totally re-write things due to the memory filling (and the strange resultant behaviour). Doing a 'crunch' allowed me to finish it all and get it working. Hopefully this will fix your issue too.

5> A whole year without 'touching a Micromite'!

WW
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9070
Posted: 11:36pm 15 Jan 2017
Copy link to clipboard 
Print this post

This is what I use:


'----------------------
'DATE AND TIME DISPLAY:
'----------------------
SUB BUILD_DATE
YEAR=VAL(MID$(DATE$,7,4)):MONTH=VAL(MID$(DATE$,4,2)):DAY=VAL(MID$(DATE$,1,2))
DOW_DAY$=DOW$(DayOfWeek(YEAR,MONTH,DAY)) + ", " + DATE$
CtrlVal(MM_DAD)="THE DATE TODAY IS: "+DOW_DAY$
END SUB


...and the DayOfWeek function:


Function DayOfWeek(year, month, day)
a = int((14-month)/12)
m = month + 12*a - 2
y = year - a
DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7
End Function


...and the simple one-dimensional array to store the day names:


DIM DOW$(6)
DOW$(0)="Sunday":DOW$(1)="Monday":DOW$(2)="Tuesday":DOW$(3)="Wednesday"
DOW$(4)="Thursday":DOW$(5)="Friday":DOW$(6)="Saturday"


...put it all together on the screen:



Edited by Grogster 2017-01-17
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3671
Posted: 11:49pm 15 Jan 2017
Copy link to clipboard 
Print this post

Weird kind of echo here...

I think each int(blah/blah)
can be changed
to blah \ blah

(integer division)

John
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 12:07am 16 Jan 2017
Copy link to clipboard 
Print this post

It would be interesting to know on what line it throws an error but I bet that it is:
d=((DOW_Day+y)+Fix(y/4)-Fix(y/100)+Fix(y/400)+Fix((31*m)/12))

The things that consume memory and can cause this error include:
- string variables, especially arrays of strings
- complex expressions, especially involving strings
- subroutines and functions nested many levels deep, especially if passing strings as arguments

Geoff


Geoff Graham - http://geoffg.net
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5031
Posted: 01:22am 16 Jan 2017
Copy link to clipboard 
Print this post

MEMORY. That makes sense. Must be mentally exhausted, I looked right past that in the manual, looking to see if there was a MEM command.

The program is using 11k flash, 49k free. I have 53k ram. Not sure on the chip, its difficult to get to, but I guess over a year old. Using the touchscreen.

On run, raised the error, MEMORY returns nonsense. 245795k used ( 68407% ), 4294721554k ( -68307%) free

Before I crash it, if I exit the program I see 47k ram used, which seams high. Not a lot of room left there.

I changed to Grogster's code and it seams to be behaving itself, but I still need to add more functions to the code so I better rethink my approach.

Not a real lot happening in the program, but I do have two string arrays.

SirenTimeArray$(7,20)
PubHolidays$(10)

Maybe these are the culprits. They are mostly empty, but I guess space is assigned to them anyway. Is there a calculation for memory used in string arrays? Each cell only needs to hold a date or a time string value.

Because they are written to only occasionally, I might save in flash instead of using arrays. I work with databases on servers most of the time and the trick is to dump the database tables into a array then use code to search the array, its faster and ram is abundant. I guess this is not really the best practice for a microcontroller.

Yes sad to say I havn't played around with much electronic stuff for the last year, trying to get my career sorted and dont have much free time.

Thanks
Glenn
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2292
Posted: 01:35am 16 Jan 2017
Copy link to clipboard 
Print this post

you are flying pretty close to the wind with those two arrays, a test program with them declared has just 4k of free RAM left.

do the strings in the arrays need to be very long? you can specify the length of the strings using:

Dim SirenTimeArray$(7,20) length 20
Dim PubHolidays$(10) length 20


this will ensure each string is only allocated space for 20 characters.


cheers,
rob :-)
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5031
Posted: 12:48pm 16 Jan 2017
Copy link to clipboard 
Print this post

Thanks Rob, that made all the difference. I'm now using 6k of ram instead of 47k. Maybe I should read the manual.

Glenn
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 11:21am 19 Jan 2017
Copy link to clipboard 
Print this post

Curiosity always gets me on what projects actually are...

I'm guessing it's some sort of start/stop work siren control?

Dim SirenTimeArray$(7,20) length 20
Dim PubHolidays$(10) length 20



Phil.
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5031
Posted: 12:47pm 19 Jan 2017
Copy link to clipboard 
Print this post

Yes Phil, is a workshop siren with a touchscreen controller. The SirenTimeArray(7,20) is for each day of the week, 20 possible times each day they will want the siren to sound. PubHolidays(10) is for dates the siren should be quiet. Once a year you would go in and edit the holiday dates for the next year.

I started the project a year or so ago, but it was taking too much time to develop, more than the client was willing to pay. But a few days ago the client called and said they want to go ahead with it anyway.

Glenn
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 06:33pm 19 Jan 2017
Copy link to clipboard 
Print this post

Cool Glenn.

I've often had DOW thought's in the back of my mind for both my ceiling heat & Spa controller.

I know our behaviour pattern is different on weekends & public holidays; hot water may be wanted earlier in the Spa & something smarter than manual over ride is something I've considered to change it from Solar to heat pump modes on those days...
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 525
Posted: 05:29pm 28 May 2017
Copy link to clipboard 
Print this post

Still catching up on old post.

Here is what I have been using
Function dow(m,d,y)

If m < 3 Then
m=m + 12
y=y-1
EndIf

dow = 1 + (d + ((26 * m+1)/10) - (2 * (y\100)) + y + (y\4) + (y\400) ) Mod 7

' 1 2 3 4 5 6 7
' sun mon tue wed the fri sat

End Function

 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1986
Posted: 09:53pm 03 Aug 2017
Copy link to clipboard 
Print this post

can't quite work out why this isn't taking 2000 into account as a leap year. The maths looks like it should but it is +1 day for anything before 01MAR2000

it's fine for everything afterEdited by CaptainBoing 2017-08-05
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 10:15pm 03 Aug 2017
Copy link to clipboard 
Print this post

I posted this in Januari 2014 and i have been using it since the GWBasic days on the PC, so it should be time tested.
I use it often in programs as one of those handy to have functions.
It is a bit cryptic, but it is small and FAST. :)
[code]
function DayOfWeek(year, month, day)
  a = int((14-month)/12)
  m = month + 12*a - 2
  y = year - a
  DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7
end function
[/code]

I think Grogster uses it also (see his post) and maybe he can confirm it works good. I only tested it for current dates, and in that case it works perfectly.
Edited by MicroBlocks 2017-08-05
Microblocks. Build with logic.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1986
Posted: 10:53pm 03 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said   I posted this in Januari 2014 and i have been using it since the GWBasic days on the PC, so it should be time tested.
I use it often in programs as one of those handy to have functions.
It is a bit cryptic, but it is small and FAST. :)
[code]
function DayOfWeek(year, month, day)
  a = int((14-month)/12)
  m = month + 12*a - 2
  y = year - a
  DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7
end function
[/code]

I think Grogster uses it also (see his post) and maybe he can confirm it works good. I only tested it for current dates, and in that case it works perfectly.


LOL. I added this as an alternative just as you posted here. Have updated the article with you as credit. This is exactly the sort of gold that mustn't be lost - you have had this for decades... like the CR2 Sort routine - also GWBasic heritage and the fastest sort ever.Edited by CaptainBoing 2017-08-05
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3671
Posted: 11:49pm 03 Aug 2017
Copy link to clipboard 
Print this post

  CaptainBoing said   like the CR2 Sort routine - also GWBasic heritage and the fastest sort ever.

That CR2 Sort item says Shellsort can fail - something I rather doubt is true!

Evidence?

John
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1986
Posted: 12:19am 04 Aug 2017
Copy link to clipboard 
Print this post

This is odd... This is something I have "known" since college is was associated with a specific sequence. However looking at the Wikipedia article there is no mention of it and I am certain it would be in there plus looking at the algorithm I can't see the opportunity.

Given also that it is in the linux core I think sweary-Linus would have kicked it out.

I will modify my brain and the article. Thanks for picking up on this.Edited by CaptainBoing 2017-08-05
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3671
Posted: 12:26am 04 Aug 2017
Copy link to clipboard 
Print this post

Thanks.

It does have the common drawback that there is a worst case input, in the sense that it takes longer than usual to sort. But that is common to most or all sorting methods.

John
 
     Page 1 of 2    
Print this page
© JAQ Software 2024