Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:03 17 Sep 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 : error

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 04:38am 25 Aug 2016
Copy link to clipboard 
Print this post

Is there a way to implement a command such as "on error:run"?

ie if the micromite comes across an error to simply run the program again?
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1427
Posted: 04:48am 25 Aug 2016
Copy link to clipboard 
Print this post

Is there a CFunctions hook into any "break" caused by an error? If not, could there be?Edited by CircuitGizmos 2016-08-26
Micromites and Maximites! - Beginning Maximite
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10400
Posted: 05:04am 25 Aug 2016
Copy link to clipboard 
Print this post

No need for Cfunction just simple Basic

Option autorun on
On error ignore
Print"restarting"
SetTick 400,errcheck
i=5
Do
Print Sqr(i) 'will create an error when i goes negative
i=i-1
Pause 1000
Loop
Sub errcheck
If MM.Errno Then CPU restart
End Sub
Edited by matherp 2016-08-26
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:07am 25 Aug 2016
Copy link to clipboard 
Print this post

Simple Basic? thats way over my head, but thanks Peter, it will help a lot with a project I'm doing because "sometimes" when the program is run it doesn't find an i2c device, yet if I run it a 2nd time it sees it perfectly
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10400
Posted: 05:12am 25 Aug 2016
Copy link to clipboard 
Print this post

  Quote  Simple Basic? thats way over my head


No its not

I set the program to continue if it finds an error

ON ERROR IGNORE


I set up a regular timer interrupt, in this case every 400msec

SetTick 400,errcheck


In the timer interrupt I check the Basic variable that says whether there has been an error and if there has I restart the CPU

  Quote  If MM.Errno Then CPU restart


See Geoff's release notes for 5.2 for details of the new error handling

 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:14am 25 Aug 2016
Copy link to clipboard 
Print this post

With an on error ignore you can check for an error and handle it.
If you need to be sure your program always restarts when something unforeseen happens use the watchdog.
Edited by MicroBlocks 2016-08-26
Microblocks. Build with logic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9642
Posted: 03:22pm 25 Aug 2016
Copy link to clipboard 
Print this post

I agree with MicroBlocks here - Watchdog command is very useful for this kind of thing, and it only needs one line of code to refresh the watchdog timer to make sure it does not time-out and restart the MM, which will restart your code.

Further analysis of matherps example code:

Option autorun on - MM will run the code when powered up.

On error ignore - MM will NOT stop at the command prompt if it runs into an error, it will just execute the next line of code.

Print"restarting" - A prompt on the screen.

SetTick 400,errcheck - Set one of the tick timers to loop to 'errcheck' in the background every 400ms, regardless of what the main code is doing.

i=5 Make the value of variable 'i' five.

Do - Start of a DO/LOOP.

Print Sqr(i) 'will create an error when i goes negative - Print the square of variable 'i' on the console.

i=i-1 - Subtract one from the current value of 'i'.

Pause 1000 - Little delay...

Loop - Go and do it all again

Sub errcheck - Craete a subroutine called 'errcheck' for the tick timer to use.

If MM.Errno Then CPU restart - If the internal error-code variable is anything but zero, then restart the MM. This variable(MM.ERRNO) will be zero if no errors occured, or a non-zero number if an error of some kind happened.

End Sub - End of Subroutine.

Hopefully that helps you to see what matherp is doing in his example.
Smoke makes things work. When the smoke gets out, it stops!
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1629
Posted: 02:05am 26 Aug 2016
Copy link to clipboard 
Print this post

Perhaps this logic would be much easier to understand when Geoff the syntax changed so

[B]ON ERROR (GOSUB) errcheck[/B]

is allowed. As far as I remember was this the Microsoft Basic standard.
Peters (appreciated) code would then look like this
Option autorun on
ON ERROR errcheck
Print"restarting"

i=5
Do
Print Sqr(i) 'will create an error when i goes negative
i=i-1
Pause 1000
Loop

Sub errcheck
If MM.Errno Then CPU restart
End Sub


just my 2c
Michael
causality ≠ correlation ≠ coincidence
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3303
Posted: 03:27am 26 Aug 2016
Copy link to clipboard 
Print this post

I am not planning to implement ON ERROR GOSUB because it has limited value. It causes all errors to be sent to the one subroutine and as a result the subroutine becomes a maze of IF statements which try to decide where the error occurred and what to do about it. In fact the above requirement is one of the few cases where the error subroutine can be kept simple.

If you just want to restart on an error the best approach would be to use the WATCHDOG command as this is what it is designed to do.

Geoff
Geoff Graham - http://geoffg.net
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1629
Posted: 03:57am 26 Aug 2016
Copy link to clipboard 
Print this post

Hi Geoff,

I respect your opinion! But I think in some cases it could be useful to distinguish between critical and less critical errors. Another point is the time factor, sometimes you have to react very fast, as fast as possible.
An ON ERROR GOSUB would make it easy to save variables and should not cost too many resources.

Just some ideas and arguments ...
I do not intent to urge you. (I hope you will understand it right)

Michael
causality ≠ correlation ≠ coincidence
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3303
Posted: 11:14am 26 Aug 2016
Copy link to clipboard 
Print this post

No worries. Recovering from errors is not easy to do in a clean way for any programming language.

Geoff
Geoff Graham - http://geoffg.net
 
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