Go Down

Topic: Resetting millis() (Read 12861 times) previous topic - next topic

Zwaf

Jun 05, 2010, 10:36 pm Last Edit: Jun 05, 2010, 10:39 pm by Zwaf Reason: 1
Probably a short question.

I'm trying to reset millis() in Arduino (0017).
I looked at the following topic:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167861718

This explains me that with the help of
Code: [Select]

extern volatile unsigned long timer0_overflow_count;
timer0_overflow_count = 0;


I can reset millis().

However, when I try
Code: [Select]


extern volatile unsigned long timer0_overflow_count;

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 Serial.println(millis());
 if(millis() > 1000)
 {
   timer0_overflow_count = 0;
 }
 delay(100);
}


It does not work. millis() keeps incrementing.
If I directly read timer0_overflow_count it reports the following in a serial monitor:
Code: [Select]

0
99
99
98
99
98
99
99
98
etc...


What am I missing?

I am using an Atmega168.

Thanks in advance!

AWOL

Probaby a simple question - why?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Zwaf

#2
Jun 06, 2010, 12:07 am Last Edit: Jun 06, 2010, 12:12 am by Zwaf Reason: 1
I have this program that runs on 4 different 'agents'.
Together, these 4 agents form a game that lasts for about 60 seconds.

1) They have to send each other whether they are powered and then all must sync their timers. If I could reset millis() on command, this would solve the problem.

2) Also, I use an operation of
millis() % gameDuration
to cause millis to be constrained to 0 to 60 seconds. I could also simply reset millis() after 60 seconds if possible.

3) I also use a sleep state that kicks in after 60 seconds. During this sleep state, millis does not increment, resulting in other difficulties.

All of these could have been overcome if I could just reset the millis() timer. I could also try and do without, but this would result in a formula that has to take 8 variables into account.

Zwaf

Nevermind guys,

I have solved it by making a custom function of millis() that can be reset.

dbo

Zwaf, can you give me your function. I need to reset Millis too for the long life PID usage. Thanks.

BenF

Quote
It does not work. millis() keeps incrementing.

The way I see it is that it works just fine.

You have a 100ms delay after resetting the value and so should expect to print a millis() value around 100 each time through the loop.

You should disable interrupts while resetting the overlfow counter however to make it reliable.


bastukee

Quote
Probaby a simple question - why?


I second that!
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Scott Baker


Cerberus

Hello I want to know to how you guys were able to resolve this. In my case I had to do a custom form of delaying since the maximum delay that the Delay() function can handle is just 30seconds. I'm doing a logging systems that logs hourly. Another odd this is that My Arduino is getting crippled by the 9 hour rollover issue when it fact the millis() function has already been updated to last for 55 days... what am I missing here?

retrolefty

Quote
Hello I want to know to how you guys were able to resolve this. In my case I had to do a custom form of delaying since the maximum delay that the Delay() function can handle is just 30seconds. I'm doing a logging systems that logs hourly. Another odd this is that My Arduino is getting crippled by the 9 hour rollover issue when it fact the millis() function has already been updated to last for 55 days... what am I missing here?


If you look at the blink without delay sketch in the Arduino IDE digital examples you will see that there is no reason to use delay(), it's a rather primitive and blocking function that is best not used these days.

As far as your 9 hour rollover, are you aware that millis() returns a long value, sounds like you may be assigning it to a int variable rather then a long variable?

Lefty

Groove

Quote
the maximum delay that the Delay() function can handle is just 30seconds

Really? You sure?
Per Arduino ad Astra

Cerberus

Oh sorry about the delay yeah it was more than 30s but my concern is that I want to be able to override that delay in case I want to do something else.

Here's my code
Quote
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Wire.h>
#include <math.h>
#include <WProgram.h>
#include <DS1307.h>
#include <ctype.h>

Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

int r0 = 0;      //value select pin at the 4051 (s0)
int r1 = 0;      //value select pin at the 4051 (s1)
int r2 = 0;      //value select pin at the 4051 (s2)
int row = 0;     // storeing the bin code
int count = 0;    // just a count
int  bin [] = {000, 1, 10, 11, 100, 101, 110, 111};

int state;
char name [32];
char sb;
unsigned long time;
int buttonState = 0;
extern volatile unsigned long timer0_overflow_count;

void setup(void)
{  
 Serial.begin(9600);  
 analogWrite(5,255);
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);  
 pinMode(4, OUTPUT);
 pinMode(10, OUTPUT);      
 card.init();              
 volume.init(card);        
 root.openRoot(volume);    
 sprintf(name,"%d%d%d.CSV",RTC.get(DS1307_DATE,true),RTC.get(DS1307_MTH,true),RTC.get(DS1307_YR,true));
}
void loop(void){    
 
 timer0_overflow_count=0;
 unsigned long loopStart=millis();
 unsigned long waitTime = loopStart + 30000;  //30mins  
 
 buttonState = digitalRead(6);

 if (buttonState == HIGH) state=1;  
 else if(buttonState == LOW) state=0;
 else state = 0;

 if(Serial.available())
 {
   sb = Serial.read();
   if(sb=='v')
   {
    //some code here
   }    
 }
 if(state==1);
 {
   //some code here
 }
 //if(!Serial.available()&&!buttonState==LOW) delay(30000);
 
 do
 {
   //subroutine
 }
 while(millis()<waitTime&&!Serial.available()&&!buttonState==LOW);
 
}

Groove

Code: [Select]
unsigned long waitTime = loopStart + 30000;  //30mins  

30000 milliseconds is not 30 minutes.
Per Arduino ad Astra

Cerberus

I'm sorry about that that was modded but it used to be 180000... This code get affected by the 9 hour rollover... Why is that...

Go Up