![]() |
Forum Index : Microcontroller and PC projects : error
Author | Message | ||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 StatesPosts: 1427 |
Is there a CFunctions hook into any "break" caused by an error? If not, could there be? Micromites and Maximites! - Beginning Maximite |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10400 |
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 |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 KingdomPosts: 10400 |
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 See Geoff's release notes for 5.2 for details of the new error handling |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
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. Microblocks. Build with logic. |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9642 |
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: GermanyPosts: 1629 |
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: AustraliaPosts: 3303 |
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: GermanyPosts: 1629 |
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: AustraliaPosts: 3303 |
No worries. Recovering from errors is not easy to do in a clean way for any programming language. Geoff Geoff Graham - http://geoffg.net |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |