0
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« on: June 05, 2010, 03:36:36 pm » |
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=1167861718This explains me that with the help of extern volatile unsigned long timer0_overflow_count; timer0_overflow_count = 0;
I can reset millis(). However, when I try 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: 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
Online
Brattain Member
Karma: 144
Posts: 19382
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: June 05, 2010, 04:48:52 pm » |
Probaby a simple question - why?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #2 on: June 05, 2010, 05:07:32 pm » |
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
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #3 on: June 06, 2010, 04:46:05 pm » |
Nevermind guys,
I have solved it by making a custom function of millis() that can be reset.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
|
 |
« Reply #4 on: June 22, 2010, 09:37:29 am » |
Zwaf, can you give me your function. I need to reset Millis too for the long life PID usage. Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #5 on: June 22, 2010, 10:42:08 am » |
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
Full Member
Karma: 0
Posts: 112
Arduino rocks
|
 |
« Reply #6 on: June 22, 2010, 04:32:05 pm » |
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
Online
Brattain Member
Karma: 144
Posts: 19382
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: June 22, 2010, 04:35:21 pm » |
You second my typo? ;D
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Canby, OR
Offline
Full Member
Karma: 1
Posts: 158
Arduino rocks
|
 |
« Reply #8 on: June 23, 2010, 10:52:35 pm » |
I third your typo!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #9 on: September 02, 2010, 10:00:44 pm » |
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
Brattain Member
Karma: 283
Posts: 15443
Measurement changes behavior
|
 |
« Reply #10 on: September 03, 2010, 12:21:36 am » |
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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #11 on: September 03, 2010, 01:30:52 am » |
the maximum delay that the Delay() function can handle is just 30seconds Really? You sure?
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #12 on: September 03, 2010, 07:39:28 am » |
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 #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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #13 on: September 03, 2010, 09:39:58 am » |
unsigned long waitTime = loopStart + 30000; //30mins 30000 milliseconds is not 30 minutes.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #14 on: September 03, 2010, 10:11:21 am » |
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
|
|
|
|
|
|