Using watchdog without any bootloader SOLVED!

Dear, After struggling since few days, I am asking for solution, Using watchdog without any bootloader!

I am using latest arduino compiler, board is mini pro. Tried included library, Adafruit Watchdog Library and nadavmatalon library. None of the example worked for me :confused: results stuck in infinite loop after triggering watchdog.

I need urgent please!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Finally found working codes at
https://microcontrollerslab.com/arduino-watchdog-timer-tutorial/
NOTE: No BOOTLOADER and BOOTRST = 0 (set) and WDTON = 1 (not set)
Tested in 328P and 328PB

// Title : Watchdog
// Author : Claus Kuehnel <ckuehnel@gmx.ch>
// Date : 2017-05-10
// Id : watchdog.ino
// Tested w/ : Arduino 1.8.0
//
// DISCLAIMER:
// The author is in no way responsible for any problems or damage caused by
// using this code. Use at your own risk.
//
// LICENSE:
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
// Definition of interrupt names
#include <avr/io.h>
// ISR interrupt service routine
#include <avr/interrupt.h>
#define wdt_reset() __asm__ __volatile__ ("wdr")
const int pLED = 13; // LED at Pin13
int idx;

// Install the interrupt routine for Watchdog Interrupt
ISR(WDT_vect)
{
flash();
}
void setup()
{
Serial.begin(9600);
pinMode(pLED, OUTPUT);
digitalWrite(pLED, LOW);
cli();
wdt_reset();
WDTCSR |= (1<<WDCE) | (1<<WDE); // Start timed sequence
WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1); // Set new prescaler = 128K cycles(~1 s)
sei();
Serial.print("WDTCSR: ");
Serial.println(WDTCSR, HEX);
Serial.println("Setup finished.");
}
void loop()
{
Serial.println(idx++); // do anything
delay(1000); // change argument to 1500 -> watchdog will be active
wdt_reset();
}
void flash()
{
static boolean output = HIGH;
digitalWrite(pLED, output);
output = !output;
}

Please read the "How to use this forum" post and follow the directions carefully.

Post your code, using code tags.

Excuse me! well I thought its a simple issue and some one here may have already experienced.
Well I solved it and like to share

//https://github.com/nadavmatalon/WatchDog

#include "WatchDog.h"
byte WDTcounts;
void(* resetFunc) (void) = 0;//declare reset function at address 0

void setup() {
WDTcounts=0;
    WatchDog::init(WDTint_, 500);

}

void loop() {
WDTcounts=0; // if you remove it your system will RESET

}

void WDTint_() {                                         // watchdog timer interrupt service routine    
WDTcounts=WDTcounts+1;
if(WDTcounts>2){
    WDTcounts=0;
    resetFunc();}

}

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

I don't understand the purpose of that code. It looks like you are using the library to reproduce the standard behavior of the watchdog but with a half-assed jump to zero style of reset? The intent of the library is to make it easier (debatable) to use the watchdog ISR. If you only want to reset the Arduino if your program hangs then use the standard avr-libc avr/wdt.h library

This is much less complex and will give you a proper reset. Note that the standard bootloader used on the Pro Mini has a bug that causes an endless reset loop after a watchdog reset but since you're not using a bootloader that's not a problem and even if you wanted to use a bootloader you just have to burn the optiboot bootloader (can use Uno bootloader if using 16 MHz Pro Mini).

The following code will accomplish much the same thing as your code. The only difference is it's a 2 second timeout instead of 1.5 seconds and it does a real reset:

#include <avr/wdt.h>

void setup() {
  wdt_enable(WDTO_2S);  //enable the watchdog timer with a 2 second timeout duration
}

void loop() {
  wdt_reset();  //reset the watchdog timer
}

Much more simple and saves memory too!

Dear Pert,
I already tried same as you suggested but result was endless reset loop after a watchdog reset. spends hours! >:(
finally the code suggested by me worked fine.
Thanks

It must have been the bootloader bug I mentioned causing the reset loop. It's very easy to solve.

Dear Pert! Please read the title of the topic...using watchdog without any bootloader....I have no bootloader on my arduino.

Are you sure that you have no bootloader? What code is in the bootloader area of program memory? What code is in the region starting at zero in program memory?

So far, there is no explanation for why you experienced the "endless reset loop" OR why your code fixed the problem.

Dear, Yes no bootloader, using "Export compiled binary", and then using USBASP and generated HEX file (without bootloader).....
Please ready this article for memory map and other infos.

atmega328p-memory-map.png