Go Down

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

robtillaart

Quote
No I'm not bold enough to use %-operator in my code...


It would suprise me if the % operator has anything todo with it.

replace the line:

Code: [Select]
n1000 = millis() % 1000;

with

Code: [Select]

unsigned long t1 = millis();
unsigned long t2 = t1 / 1000;
n1000 = t1 - (t2 * 1000);


This is what the modulo does so now you can test the code without the % operator to see if it still behaves that way.

Please post your results.

Rob Tillaart

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

vesal

#11
Dec 30, 2010, 01:08 pm Last Edit: Dec 30, 2010, 01:41 pm by vesal Reason: 1
Now I have a code:

Code: [Select]

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

#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() {

 unsigned long t1 = millis();
 unsigned long t2 = t1 / 1000;
 n1000 = t1 - (t2 * 1000);

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

}




And that woks exactly same way in all 3 Arduino Uno-modules.
Pressing Rest button or starting Terminal hangs the Arduino.
Upulod or Power on resets and led in 7 starts to blink once
a second.

Instead the code:

Code: [Select]

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

#include <stdio.h>

const int dummyPort = 7;


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


unsigned long n1000 = 0;
boolean dOn = true;
unsigned long ms = 0;
unsigned long msHigh = 0;
unsigned long msgoal = 500000L;
unsigned long msgoalHigh = 0;

void loop() {

 ms = n1000;
 n1000 = micros();
 if ( n1000 < ms ) msHigh++;
 if ( n1000 >= msgoal && msHigh >= msgoalHigh) {
   digitalWrite(dummyPort, dOn);
   dOn = !dOn;
   unsigned long newms = msgoal + 500000L;
   if ( newms < msgoal ) msgoalHigh++;
   msgoal = newms;
 }  
}



works perfect in this form and also by lot of other stuff in
loop or in interrupts.  So what else I could suspect than
division (or modulo)?

vesal

Actually changing
Code: [Select]
n1000 = micros() % 1000000;
 
to
Code: [Select]

 n1000 = micros();
 while ( n1000 > 1000000L) n1000 -= 1000000L;


(where is no sense after few minutes of course)
makes the code work.  So even more proofs against
modulo % or division  / ???

 

James C4S

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

retrolefty

In your failing code example:

Code: [Select]
// n1000 = micros() % 1000000;
 if ( !dOn && n1000 >= 500 ) {
   digitalWrite(dummyPort, 1);
   dOn = 1;
 }  
 
 if ( dOn && n1000 < 500 ) {
   digitalWrite(dummyPort, 0);
   dOn = 0;
 }


Software isn't my strong suit, but shouldn't it be:

Code: [Select]
// n1000 = micros() % 1000000;
 if ( [glow]([/glow]!dOn && n1000[glow]) [/glow]>= 500 ) {
   digitalWrite(dummyPort, 1);
   dOn = 1;
 }  
 
 if ( [glow]([/glow]dOn && n1000[glow]) [/glow]< 500 ) {
   digitalWrite(dummyPort, 0);
   dOn = 0;
 }


Lefty

Go Up