Inquiry about Enabling the Watchdog Timer and Reset

For general Flowcode discussion that does not belong in the other sections.
Post Reply
bazkhf
Posts: 20
http://meble-kuchenne.info.pl
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Inquiry about Enabling the Watchdog Timer and Reset

Post by bazkhf »

Hello everyone,

I need help with enabling the Watchdog Timer on an Arduino board. I have searched through several articles on the topic, but I wasn't able to fully understand the correct way to enable the Watchdog as needed.

I understand that the Watchdog Timer will automatically restart the system if it is not reset via code (using wdt_reset()). However, I want to make sure how to properly enable the Watchdog Timer and how to handle the reset (Reset) when the program freezes or stops.

If you could provide code to enable the Watchdog along with resetting the system (Reset), or guide me through the correct steps to implement this on Arduino, I would greatly appreciate it.

Thank you in advance for your help.

Best regards,
bilal

medelec35
Matrix Staff
Posts: 1956
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 626 times
Been thanked: 656 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by medelec35 »

Hello, bilal.
Enabling the WDT on Ardion is nice and straight forward e.g for a watchdog timeout of 2 seconds:
1. Within Build >Project options, enable AutoClear watchdog.
2. At the very start of Main add a c code block with

Code: Select all

wdt_enable(WDTO_2S); 
That will automatically reset the WDT if a delay or any components that contain a delay is accessed.

If you need to manually add rest watchdog commands, then add a c code block within your project with

Code: Select all

wdt_reset();
Martin

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by bazkhf »

Hello Martin,

Thank you very much for your help. I will try the method you suggested and see if it resolves the issue. I will update you soon.

I always appreciate your support!

Best regards,
Bilal

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by bazkhf »

Hello Martin,

Thank you once again for your help!

I have tried the method you mentioned. Here’s what I did:

I selected the Arduino Uno board for programming.
I enabled the AutoClear watchdog option.
I added an LED and connected it to pin D13.
I added the following line before starting the loop: wdt_enable(WDTO_2S);
I added the first part of the code to keep the LED on for 3 seconds continuously.
After that, I entered the loop, where I repeated the process 10 times, and added code to turn the LED off for 900ms and on for 100ms.
I uploaded the code to the Arduino.
As expected, the LED was supposed to stay on continuously for 3 seconds initially, as the delay was set to 3 seconds, and then the Watchdog Timer was supposed to reset the Arduino every 2 seconds (due to WDTO_2S). However, what happened was that the LED stayed on for 3 seconds as expected at the start, and then it proceeded to execute the code within the loop, where the LED turned off for 900ms and on for 100ms, which indicates that the Watchdog Timer did not function as expected.

Note: I performed a toggle disable for the wdt_reset(); inside the code.

I will attach the test file with this message so you can review the code and provide any feedback if there are any errors I might have missed.

I truly appreciate your support!

Best regards,
Bilal
Attachments
watchdog01.fcfx
(10.12 KiB) Downloaded 23 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by mnfisher »

Hi Bilal,

The code you have is working correctly - in project options you have 'auto clear watchdog' - set to true. This will clear the wdt whenever there is a delay in your code (hence it won't trigger in the 3s delay or in the loop) - after the flashing - the program goes to a while(1); loop (rather than executing 'random' memory) - and the wdt will trigger restarting the program.

I you remove the auto clear toggle - the program will constantly restart during the 3s delay...

(other) Martin

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by bazkhf »

Hello medelec35,

I wanted to update you on my experience with the code. Initially, I hadn’t realized the meaning of AutoClear watchdog. I thought that enabling this option simply allowed the Watchdog Timer to function. But after writing my last message, I realized that this option causes the timer to be automatically cleared, which prevented the Arduino from resetting as I had expected.

Once I understood this, I re-tested the code without enabling the AutoClear watchdog option and only included the first line to activate the Watchdog Timer. The test was successful! After adding the wdt_reset() code to perform the reset, the full code worked as expected.

This confirms that the code you provided was functioning as intended, and I sincerely thank you for your valuable help.

Best regards,
Bilal

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Enabling the Watchdog Timer and Reset

Post by bazkhf »

Hello mnfisher,

Thank you for your input!

I now understand that enabling the AutoClear watchdog option causes the watchdog timer to be automatically cleared whenever there is a delay in the code, which prevents the timer from resetting during the 3-second delay or in the loop. This is why it wasn't working as I expected.

I re-tested the code after disabling AutoClear watchdog and only included the line wdt_enable(WDTO_2S);, which allowed the watchdog timer to work correctly. After adding the wdt_reset() code to perform the reset, the entire code worked as expected.

Thank you again for your helpful feedback!

Best regards,
Bilal

Post Reply