Pages: [1] 2   Go Down
Author Topic: Resetting millis()  (Read 10840 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
extern volatile unsigned long timer0_overflow_count;
timer0_overflow_count = 0;

I can reset millis().

However, when I try
Code:

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:
0
99
99
98
99
98
99
99
98
etc...

What am I missing?

I am using an Atmega168.

Thanks in advance!
« Last Edit: June 05, 2010, 03:39:19 pm by Zwaf » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probaby a simple question - why?
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 05, 2010, 05:12:50 pm by Zwaf » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nevermind guys,

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Probaby a simple question - why?

I second that!
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You second my typo?   ;D
Logged

"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.

Canby, OR
Offline Offline
Full Member
***
Karma: 1
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I third your typo!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the maximum delay that the Delay() function can handle is just 30seconds
Really? You sure?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  
}
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

30000 milliseconds is not 30 minutes.
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Pages: [1] 2   Go Up
Jump to: