Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:46 09 Nov 2025 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 : Weird timer vs IF/THEN error...

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 02:15am 07 Mar 2017
Copy link to clipboard 
Print this post

This is really odd:





The SCREENSAVER routine should NOT be called, but it is called as soon as it runs into this command in this loop.

Console output proves that system timer is less then SSDELAY, but it just loops over to SCREENSAVER anyway - what gives?Edited by Grogster 2017-03-08
Smoke makes things work. When the smoke gets out, it stops!
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10565
Posted: 02:26am 07 Mar 2017
Copy link to clipboard 
Print this post

Are you using OPTION EXPLICIT?

If not probably a typo somewhere e.g. the setting of SSDELAY which you don't show

Of course GOTOs out of a DO LOOP deserve what they get
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 02:42am 07 Mar 2017
Copy link to clipboard 
Print this post

There is simply not enough nested depth within MMBASIC to do all I have to do inside the select case, so it won't work when you have about ten different GUI menus under each one of those GOTO's, as you would end up with about 50 or more deep nested loops - about the only way I could deal with it was with GOTO....

Here is the complete code for that section, and I am now getting results from all the labels that should not be executed unless the timer was greater then or equal to 20000

Yes, OPTION EXPLICIT used at the top of the code - has saved my bacon many times when I get variable names wrong etc.

[Quote=matherp]If not probably a typo somewhere e.g. the setting of SSDELAY which you don't show[/Quote]
Yes I do. It is in the console output part of the image above "? ssdelay", which returns 20000.


