Reset Interrupt possible?

Hi

I would like to know if it is possible to reset the arduino with code only?
I know the highest priority interrupt is the reset interrupt but how do I use it on code?

Thanks
Ard

I would like to know if it is possible to reset the arduino with code only?

This has been discussed many, many times. The answer is almost always the same - don't do that; fix the code that is causing the problem.

So, what problem do you have that you hope resetting will solve?

Gosh. Twice in a week.

:frowning: sorry...

The 50 day millis() overflow Problem.

I have some code that will ultimately after an overflow just switch on EVERYTHING it shouldn't and will do so for almost 50 days...

Unless I reset the arduino which will take like 3 seconds and then I have a new 50Days.

The 50 day millis() overflow Problem.

isn't a problem if you write the code correctly. Your watch overflows every day. How often does that cause you to need to be reset?

The last recorded millis() variable will be recorded somewhere at the end of the 50 days and because im working with " Current-Millis() - Initial-Millis() < xx " or " Current-Millis() - Initial-Millis() > xx "
will undoubtedly be a problem since the Millis() a.k.a " Current-Millis() " will overflow and start from 0 which will mess up method of using Millis() above.

Plus the Restart will really not be that big deal to be honest.Wont mess up anything.
Chances are that something happens in the 3 seconds the Arduino restarts are probably not much.

An example of my millis method Delay.

char  Track  = 0;
unsigned long InitialTime

void Example()
  {
  
    if (Track == 0)
      {
        InitialTime=millis();
        Track =1;
      }
      
      unsigned long yy = millis();
      if (yy - InitialTime < 5)
        {
          
        }
      else if (yy - InitialTime >=5 && yy - InitialTime < 10)
        {
         
          
        }
        else if (yy - InitialTime > 10)
        {
        Track =0;
        }
         
  }

will undoubtedly be a problem since the Millis() a.k.a " Current-Millis() " will overflow and start from 0 which will mess up method of using Millis() above.

No, it won't. Subtraction involving unsigned longs works.

You can test it, using micros(), which overflows the same way, only a whole lot sooner.

Plus the Restart will really not be that big deal to be honest.

Except that there is no guaranteed way to have the Arduino press the reset switch.

ArdGuy;

If you use unsigned longs for time then when you subtract past from present it will be right even with rollover.

Here's a short sketch that shows unsigned math at work with numbers in decimal, hex and binary to show what goes on.

unsigned long a, b, c;

void setup() {
  Serial.begin( 9600 );
  a = 0xffffff00UL;
  b = 0x10UL;
  Serial.println( "\n unsigned math\n" );
  Serial.print( "a = ");
  Serial.print( a, DEC );
  Serial.print( " = 0x");
  Serial.print( a, HEX );
  Serial.print( " = 0b");
  Serial.println( a, BIN );
  Serial.print( "b = ");
  Serial.print( b, DEC );
  Serial.print( " = 0x");
  Serial.print( b, HEX );
  Serial.print( " = 0b");
  Serial.println( b, BIN );
  if ( b >= a ) Serial.println( "b >= a" );
  else          Serial.println( "a > b" );
  c = a - b;
  Serial.print( "a - b = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.print( c, HEX );
  Serial.print( " = 0b");
  Serial.println( c, BIN );
  c = b - a;
  Serial.print( "b - a = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.print( c, HEX );
  Serial.print( " = 0b");
  Serial.println( c, BIN );
  c = b - (b + 1);
  Serial.print( "b - (b + 1) = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.print( c, HEX );
  Serial.print( " = 0b");
  Serial.println( c, BIN );
}

void loop() {};

Hey Guys

Thanks allot for helping me understand the overflow problem :slight_smile:

Appreciated!!

So the way my makeshift Millis() function work is good?

Thanks
Ard

You don't need it unless you feel a need to waste cycles, flash and ram.

Unsigned math is like a clock. From 11 to 1 is 2 hours without having to add or subtract 12.
That's what you should see in the sketch I posted above, but you have to look. How long does it take to do that? Should be far less time than you have spent asking for answers.

Ok cool I see.

I get what you are saying. One more thing though

After roughly 50 Days it will overflow meaning Millis() should be somewhere close to " 4320000000 "
Will it be possible to say if (millis() >= 4319500000) { var1 = 0 ; } ?

The output of that sketch I posted shows that with unsigned math roll over is not a problem.
Run the sketch, change the numbers, compile and run it again. It shows the results.

You've got to let go of signed integer thinking to understand unsigned integers. Unsigned integers -can't- be negative. That's -any- unsigned integer so let's play with unsigned 8-bit, known as byte.

byte B = 1; // binary 00000001
byte A = 250; // binary 11111010
// B - A = 7 binary 00000111 -- the 'borrow' is automatic, the results will be the lower 8 bits
B = 255; // binary 11111111
A = 250; // binary 11111010
// B - A = 5 binary 00000101
A = 250; // binary 11111010
B = 7; // binary 00000111
// A + B = binary 100000001 -- 9 bits, you only get the lower 8, the overflow does not matter

You don't need an if(). What you do need is to spend however long to understand that it works.

What overflow can mess up is if you overflow by half or more what your bits can hold. Don't let that happen. But with 49.7 days maximum you have over 3 weeks to catch the change.