How can i handle cause of reset for Arduino?

i’m using arduino board , I uninstalled the bootloader located inside and program with usbasp.

i want to handle cause of reset.

Here is my code

.h file

#ifndef IWDGTRACE_H
#define IWDGTRACE_H

#include "Arduino.h"

#define mLOW_POWER_RESET 0
#define mWINDOW_WATCHDOG_RESET (MCUSR & 8)
#define mSOFTWARE_RESET 0
#define mPOWER_ON_RESET (MCUSR & 1)
#define mEXTERNAL_RESET_PIN_RESET (MCUSR & 2)
#define mBROWNOUT_RESET (MCUSR & 4)
#define mINDEPENDENT_WATCHDOG_RESET (MCUSR & 8)

typedef enum
{
    TO_THE_DETERMINED,
    LOW_POWER_RESET,
    WINDOW_WATCHDOG_RESET,
    INDEPENDENT_WATCHDOG_RESET,
    SOFTWARE_RESET,
    POWER_ON_RESET,
    POWER_DOWN_RESET,
    EXTERNAL_RESET_PIN_RESET,
    BROWNOUT_RESET,
    UNKNOWN,

}eCauseOfReset;

eCauseOfReset CauseOfReset(void);

#endif // IWDGTRACE_H

.cpp file

#include "IWDGTrace.h"

eCauseOfReset CauseOfReset(void)
{
    eCauseOfReset cor = TO_THE_DETERMINED;

    if(mLOW_POWER_RESET)
    {
        cor = LOW_POWER_RESET;
    }
    else if(mWINDOW_WATCHDOG_RESET)
    {
        cor = WINDOW_WATCHDOG_RESET;
    }
    else if(mSOFTWARE_RESET)
    {
        cor = SOFTWARE_RESET;
    }
    else if(mPOWER_ON_RESET)
    {
        cor = POWER_ON_RESET;
    }
    else if(mEXTERNAL_RESET_PIN_RESET)
    {
        cor = EXTERNAL_RESET_PIN_RESET;
    }
    else if(mBROWNOUT_RESET)
    {
        cor = BROWNOUT_RESET;
    }
    else
    {
        cor = UNKNOWN;
    }


    return cor;
}

.ino file

#include "IWDGTrace.h"
#include <avr/wdt.h>

void setup()
{  
   pinMode(13,OUTPUT);
   digitalWrite(13,0);
   digitalWrite(12,0);
   delay(2000);
  if (CauseOfReset() ==  7 ||  CauseOfReset() ==  2)
  {
    digitalWrite(12,1);
  }
  else
  {
    digitalWrite(13,1);
  }
}

void loop() {
  wdt_enable(WDTO_15MS);  

  while(1);
  
}

I reset the Arduino card, or I reset it because of wdt. But it always returns as UNKNOWN.

Note that MCUSR can have multiple bits set. If you never reset it, the “Power on” bit will always be set, for example.

I reset the Arduino card, or I reset it because of wdt. But it always returns as UNKNOWN.

  if (CauseOfReset() ==  7 ||  CauseOfReset() ==  2)

Actually unknown, or just “not 7 or 2”? I get 5 (power on) when I run a slightly modified version of your code on an Uno with the newer Optiboot (that doesn’t much touch MCUSR like it used to.) MCUSR itself is 7

That’s a TERRIBLE if statement, BTW. You went to all the trouble of defining an enum; you shouldn’t be comparing against integer constant!

Likewise there are nice symbolic constants for the MCUSR bits.

#define mPOWER_ON_RESET (MCUSR & 1)

Should be

#define mPOWER_ON_RESET (MCUSR & (1<<PORF))

and etc.
(I like the use of 0 for the reset causes that don’t exist on this particular chip. The code will just disappear during optimization…)

pinMode(12,OUTPUT) ; ?

westfw:
Note that MCUSR can have multiple bits set. If you never reset it, the “Power on” bit will always be set, for example.

Actually unknown, or just “not 7 or 2”? I get 5 (power on) when I run a slightly modified version of your code on an Uno with the newer Optiboot (that doesn’t much touch MCUSR like it used to.) MCUSR itself is 7

That’s a TERRIBLE if statement, BTW. You went to all the trouble of defining an enum; you shouldn’t be comparing against integer constant!

Likewise there are nice symbolic constants for the MCUSR bits.

#define mPOWER_ON_RESET (MCUSR & 1)

Should be

#define mPOWER_ON_RESET (MCUSR & (1<<PORF))

and etc.
(I like the use of 0 for the reset causes that don’t exist on this particular chip. The code will just disappear during optimization…)

Hi , @westfw thanks for reply ,

You 're right , if statement looks like terrible ,

if i press the reset button on Arduino Uno Board , i get “UNKNOWN” . i tried without bootloader

void setup()
{
  wdt_enable(WDTO_8S);  // if a WDT reset occured, the WDT will still be on, and we can't disable it
                        //  till after we clear WDRF in MCUSR.  Stretch it out, instead, so we can look...
  pinMode(13, OUTPUT);
  digitalWrite(13, 0);
  digitalWrite(12, 0);
  Serial.begin(9600);
  while (!Serial) ;   // wait for serial to start
  Serial.print("MCUSR = "); Serial.println(MCUSR);
  Serial.print("CauseOfReset = "); Serial.println(CauseOfReset());
  delay(1000);       // wait for printing to finish
  MCUSR &= ~((1 << PORF) | (1 << BORF) | (1 << EXTRF));  // clear some of the causes
  if (CauseOfReset() ==  7 ||  CauseOfReset() ==  2)  {
    digitalWrite(12, 1);
  }  else  {
    digitalWrite(13, 1);
  }
}
MCUSR = 8
CauseOfReset = 2
MCUSR = 2    [color=red]hits RESET button, sometime around here.[/color]
CauseOfReset = 7
MCUSR = 8
CauseOfReset = 2
MCUSR = 8
CauseOfReset = 2