Go Down

Topic: Problem with modulo (%) and reset (Read 14 times) previous topic - next topic

vesal

I have a problem that after a small piece of code the Arduoino (UNO)
does no reset.  It resets only by uploading a new sketch or by power off.

In the code below I separated the problem to % operation.  My idea
was to give exact 1 second signal compared to last reset.  I can go
around the problem by other ways, but I'm worried about why this peace of code has a problem.  Because I got the same problem in longer code but occasionally (when interrupt happens during %).

My guess is that if reset or interrupt happens during % then the board is somehow  in unknown state and does not reset.

The problem can be repeated by code below and pressing the reset button repeatedly until led in line 13 remains blinking.  Then the Arduiono is possible to reset only by new sketch or power on.


#include <Wire.h>
#include <string.h>

#undef int
#include <stdio.h>

const int dummyPort = 7;


void setup() {
 Serial.begin(115200);
 Serial.println("Start Arduino");
 pinMode(dummyPort, OUTPUT);
 
}


unsigned long n1000 = 0;
int dOn = 0;


void loop() {
 
 n1000 = micros() % 1000000;
 if ( !dOn && n1000 >= 500000 ) {
   digitalWrite(dummyPort, 1);
   dOn = 1;
 }  
 
 if ( dOn && n1000 < 500000 ) {
   digitalWrite(dummyPort, 0);
   dOn = 0;
 }
 

}

:o

robtillaart

Quote
I have a problem that after a small piece of code the Arduino (UNO) does no reset.


How should this code do a reset? as simple as the code is I do not understand how it should do a reset.

some remarks:

instead of - int dOn - use a boolean variable, is more clear imho

I can imagine that the micros() wraps around quite fast - start with 0 again, maybe you should try millis() instead as it takes 1000 times as long before wrapping around.

(code not tested)
Code: [Select]

unsigned long n1000 = 0;
boolean dOn = false;

void loop()
{
 n1000 = millis() % 1000;
 if ( !dOn && n1000 >= 500 )
 {
   digitalWrite(dummyPort, 1);
   dOn = true;
 }  
 if ( dOn && n1000 < 500 )
 {
   digitalWrite(dummyPort, 0);
   dOn = false;
 }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

vesal

This code is not doing the reset.  The problem is that if YOU do reset
somehow during this code, the Aruino hangs and you can not do another
reset.  Why should you do the reset?  When you start serial from PC
it sinks the DTR and resets Arduino. So the problem is that when you have code like my example, you have good change to hang the Arduino
when you connect it thru USB from PC.  And to simulate that it is much faster to just press the reset button and see what happends.

The micros is just OK for me, because millis is not accurate enough in my purpose.  And dOn I admit that boolean is better in this case, but this was just a smallest peace of code from much much bigger case
where a noticed the problem and tried to minimize the problem something that is easy to repeat.  

And the worst thing is that the same problem comes when some interrupt happens.  And that is the main point, but I think Reset is more familiar for most people.

;)

robtillaart

OK, so when you press the reset button the Arduino locks itself up in a permanent state. Sounds definitely as a hardware problem.
If you have a voltmeter you could check if the reset switch is working correctly or if it blocks.
Also check your board for small pieces that connect (bad soldering a bit of grease whatever).
You could also circumvent the reset switch by resetting using the reset pin.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

vesal

I have 3 Arduino Uno module and all behaves the same way.
And the reset comes also from USB-serial line by DTR low and
it has exactly same effect.  So do external reset.

The difference in reset by reset button, external reset,
serial start reset and uplod is that when upload is done, the
rset signal stays 5 V, goes to 0 V and the over 7 V and then back to
5 V.  All others just pull reset signal to 0 V and returns it 5 V.

The reset is one of my worries.  The biggest is that WHY Arduino
hangs when Reset or Interrupt is done during %-operation???

:-[

Go Up