Restting the arduino UNO: only once

Hello,

I want to reset the arduino only once.
i have found Nick Gammon sir’s code like

#include <avr/wdt.h>
void setup ()
{
  Serial.begin (115200);
  Serial.println ("Restarted.");
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
}  // end of setup

void loop ()
{
  Serial.println ("Entered loop ...");
  wdt_reset ();  // give me another second to do stuff (pat the dog)
  while (true) ;   // oops, went into a loop
}  // end of loop

but in this after every 1 second device is resetting but i want t rese the code only if some condition is true.how to do that using avr/wdt.h library

but i want t rese the code only if some condition is true

Stop patting the dog when the condition is true.

Make sure you do a wdt_reset() every second. Stop doing it when the condition is True.

i’m doing this

[code]#include <avr/wdt.h>

void setup ()
{
  Serial.begin (9600);
  Serial.println ("Restarted.");
    // reset after one second, if no "pat the dog" received
}  // end of setup

void loop ()
{
  Serial.println ("Entered loop ...");
  //wdt_reset ();  // give me another second to do stuff (pat the dog)
  //while (true) ;   // oops, went into a loop
  for(int a=0;a<50;a++)
   {
    Serial.println(a); 
    if(a==25)
     { wdt_enable (WDTO_1S);
     }
      delay(5000);
   }
   
}  // end of loop

[/code]

is this OK?

is this OK?

If it's doing what you want, then it's probably OK. If it isn't doing what you want, then it's probably not OK.

it’s working…
sir how to pass char array using function call and how to receive in function.i have done like this but it hangs after some time.

i’m printing i on serial, so up to 12 time it prints 0…79 and one time 0…17 and then hangs.
if i restart board then also at same time it hangs why this?

char buffer[80];

funcall(buffer);


void decode_sms(char *array1)
{
   for(byte i=0;i<80;i++)
     {Serial.print(i); Serial.print(' ');Serial.println(array1[i]);}

}

i have put array1[79]=NULL;
then also hang’s.

ankushg989: i have put array1[79]=NULL; then also hang's.

Your Arduino needs to know the difference between "the first boot" and "subsequent boots". It needs to "remember" what happened last time. You can't depend on SRAM holding a value, you can't write to PROGMEM inside a program, so what's left? EEPROM. Pick one EEPROM address to store a flag that tells you "first boot" or "subsequent boot" and then setup (or not) the watchdog timer accordingly.

But sir, why arduino hangs? i have used your provided freememory() library. it shows 403 bytes.

arduino also does not shows error to know reason for hanging.how to debug?

ankushg989:
i’m printing i on serial, so up to 12 time it prints 0…79 and one time 0…17 and then hangs.
if i restart board then also at same time it hangs why this?

That is not the full sketch. The reason for the hang is probably in the part you didn’t show. Show the full sketch.

Hello,

I'm trying to reset controller using

wdt_enable(WDTO_1S);

i want to know is this reset starts from setup()or from global variable assign.