'----------
'MAIN LOOP:
'----------
START:
DO
If DEBUG then PRINT "Main menu."
'Tempr START PSU_BAT 'Begin background process to read battery backup temperature
'Tempr START PSU_TEMP 'Begin background process to read PSU temperature
'Tempr START NSP_TEMP 'Begin background process to read system temperature
CHECK_SETTINGS
KEY=0:LCD_EXIT=0:LCD_OK=0:TEST_PASSWORD=0 'Clear flags
BUILD_DATE
DATE_REF=VAL(MID$(DATE$,1,2))
PAGE 1,6 'Main menu and buttons
GUI INTERRUPT MENU 'This interrupt will be called when there is an LCD touch
Timer=0 'Reset timer
DO 'This loop detects any activity or other events while waiting for an LCD touch
If Val(MID$(DATE$,1,2))>DATE_REF then goto START 'If new day, then update the clock
If LOC(#2)<>0 then goto MSG_RXD 'Some kind of message was received.
If PIN(PSU_MFD)=1 And MFDFLAG=0 Then Goto MAINSFAIL 'MCT6 opto pulls this line LOW if power is OK
If PIN(PSU_MFD)=0 And MFDFLAG=1 Then Goto MAINSOK 'MCT6 opto says power is back now
If Pin(NSP_FUSE)=0 then goto FUSEFAIL 'MCT6 opto pulls this line LOW if receiver fuse is OK
If Pin(NSP_FAN_RPM)<25 then goto FANFAIL 'Count fansink RPM, and if too slow, inform users
If TIMER>=SSDELAY then goto SCREENSAVER 'Call screensaver as nothing touched and no messages to process.
LOOP until KEY<>0
Select Case KEY
CASE 1 'DATABASE - Get password before access
Goto GET_PASSWORD
Case 2
Goto OTHER_OPTIONS
Case 3
Goto MESSAGING
End Select
Loop

Print "Should never get here."

'--------------------
'MAINS FAIL DETECTED:
'--------------------
MAINSFAIL:
MFDFLAG=1
Print "Should never get here.(MAINSFAIL)"

'-----------------------
'MAIN POWER OK DETECTED:
'-----------------------
MAINSOK:
MFDFLAG=0
Print "Should never get here.(MAINSOK)"

'------------------------------
'RECEIVER POWER FUSE HAS BLOWN:
'------------------------------
FUSEFAIL:
Print "Should never get here.(FUSEFAIL)"

'----------------------------------
'SYSTEM FANSINK FAN RPM IS TOO LOW:
'----------------------------------
FANFAIL:
Print "Should never get here.(FANFAIL)"


'--------------------
'DISPLAY SCREENSAVER:
'--------------------
SCREENSAVER:
Do
If DEBUG Then Print "Screensaver called."
CLS 'Clear screen. This also HIDES all GUI controls!!!
For NBR=1 To 250 'Number of lines to draw before clearing the screen
X1=Rnd*MM.HRES
Y1=Rnd*MM.VRES
X2=Rnd*MM.HRES
Y2=Rnd*MM.VRES
R=Cint(Rnd)*255 'Ensure 'r' is one or zero only
G=Cint(Rnd)*255 'Ensure 'g' is one or zero only
B=Cint(Rnd)*255 'Ensure 'b' is one or zero only
Line X1,Y1,X2,Y2,1,RGB(R,G,B) 'Draw the line
Line X1+1,Y1+1,X2+1,Y2+1,1,RGB(R,G,B) 'Draw the line
Line X1+2,Y1+2,X2+2,Y2+2,1,RGB(R,G,B) 'Draw the line
Line X1+3,Y1+3,X2+3,Y2+3,1,RGB(R,G,B) 'Draw the line
Pause 100 'Delay between lines(increase to slow down)
If Touch(DOWN) or LOC(#2)<>0 then 'If LCD touched or message received, stop screensaver
CLS 'Clear screen.
GUI SHOW ALL 'Re-enable all GUI elements
GOTO START
Endif
Next
LOOP






So what's odd about this one, is that the code should never get out of that first loop, but it is, and I don't see how.Edited by Grogster 2017-03-08
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 03:12am 07 Mar 2017
Copy link to clipboard 
Print this post

I think I am on to something here as to what is causing it. Standby......

EDIT: Yep, I know what is causing it now.

Schoolboy mistake, really - should not write code at 2AM.

The routine that was tripping it up was actually MAINSFAIL.
This is 100% correct too - the code looks at the opto-coupler, and as there is no connection to that just yet, the on-board 4k7 was pulling the pin high, the code sees that, and hops on over to MAINSFAIL.

.....and because I had not written those routines just yet, I just put in the labels to keep the interpreter happy, so it was jumping over each one('Nothing to see here! Move along!'), then it runs smack into the SCREENSAVER code, so executes it.

Bah!

Sleepy time now, will fix this up tomorrow.Edited by Grogster 2017-03-08
Smoke makes things work. When the smoke gets out, it stops!
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 03:29am 07 Mar 2017
Copy link to clipboard 
Print this post

Yep, lots of GOTO's which can cause these kinds of obscure running errors.
It looks though that most of them could be replaced with a call to a defined subroutine.
No risk of 'falling' through a label in that case.

Microblocks. Build with logic.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10565
Posted: 03:35am 07 Mar 2017
Copy link to clipboard 
Print this post

  Quote  Yep, lots of GOTO's which can cause these kinds of obscure running errors.
It looks though that most of them could be replaced with a call to a defined subroutine.
No risk of 'falling' through a label in that case.


An alternative is, for each test, to set a status flag with the condition number and then execute EXIT DO. That should ensure tidy loop exit. Then outside the loop just test the status for what to do next
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 02:09pm 07 Mar 2017
Copy link to clipboard 
Print this post

Good advise. I don't really like GOTO's - this has been talked about before, and I understand the reasons. But when you have a multi-page GUI program, if you try to put EVERYTHING inside one master loop, it would get VERY deep VERY quickly, and besides being hard to read with about 20 indents to the deeper loops, MMBASIC can't handle it.

Having said that, yes, I agree - I will change all these loops so that they set flags and EXIT DO rather then GOTO from within the loop - much cleaner as you say.

Last night's problem also stemmed from the fact that I was trying to add three or four new test IF/THEN lines and routines to the main loop, and had not written them yet, so just put in the labels. As I say - probably not a good idea to try to write code at 2AM when you brain is half asleep......
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


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

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025